jssign 0.3.1 → 0.3.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +32 -11
- package/dist/cjs/index.js +19 -12
- package/dist/esm/index.d.ts +1 -1
- package/dist/esm/index.js +20 -13
- package/package.json +45 -44
package/README.md
CHANGED
|
@@ -1,10 +1,16 @@
|
|
|
1
1
|
# jssign
|
|
2
|
+
|
|
2
3
|
A better, faster, lighter and more secure alternative to [jsonwebtoken](https://www.npmjs.com/package/jsonwebtoken)
|
|
4
|
+
|
|
3
5
|
## Features
|
|
6
|
+
|
|
4
7
|
- Encrypt data using a secret
|
|
5
8
|
- Decrypt a token with secret to retrive data back
|
|
9
|
+
|
|
6
10
|
## Installation
|
|
11
|
+
|
|
7
12
|
To install jssign
|
|
13
|
+
|
|
8
14
|
```bash
|
|
9
15
|
# with npm:
|
|
10
16
|
npm install jssign --save
|
|
@@ -18,49 +24,64 @@ To install jssign
|
|
|
18
24
|
# with bun:
|
|
19
25
|
bun add jssign
|
|
20
26
|
```
|
|
27
|
+
|
|
21
28
|
## Usage
|
|
29
|
+
|
|
22
30
|
`jssign` exports different functions for data encryption for different use cases:
|
|
31
|
+
|
|
23
32
|
### Faster Usage
|
|
33
|
+
|
|
24
34
|
For a faster (but less secure) encoding and decoding of data using a secret, `jssign` exports the following functions:
|
|
35
|
+
|
|
25
36
|
- `sign(data, secret, options)`: returns encoded token
|
|
26
37
|
- `verify(token, secret)`: returns decoded data
|
|
38
|
+
|
|
27
39
|
```javascript
|
|
28
|
-
import { sign, verify } from
|
|
40
|
+
import { sign, verify } from "jssign";
|
|
29
41
|
|
|
30
|
-
const secret =
|
|
31
|
-
const token = sign({ foo:
|
|
32
|
-
const data = verify(token, secret)
|
|
42
|
+
const secret = "top-secret";
|
|
43
|
+
const token = sign({ foo: "bar" }, secret, { sl: 16 }); // no expiration
|
|
44
|
+
const data = verify(token, secret);
|
|
33
45
|
|
|
34
|
-
console.log(data) // { foo: 'bar' }
|
|
46
|
+
console.log(data); // { foo: 'bar' }
|
|
35
47
|
```
|
|
48
|
+
|
|
36
49
|
`data` can be an object literal, buffer or string representing valid JSON.
|
|
37
50
|
|
|
38
51
|
`secret` can be a string
|
|
39
52
|
|
|
40
53
|
`options`:
|
|
54
|
+
|
|
41
55
|
- `expiresIn` can be a numeric value representing time in ms (default value is `0` which represents no expiration).
|
|
42
56
|
- `sl` can be a numberic value representing salt length (default value is `32`). Salt is a random string which is added on top of data to keep the token different everytime even for the same data.
|
|
43
57
|
|
|
44
58
|
### More secure Usage
|
|
59
|
+
|
|
45
60
|
For a more secure (but slower) encryption and decryption of data using a secret, `jssign` exports the following functions that uses [sjcl](https://www.npmjs.com/package/sjcl) under the hood:
|
|
61
|
+
|
|
46
62
|
- `encrypt(data, secret, options, sjclOptions)`: return encrypted token
|
|
47
63
|
- `decrypt(token, secret)`: returns decrypted data
|
|
64
|
+
|
|
48
65
|
```javascript
|
|
49
|
-
import { encrypt, decrypt } from
|
|
66
|
+
import { encrypt, decrypt } from "jssign";
|
|
50
67
|
|
|
51
|
-
const secret =
|
|
52
|
-
const token = encrypt({ id:
|
|
53
|
-
const data = decrypt(token, secret)
|
|
68
|
+
const secret = "top-secret";
|
|
69
|
+
const token = encrypt({ id: "confidential_data" }, secret, { expiresIn: 180000 }); // will expire after 30 minutes of token creation
|
|
70
|
+
const data = decrypt(token, secret);
|
|
54
71
|
|
|
55
|
-
console.log(data) // { id: 'confidential_data' }
|
|
72
|
+
console.log(data); // { id: 'confidential_data' }
|
|
56
73
|
```
|
|
74
|
+
|
|
57
75
|
`data` can be an object literal, buffer or string representing valid JSON.
|
|
58
76
|
|
|
59
77
|
`secret` can be a string
|
|
60
78
|
|
|
61
79
|
`options`:
|
|
80
|
+
|
|
62
81
|
- `expiresIn` can be a numeric value representing time in ms (default value is `0` which represents no expiration).
|
|
63
82
|
|
|
64
83
|
`sjclOptions` are the options taken by `sjcl.encrypt` method having type `SjclCipherEncryptParams`
|
|
84
|
+
|
|
65
85
|
## Author
|
|
66
|
-
|
|
86
|
+
|
|
87
|
+
[Sahil Aggarwal](https://www.github.com/SahilAggarwal2004)
|
package/dist/cjs/index.js
CHANGED
|
@@ -5,12 +5,12 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.decrypt = exports.encrypt = exports.verify = exports.sign = void 0;
|
|
7
7
|
const sjcl_1 = __importDefault(require("sjcl"));
|
|
8
|
-
const characters = [
|
|
8
|
+
const characters = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "`", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "-", "=", "[", "]", ";", ",", ".", "/", "~", "!", "@", "#", "$", "%", "^", "&", "*", "(", ")", "_", "+", "{", "}", ":", "<", ">", "?"];
|
|
9
9
|
const encoder = new TextEncoder();
|
|
10
10
|
const decoder = new TextDecoder();
|
|
11
11
|
const randomNumber = (min, max) => min + Math.floor(Math.random() * (max - min + 1));
|
|
12
12
|
const randomElement = (array) => array[randomNumber(0, array.length - 1)];
|
|
13
|
-
const textToChars = (text) => text.split(
|
|
13
|
+
const textToChars = (text) => text.split("").map((c) => c.charCodeAt(0));
|
|
14
14
|
const byteHex = (n) => ("0" + Number(n).toString(16)).slice(-2);
|
|
15
15
|
const stringFromCode = (code) => String.fromCharCode(code);
|
|
16
16
|
function genSalt(length) {
|
|
@@ -22,7 +22,7 @@ function genSalt(length) {
|
|
|
22
22
|
function encode(data, secret, type) {
|
|
23
23
|
if (type === 0) {
|
|
24
24
|
const applySecret = (code) => textToChars(secret).reduce((a, b) => a ^ b, code);
|
|
25
|
-
return data.toString().split(
|
|
25
|
+
return data.toString().split("").map(textToChars).map(applySecret).map(byteHex).join("");
|
|
26
26
|
}
|
|
27
27
|
const dataBytes = encoder.encode(data);
|
|
28
28
|
const secretBytes = encoder.encode(secret);
|
|
@@ -31,14 +31,21 @@ function encode(data, secret, type) {
|
|
|
31
31
|
const tokenBytes = new Uint8Array(dataLength);
|
|
32
32
|
for (let i = 0; i < dataLength; i++)
|
|
33
33
|
tokenBytes[i] = dataBytes[i] ^ secretBytes[i % secretLength];
|
|
34
|
-
return Array.from(tokenBytes)
|
|
34
|
+
return Array.from(tokenBytes)
|
|
35
|
+
.map((byte) => byte.toString(16).padStart(2, "0"))
|
|
36
|
+
.join("");
|
|
35
37
|
}
|
|
36
38
|
function decode(token, secret, type) {
|
|
37
39
|
if (type === 0) {
|
|
38
40
|
const applySecret = (code) => textToChars(secret).reduce((a, b) => a ^ b, code);
|
|
39
|
-
return token
|
|
41
|
+
return token
|
|
42
|
+
.match(/.{1,2}/g)
|
|
43
|
+
.map((hex) => parseInt(hex, 16))
|
|
44
|
+
.map(applySecret)
|
|
45
|
+
.map(stringFromCode)
|
|
46
|
+
.join("");
|
|
40
47
|
}
|
|
41
|
-
const tokenBytes = new Uint8Array(token.match(/.{1,2}/g).map(byte => parseInt(byte, 16)));
|
|
48
|
+
const tokenBytes = new Uint8Array(token.match(/.{1,2}/g).map((byte) => parseInt(byte, 16)));
|
|
42
49
|
const secretBytes = encoder.encode(secret);
|
|
43
50
|
const tokenLength = tokenBytes.length;
|
|
44
51
|
const secretLength = secretBytes.length;
|
|
@@ -56,7 +63,7 @@ function sign(data, secret, { expiresIn = 0, sl = 32 } = {}) {
|
|
|
56
63
|
exports.sign = sign;
|
|
57
64
|
function verify(token, secret) {
|
|
58
65
|
try {
|
|
59
|
-
const [dataStr, signature] = token.split(
|
|
66
|
+
const [dataStr, signature] = token.split(".");
|
|
60
67
|
const salt = decode(signature, secret, 0);
|
|
61
68
|
const { data, iat, exp } = JSON.parse(decode(dataStr, salt, 1));
|
|
62
69
|
if (!exp || Date.now() < iat + exp)
|
|
@@ -64,21 +71,21 @@ function verify(token, secret) {
|
|
|
64
71
|
throw new Error();
|
|
65
72
|
}
|
|
66
73
|
catch (_a) {
|
|
67
|
-
throw new Error(
|
|
74
|
+
throw new Error("Invalid token or secret!");
|
|
68
75
|
}
|
|
69
76
|
}
|
|
70
77
|
exports.verify = verify;
|
|
71
78
|
function encrypt(data, secret, { expiresIn = 0 } = {}, sjclOptions) {
|
|
72
79
|
const encryptedObj = JSON.parse(sjcl_1.default.encrypt(secret, JSON.stringify({ data, iat: Date.now(), exp: expiresIn }), sjclOptions));
|
|
73
80
|
const encryptedArr = Object.entries(encryptedObj).map(([key, value]) => `${key}:${value}`);
|
|
74
|
-
return encryptedArr.join(
|
|
81
|
+
return encryptedArr.join(".");
|
|
75
82
|
}
|
|
76
83
|
exports.encrypt = encrypt;
|
|
77
84
|
function decrypt(token, secret) {
|
|
78
85
|
try {
|
|
79
|
-
const encryptedArr = token.split(
|
|
86
|
+
const encryptedArr = token.split(".");
|
|
80
87
|
const encryptedObj = encryptedArr.reduce((obj, str) => {
|
|
81
|
-
const [key, value] = str.split(
|
|
88
|
+
const [key, value] = str.split(":");
|
|
82
89
|
obj[key] = +value || value;
|
|
83
90
|
return obj;
|
|
84
91
|
}, {});
|
|
@@ -88,7 +95,7 @@ function decrypt(token, secret) {
|
|
|
88
95
|
throw new Error();
|
|
89
96
|
}
|
|
90
97
|
catch (_a) {
|
|
91
|
-
throw new Error(
|
|
98
|
+
throw new Error("Invalid token or secret!");
|
|
92
99
|
}
|
|
93
100
|
}
|
|
94
101
|
exports.decrypt = decrypt;
|
package/dist/esm/index.d.ts
CHANGED
package/dist/esm/index.js
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import sjcl from
|
|
2
|
-
const characters = [
|
|
1
|
+
import sjcl from "sjcl";
|
|
2
|
+
const characters = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "`", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "-", "=", "[", "]", ";", ",", ".", "/", "~", "!", "@", "#", "$", "%", "^", "&", "*", "(", ")", "_", "+", "{", "}", ":", "<", ">", "?"];
|
|
3
3
|
const encoder = new TextEncoder();
|
|
4
4
|
const decoder = new TextDecoder();
|
|
5
5
|
const randomNumber = (min, max) => min + Math.floor(Math.random() * (max - min + 1));
|
|
6
6
|
const randomElement = (array) => array[randomNumber(0, array.length - 1)];
|
|
7
|
-
const textToChars = (text) => text.split(
|
|
7
|
+
const textToChars = (text) => text.split("").map((c) => c.charCodeAt(0));
|
|
8
8
|
const byteHex = (n) => ("0" + Number(n).toString(16)).slice(-2);
|
|
9
9
|
const stringFromCode = (code) => String.fromCharCode(code);
|
|
10
10
|
function genSalt(length) {
|
|
@@ -16,7 +16,7 @@ function genSalt(length) {
|
|
|
16
16
|
function encode(data, secret, type) {
|
|
17
17
|
if (type === 0) {
|
|
18
18
|
const applySecret = (code) => textToChars(secret).reduce((a, b) => a ^ b, code);
|
|
19
|
-
return data.toString().split(
|
|
19
|
+
return data.toString().split("").map(textToChars).map(applySecret).map(byteHex).join("");
|
|
20
20
|
}
|
|
21
21
|
const dataBytes = encoder.encode(data);
|
|
22
22
|
const secretBytes = encoder.encode(secret);
|
|
@@ -25,14 +25,21 @@ function encode(data, secret, type) {
|
|
|
25
25
|
const tokenBytes = new Uint8Array(dataLength);
|
|
26
26
|
for (let i = 0; i < dataLength; i++)
|
|
27
27
|
tokenBytes[i] = dataBytes[i] ^ secretBytes[i % secretLength];
|
|
28
|
-
return Array.from(tokenBytes)
|
|
28
|
+
return Array.from(tokenBytes)
|
|
29
|
+
.map((byte) => byte.toString(16).padStart(2, "0"))
|
|
30
|
+
.join("");
|
|
29
31
|
}
|
|
30
32
|
function decode(token, secret, type) {
|
|
31
33
|
if (type === 0) {
|
|
32
34
|
const applySecret = (code) => textToChars(secret).reduce((a, b) => a ^ b, code);
|
|
33
|
-
return token
|
|
35
|
+
return token
|
|
36
|
+
.match(/.{1,2}/g)
|
|
37
|
+
.map((hex) => parseInt(hex, 16))
|
|
38
|
+
.map(applySecret)
|
|
39
|
+
.map(stringFromCode)
|
|
40
|
+
.join("");
|
|
34
41
|
}
|
|
35
|
-
const tokenBytes = new Uint8Array(token.match(/.{1,2}/g).map(byte => parseInt(byte, 16)));
|
|
42
|
+
const tokenBytes = new Uint8Array(token.match(/.{1,2}/g).map((byte) => parseInt(byte, 16)));
|
|
36
43
|
const secretBytes = encoder.encode(secret);
|
|
37
44
|
const tokenLength = tokenBytes.length;
|
|
38
45
|
const secretLength = secretBytes.length;
|
|
@@ -49,7 +56,7 @@ export function sign(data, secret, { expiresIn = 0, sl = 32 } = {}) {
|
|
|
49
56
|
}
|
|
50
57
|
export function verify(token, secret) {
|
|
51
58
|
try {
|
|
52
|
-
const [dataStr, signature] = token.split(
|
|
59
|
+
const [dataStr, signature] = token.split(".");
|
|
53
60
|
const salt = decode(signature, secret, 0);
|
|
54
61
|
const { data, iat, exp } = JSON.parse(decode(dataStr, salt, 1));
|
|
55
62
|
if (!exp || Date.now() < iat + exp)
|
|
@@ -57,19 +64,19 @@ export function verify(token, secret) {
|
|
|
57
64
|
throw new Error();
|
|
58
65
|
}
|
|
59
66
|
catch (_a) {
|
|
60
|
-
throw new Error(
|
|
67
|
+
throw new Error("Invalid token or secret!");
|
|
61
68
|
}
|
|
62
69
|
}
|
|
63
70
|
export function encrypt(data, secret, { expiresIn = 0 } = {}, sjclOptions) {
|
|
64
71
|
const encryptedObj = JSON.parse(sjcl.encrypt(secret, JSON.stringify({ data, iat: Date.now(), exp: expiresIn }), sjclOptions));
|
|
65
72
|
const encryptedArr = Object.entries(encryptedObj).map(([key, value]) => `${key}:${value}`);
|
|
66
|
-
return encryptedArr.join(
|
|
73
|
+
return encryptedArr.join(".");
|
|
67
74
|
}
|
|
68
75
|
export function decrypt(token, secret) {
|
|
69
76
|
try {
|
|
70
|
-
const encryptedArr = token.split(
|
|
77
|
+
const encryptedArr = token.split(".");
|
|
71
78
|
const encryptedObj = encryptedArr.reduce((obj, str) => {
|
|
72
|
-
const [key, value] = str.split(
|
|
79
|
+
const [key, value] = str.split(":");
|
|
73
80
|
obj[key] = +value || value;
|
|
74
81
|
return obj;
|
|
75
82
|
}, {});
|
|
@@ -79,6 +86,6 @@ export function decrypt(token, secret) {
|
|
|
79
86
|
throw new Error();
|
|
80
87
|
}
|
|
81
88
|
catch (_a) {
|
|
82
|
-
throw new Error(
|
|
89
|
+
throw new Error("Invalid token or secret!");
|
|
83
90
|
}
|
|
84
91
|
}
|
package/package.json
CHANGED
|
@@ -1,45 +1,46 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "jssign",
|
|
3
|
-
"version": "0.3.
|
|
4
|
-
"description": "A token generator library to encode and decode data using a secret key",
|
|
5
|
-
"main": "dist/cjs/index.js",
|
|
6
|
-
"module": "dist/esm/index.js",
|
|
7
|
-
"
|
|
8
|
-
|
|
9
|
-
"
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
"
|
|
15
|
-
"
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
"
|
|
19
|
-
"
|
|
20
|
-
"
|
|
21
|
-
"
|
|
22
|
-
"
|
|
23
|
-
"
|
|
24
|
-
"
|
|
25
|
-
"
|
|
26
|
-
"
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
"
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
"
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
"
|
|
41
|
-
"
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
1
|
+
{
|
|
2
|
+
"name": "jssign",
|
|
3
|
+
"version": "0.3.5",
|
|
4
|
+
"description": "A token generator library to encode and decode data using a secret key",
|
|
5
|
+
"main": "dist/cjs/index.js",
|
|
6
|
+
"module": "dist/esm/index.js",
|
|
7
|
+
"types": "dist/esm/index.d.ts",
|
|
8
|
+
"scripts": {
|
|
9
|
+
"esm": "tsc",
|
|
10
|
+
"cjs": "tsc --module commonjs --outDir dist/cjs --declaration false",
|
|
11
|
+
"build": "bun i && bun esm && pnpm cjs"
|
|
12
|
+
},
|
|
13
|
+
"repository": {
|
|
14
|
+
"type": "git",
|
|
15
|
+
"url": "git+https://github.com/SahilAggarwal2004/jssign.git"
|
|
16
|
+
},
|
|
17
|
+
"keywords": [
|
|
18
|
+
"jssign",
|
|
19
|
+
"jss",
|
|
20
|
+
"javascript",
|
|
21
|
+
"sign",
|
|
22
|
+
"javascriptsign",
|
|
23
|
+
"token",
|
|
24
|
+
"encryption",
|
|
25
|
+
"decryption",
|
|
26
|
+
"jsonwebtoken",
|
|
27
|
+
"sjcl",
|
|
28
|
+
"typescript",
|
|
29
|
+
"js",
|
|
30
|
+
"encoder",
|
|
31
|
+
"decoder",
|
|
32
|
+
"jwt-alternative"
|
|
33
|
+
],
|
|
34
|
+
"author": "Sahil Aggarwal",
|
|
35
|
+
"license": "MIT",
|
|
36
|
+
"bugs": {
|
|
37
|
+
"url": "https://github.com/SahilAggarwal2004/jssign/issues"
|
|
38
|
+
},
|
|
39
|
+
"homepage": "https://github.com/SahilAggarwal2004/jssign#readme",
|
|
40
|
+
"dependencies": {
|
|
41
|
+
"sjcl": "^1.0.8"
|
|
42
|
+
},
|
|
43
|
+
"devDependencies": {
|
|
44
|
+
"@types/sjcl": "^1.0.34"
|
|
45
|
+
}
|
|
45
46
|
}
|