jssign 0.2.0 → 0.2.2
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 +1 -1
- package/build/cjs/index.d.ts +9 -7
- package/build/cjs/index.js +3 -3
- package/build/esm/index.d.ts +9 -7
- package/build/esm/index.js +3 -3
- package/package.json +40 -40
package/README.md
CHANGED
|
@@ -33,7 +33,7 @@ console.log(data) // { foo: 'bar' }
|
|
|
33
33
|
|
|
34
34
|
`options`:
|
|
35
35
|
- `expiresIn` can be a numeric value representing time in ms (no expiration by default).
|
|
36
|
-
- `sl` can be a numberic value representing salt length (default value is `
|
|
36
|
+
- `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.
|
|
37
37
|
|
|
38
38
|
### More secure Usage
|
|
39
39
|
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:
|
package/build/cjs/index.d.ts
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
|
-
|
|
2
|
-
expiresIn
|
|
3
|
-
sl
|
|
4
|
-
}
|
|
1
|
+
export interface SignOptions {
|
|
2
|
+
expiresIn?: number;
|
|
3
|
+
sl?: number;
|
|
4
|
+
}
|
|
5
|
+
export interface EncryptOptions {
|
|
6
|
+
expiresIn?: number;
|
|
7
|
+
}
|
|
8
|
+
declare function sign(data: any, secret: string, { expiresIn, sl }?: SignOptions): string;
|
|
5
9
|
declare function verify(token: string, secret: string): any;
|
|
6
|
-
declare function encrypt(data: any, secret: string, { expiresIn }?:
|
|
7
|
-
expiresIn: number;
|
|
8
|
-
}): string;
|
|
10
|
+
declare function encrypt(data: any, secret: string, { expiresIn }?: EncryptOptions): string;
|
|
9
11
|
declare function decrypt(token: string, secret: string): any;
|
|
10
12
|
export { sign, verify, encrypt, decrypt };
|
package/build/cjs/index.js
CHANGED
|
@@ -6,7 +6,7 @@ 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
8
|
const defaults = { v: 1, iter: 10000, ks: 128, ts: 64, mode: "ccm", adata: "", cipher: "aes" };
|
|
9
|
-
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', '
|
|
9
|
+
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', '-', '=', '[', ']', ';', ',', '.', '/', '~', '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_', '+', '{', '}', ':', '<', '>', '?'];
|
|
10
10
|
const encoder = new TextEncoder();
|
|
11
11
|
const decoder = new TextDecoder();
|
|
12
12
|
const randomNumber = (min, max) => min + Math.floor(Math.random() * (max - min + 1));
|
|
@@ -48,7 +48,7 @@ function decode(token, secret, type) {
|
|
|
48
48
|
decodedBytes[i] = tokenBytes[i] ^ secretBytes[i % secretLength];
|
|
49
49
|
return decoder.decode(decodedBytes);
|
|
50
50
|
}
|
|
51
|
-
function sign(data, secret, { expiresIn = 0, sl =
|
|
51
|
+
function sign(data, secret, { expiresIn = 0, sl = 32 } = {}) {
|
|
52
52
|
const salt = genSalt(sl);
|
|
53
53
|
const token = encode(JSON.stringify({ data, iat: Date.now(), exp: expiresIn }), salt, 1);
|
|
54
54
|
const signature = encode(salt, secret, 0);
|
|
@@ -69,7 +69,7 @@ function verify(token, secret) {
|
|
|
69
69
|
}
|
|
70
70
|
}
|
|
71
71
|
exports.verify = verify;
|
|
72
|
-
function encrypt(data, secret, { expiresIn = 0 } = {
|
|
72
|
+
function encrypt(data, secret, { expiresIn = 0 } = {}) {
|
|
73
73
|
const { ct, iv, salt } = JSON.parse(sjcl_1.default.encrypt(secret, JSON.stringify({ data, iat: Date.now(), exp: expiresIn })));
|
|
74
74
|
return `${ct}.${iv}.${salt}`;
|
|
75
75
|
}
|
package/build/esm/index.d.ts
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
|
-
|
|
2
|
-
expiresIn
|
|
3
|
-
sl
|
|
4
|
-
}
|
|
1
|
+
export interface SignOptions {
|
|
2
|
+
expiresIn?: number;
|
|
3
|
+
sl?: number;
|
|
4
|
+
}
|
|
5
|
+
export interface EncryptOptions {
|
|
6
|
+
expiresIn?: number;
|
|
7
|
+
}
|
|
8
|
+
declare function sign(data: any, secret: string, { expiresIn, sl }?: SignOptions): string;
|
|
5
9
|
declare function verify(token: string, secret: string): any;
|
|
6
|
-
declare function encrypt(data: any, secret: string, { expiresIn }?:
|
|
7
|
-
expiresIn: number;
|
|
8
|
-
}): string;
|
|
10
|
+
declare function encrypt(data: any, secret: string, { expiresIn }?: EncryptOptions): string;
|
|
9
11
|
declare function decrypt(token: string, secret: string): any;
|
|
10
12
|
export { sign, verify, encrypt, decrypt };
|
package/build/esm/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import sjcl from 'sjcl';
|
|
2
2
|
const defaults = { v: 1, iter: 10000, ks: 128, ts: 64, mode: "ccm", adata: "", cipher: "aes" };
|
|
3
|
-
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', '
|
|
3
|
+
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', '-', '=', '[', ']', ';', ',', '.', '/', '~', '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_', '+', '{', '}', ':', '<', '>', '?'];
|
|
4
4
|
const encoder = new TextEncoder();
|
|
5
5
|
const decoder = new TextDecoder();
|
|
6
6
|
const randomNumber = (min, max) => min + Math.floor(Math.random() * (max - min + 1));
|
|
@@ -42,7 +42,7 @@ function decode(token, secret, type) {
|
|
|
42
42
|
decodedBytes[i] = tokenBytes[i] ^ secretBytes[i % secretLength];
|
|
43
43
|
return decoder.decode(decodedBytes);
|
|
44
44
|
}
|
|
45
|
-
function sign(data, secret, { expiresIn = 0, sl =
|
|
45
|
+
function sign(data, secret, { expiresIn = 0, sl = 32 } = {}) {
|
|
46
46
|
const salt = genSalt(sl);
|
|
47
47
|
const token = encode(JSON.stringify({ data, iat: Date.now(), exp: expiresIn }), salt, 1);
|
|
48
48
|
const signature = encode(salt, secret, 0);
|
|
@@ -61,7 +61,7 @@ function verify(token, secret) {
|
|
|
61
61
|
throw new Error('Invalid token or secret!');
|
|
62
62
|
}
|
|
63
63
|
}
|
|
64
|
-
function encrypt(data, secret, { expiresIn = 0 } = {
|
|
64
|
+
function encrypt(data, secret, { expiresIn = 0 } = {}) {
|
|
65
65
|
const { ct, iv, salt } = JSON.parse(sjcl.encrypt(secret, JSON.stringify({ data, iat: Date.now(), exp: expiresIn })));
|
|
66
66
|
return `${ct}.${iv}.${salt}`;
|
|
67
67
|
}
|
package/package.json
CHANGED
|
@@ -1,40 +1,40 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "jssign",
|
|
3
|
-
"version": "0.2.
|
|
4
|
-
"description": "A token generator library to encode and decode data using a secret key",
|
|
5
|
-
"main": "./build/cjs/index.js",
|
|
6
|
-
"module": "./build/esm/index.js",
|
|
7
|
-
"scripts": {
|
|
8
|
-
"esm": "tsc",
|
|
9
|
-
"cjs": "tsc --module commonjs --outDir build/cjs",
|
|
10
|
-
"build": "npm i && npm run esm && npm run cjs"
|
|
11
|
-
},
|
|
12
|
-
"repository": {
|
|
13
|
-
"type": "git",
|
|
14
|
-
"url": "git+https://github.com/SahilAggarwal2004/jssign.git"
|
|
15
|
-
},
|
|
16
|
-
"keywords": [
|
|
17
|
-
"jssign",
|
|
18
|
-
"jss",
|
|
19
|
-
"javascript",
|
|
20
|
-
"sign",
|
|
21
|
-
"javascriptsign",
|
|
22
|
-
"token",
|
|
23
|
-
"encryption",
|
|
24
|
-
"decryption",
|
|
25
|
-
"jsonwebtoken",
|
|
26
|
-
"sjcl"
|
|
27
|
-
],
|
|
28
|
-
"author": "Sahil Aggarwal",
|
|
29
|
-
"license": "MIT",
|
|
30
|
-
"bugs": {
|
|
31
|
-
"url": "https://github.com/SahilAggarwal2004/jssign/issues"
|
|
32
|
-
},
|
|
33
|
-
"homepage": "https://github.com/SahilAggarwal2004/jssign#readme",
|
|
34
|
-
"dependencies": {
|
|
35
|
-
"sjcl": "^1.0.8"
|
|
36
|
-
},
|
|
37
|
-
"devDependencies": {
|
|
38
|
-
"@types/sjcl": "^1.0.30"
|
|
39
|
-
}
|
|
40
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "jssign",
|
|
3
|
+
"version": "0.2.2",
|
|
4
|
+
"description": "A token generator library to encode and decode data using a secret key",
|
|
5
|
+
"main": "./build/cjs/index.js",
|
|
6
|
+
"module": "./build/esm/index.js",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"esm": "tsc",
|
|
9
|
+
"cjs": "tsc --module commonjs --outDir build/cjs",
|
|
10
|
+
"build": "npm i && npm run esm && npm run cjs"
|
|
11
|
+
},
|
|
12
|
+
"repository": {
|
|
13
|
+
"type": "git",
|
|
14
|
+
"url": "git+https://github.com/SahilAggarwal2004/jssign.git"
|
|
15
|
+
},
|
|
16
|
+
"keywords": [
|
|
17
|
+
"jssign",
|
|
18
|
+
"jss",
|
|
19
|
+
"javascript",
|
|
20
|
+
"sign",
|
|
21
|
+
"javascriptsign",
|
|
22
|
+
"token",
|
|
23
|
+
"encryption",
|
|
24
|
+
"decryption",
|
|
25
|
+
"jsonwebtoken",
|
|
26
|
+
"sjcl"
|
|
27
|
+
],
|
|
28
|
+
"author": "Sahil Aggarwal",
|
|
29
|
+
"license": "MIT",
|
|
30
|
+
"bugs": {
|
|
31
|
+
"url": "https://github.com/SahilAggarwal2004/jssign/issues"
|
|
32
|
+
},
|
|
33
|
+
"homepage": "https://github.com/SahilAggarwal2004/jssign#readme",
|
|
34
|
+
"dependencies": {
|
|
35
|
+
"sjcl": "^1.0.8"
|
|
36
|
+
},
|
|
37
|
+
"devDependencies": {
|
|
38
|
+
"@types/sjcl": "^1.0.30"
|
|
39
|
+
}
|
|
40
|
+
}
|