jssign 0.2.7 → 0.3.0
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 +3 -1
- package/dist/cjs/index.js +11 -7
- package/dist/esm/index.d.ts +6 -5
- package/dist/esm/index.js +14 -11
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -43,7 +43,7 @@ console.log(data) // { foo: 'bar' }
|
|
|
43
43
|
|
|
44
44
|
### More secure Usage
|
|
45
45
|
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:
|
|
46
|
-
- `encrypt(data, secret, options)`: return encrypted token
|
|
46
|
+
- `encrypt(data, secret, options, sjclOptions)`: return encrypted token
|
|
47
47
|
- `decrypt(token, secret)`: returns decrypted data
|
|
48
48
|
```javascript
|
|
49
49
|
import { encrypt, decrypt } from 'jssign'
|
|
@@ -60,5 +60,7 @@ console.log(data) // { id: 'confidential_data' }
|
|
|
60
60
|
|
|
61
61
|
`options`:
|
|
62
62
|
- `expiresIn` can be a numeric value representing time in ms (no expiration by default).
|
|
63
|
+
|
|
64
|
+
`sjclOptions` are the options taken by `sjcl.encrypt` method having type `SjclCipherEncryptParams`
|
|
63
65
|
## Author
|
|
64
66
|
[Sahil Aggarwal](https://www.github.com/SahilAggarwal2004)
|
package/dist/cjs/index.js
CHANGED
|
@@ -5,7 +5,6 @@ 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 defaults = { v: 1, iter: 10000, ks: 128, ts: 64, mode: "ccm", adata: "", cipher: "aes" };
|
|
9
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', '-', '=', '[', ']', ';', ',', '.', '/', '~', '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_', '+', '{', '}', ':', '<', '>', '?'];
|
|
10
9
|
const encoder = new TextEncoder();
|
|
11
10
|
const decoder = new TextDecoder();
|
|
@@ -69,16 +68,21 @@ function verify(token, secret) {
|
|
|
69
68
|
}
|
|
70
69
|
}
|
|
71
70
|
exports.verify = verify;
|
|
72
|
-
function encrypt(data, secret, { expiresIn = 0 } = {}) {
|
|
73
|
-
const
|
|
74
|
-
|
|
71
|
+
function encrypt(data, secret, { expiresIn = 0 } = {}, sjclOptions) {
|
|
72
|
+
const encryptedObj = JSON.parse(sjcl_1.default.encrypt(secret, JSON.stringify({ data, iat: Date.now(), exp: expiresIn }), sjclOptions));
|
|
73
|
+
const encryptedArr = Object.entries(encryptedObj).map(([key, value]) => `${key}:${value}`);
|
|
74
|
+
return encryptedArr.join('.');
|
|
75
75
|
}
|
|
76
76
|
exports.encrypt = encrypt;
|
|
77
77
|
function decrypt(token, secret) {
|
|
78
78
|
try {
|
|
79
|
-
const
|
|
80
|
-
|
|
81
|
-
|
|
79
|
+
const encryptedArr = token.split('.');
|
|
80
|
+
const encryptedObj = encryptedArr.reduce((obj, str) => {
|
|
81
|
+
const [key, value] = str.split(':');
|
|
82
|
+
obj[key] = +value || value;
|
|
83
|
+
return obj;
|
|
84
|
+
}, {});
|
|
85
|
+
const { data, iat, exp } = JSON.parse(sjcl_1.default.decrypt(secret, JSON.stringify(encryptedObj)));
|
|
82
86
|
if (!exp || Date.now() < iat + exp)
|
|
83
87
|
return data;
|
|
84
88
|
throw new Error();
|
package/dist/esm/index.d.ts
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
|
+
import { SjclCipherEncryptParams } from 'sjcl';
|
|
1
2
|
export type EncryptOptions = {
|
|
2
3
|
expiresIn?: number;
|
|
3
4
|
};
|
|
4
5
|
export type SignOptions = EncryptOptions & {
|
|
5
6
|
sl?: number;
|
|
6
7
|
};
|
|
7
|
-
|
|
8
|
-
declare function
|
|
9
|
-
declare function
|
|
10
|
-
declare function
|
|
11
|
-
export
|
|
8
|
+
export type { SjclCipherEncryptParams };
|
|
9
|
+
export declare function sign(data: any, secret: string, { expiresIn, sl }?: SignOptions): string;
|
|
10
|
+
export declare function verify(token: string, secret: string): any;
|
|
11
|
+
export declare function encrypt(data: any, secret: string, { expiresIn }?: EncryptOptions, sjclOptions?: SjclCipherEncryptParams): string;
|
|
12
|
+
export declare function decrypt(token: string, secret: string): any;
|
package/dist/esm/index.js
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import sjcl from 'sjcl';
|
|
2
|
-
const defaults = { v: 1, iter: 10000, ks: 128, ts: 64, mode: "ccm", adata: "", cipher: "aes" };
|
|
3
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', '-', '=', '[', ']', ';', ',', '.', '/', '~', '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_', '+', '{', '}', ':', '<', '>', '?'];
|
|
4
3
|
const encoder = new TextEncoder();
|
|
5
4
|
const decoder = new TextDecoder();
|
|
@@ -42,13 +41,13 @@ function decode(token, secret, type) {
|
|
|
42
41
|
decodedBytes[i] = tokenBytes[i] ^ secretBytes[i % secretLength];
|
|
43
42
|
return decoder.decode(decodedBytes);
|
|
44
43
|
}
|
|
45
|
-
function sign(data, secret, { expiresIn = 0, sl = 32 } = {}) {
|
|
44
|
+
export function sign(data, secret, { expiresIn = 0, sl = 32 } = {}) {
|
|
46
45
|
const salt = genSalt(sl);
|
|
47
46
|
const token = encode(JSON.stringify({ data, iat: Date.now(), exp: expiresIn }), salt, 1);
|
|
48
47
|
const signature = encode(salt, secret, 0);
|
|
49
48
|
return `${token}.${signature}`;
|
|
50
49
|
}
|
|
51
|
-
function verify(token, secret) {
|
|
50
|
+
export function verify(token, secret) {
|
|
52
51
|
try {
|
|
53
52
|
const [dataStr, signature] = token.split('.');
|
|
54
53
|
const salt = decode(signature, secret, 0);
|
|
@@ -61,15 +60,20 @@ function verify(token, secret) {
|
|
|
61
60
|
throw new Error('Invalid token or secret!');
|
|
62
61
|
}
|
|
63
62
|
}
|
|
64
|
-
function encrypt(data, secret, { expiresIn = 0 } = {}) {
|
|
65
|
-
const
|
|
66
|
-
|
|
63
|
+
export function encrypt(data, secret, { expiresIn = 0 } = {}, sjclOptions) {
|
|
64
|
+
const encryptedObj = JSON.parse(sjcl.encrypt(secret, JSON.stringify({ data, iat: Date.now(), exp: expiresIn }), sjclOptions));
|
|
65
|
+
const encryptedArr = Object.entries(encryptedObj).map(([key, value]) => `${key}:${value}`);
|
|
66
|
+
return encryptedArr.join('.');
|
|
67
67
|
}
|
|
68
|
-
function decrypt(token, secret) {
|
|
68
|
+
export function decrypt(token, secret) {
|
|
69
69
|
try {
|
|
70
|
-
const
|
|
71
|
-
|
|
72
|
-
|
|
70
|
+
const encryptedArr = token.split('.');
|
|
71
|
+
const encryptedObj = encryptedArr.reduce((obj, str) => {
|
|
72
|
+
const [key, value] = str.split(':');
|
|
73
|
+
obj[key] = +value || value;
|
|
74
|
+
return obj;
|
|
75
|
+
}, {});
|
|
76
|
+
const { data, iat, exp } = JSON.parse(sjcl.decrypt(secret, JSON.stringify(encryptedObj)));
|
|
73
77
|
if (!exp || Date.now() < iat + exp)
|
|
74
78
|
return data;
|
|
75
79
|
throw new Error();
|
|
@@ -78,4 +82,3 @@ function decrypt(token, secret) {
|
|
|
78
82
|
throw new Error('Invalid token or secret!');
|
|
79
83
|
}
|
|
80
84
|
}
|
|
81
|
-
export { sign, verify, encrypt, decrypt };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "jssign",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"description": "A token generator library to encode and decode data using a secret key",
|
|
5
5
|
"main": "dist/cjs/index.js",
|
|
6
6
|
"module": "dist/esm/index.js",
|
|
@@ -35,7 +35,7 @@
|
|
|
35
35
|
"sjcl": "^1.0.8"
|
|
36
36
|
},
|
|
37
37
|
"devDependencies": {
|
|
38
|
-
"@types/sjcl": "^1.0.
|
|
38
|
+
"@types/sjcl": "^1.0.34"
|
|
39
39
|
},
|
|
40
40
|
"scripts": {
|
|
41
41
|
"esm": "tsc",
|