jssign 0.3.6 → 0.3.8
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/LICENSE +2 -2
- package/README.md +23 -13
- package/dist/cjs/index.cjs +104 -0
- package/dist/esm/index.d.ts +9 -12
- package/dist/esm/index.js +87 -83
- package/dist/esm/types.d.ts +12 -0
- package/dist/esm/types.js +1 -0
- package/package.json +65 -45
- package/dist/cjs/index.js +0 -101
package/LICENSE
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
MIT License
|
|
2
2
|
|
|
3
|
-
Copyright (c)
|
|
3
|
+
Copyright (c) Sahil Aggawal, <aggarwalsahil2004@gmail.com>
|
|
4
4
|
|
|
5
5
|
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
6
6
|
|
|
7
7
|
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
8
8
|
|
|
9
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
9
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# jssign
|
|
2
2
|
|
|
3
|
-
A better, faster, lighter and more secure alternative to [jsonwebtoken](https://
|
|
3
|
+
A better, faster, lighter and more secure alternative to [`jsonwebtoken`](https://github.com/auth0/node-jsonwebtoken)
|
|
4
4
|
|
|
5
5
|
## Features
|
|
6
6
|
|
|
@@ -9,20 +9,20 @@ A better, faster, lighter and more secure alternative to [jsonwebtoken](https://
|
|
|
9
9
|
|
|
10
10
|
## Installation
|
|
11
11
|
|
|
12
|
-
|
|
12
|
+
Install `jssign` using your preferred package manager:
|
|
13
13
|
|
|
14
14
|
```bash
|
|
15
|
-
|
|
16
|
-
|
|
15
|
+
# with npm:
|
|
16
|
+
npm install jssign --save
|
|
17
17
|
|
|
18
|
-
|
|
19
|
-
|
|
18
|
+
# with yarn:
|
|
19
|
+
yarn add jssign
|
|
20
20
|
|
|
21
|
-
|
|
22
|
-
|
|
21
|
+
# with pnpm:
|
|
22
|
+
pnpm add jssign
|
|
23
23
|
|
|
24
|
-
|
|
25
|
-
|
|
24
|
+
# with bun:
|
|
25
|
+
bun add jssign
|
|
26
26
|
```
|
|
27
27
|
|
|
28
28
|
## Usage
|
|
@@ -80,8 +80,18 @@ console.log(data); // { id: 'confidential_data' }
|
|
|
80
80
|
|
|
81
81
|
- `expiresIn` can be a numeric value representing time in ms (default value is `0` which represents no expiration).
|
|
82
82
|
|
|
83
|
-
`sjclOptions` are the options taken by `sjcl.encrypt` method having type `
|
|
83
|
+
`sjclOptions` are the options taken by `sjcl.encrypt` method having type [`SjclOptions`](#sjcloptions)
|
|
84
84
|
|
|
85
|
-
##
|
|
85
|
+
## Types
|
|
86
86
|
|
|
87
|
-
|
|
87
|
+
### SjclOptions
|
|
88
|
+
|
|
89
|
+
```typescript
|
|
90
|
+
import type { SjclCipherEncryptParams } from "sjcl";
|
|
91
|
+
|
|
92
|
+
type SjclOptions = SjclCipherEncryptParams;
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
## License
|
|
96
|
+
|
|
97
|
+
This project is licensed under the [MIT License](LICENSE).
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var sjcl = require('sjcl');
|
|
4
|
+
|
|
5
|
+
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
|
6
|
+
|
|
7
|
+
var sjcl__default = /*#__PURE__*/_interopDefault(sjcl);
|
|
8
|
+
|
|
9
|
+
// src/index.ts
|
|
10
|
+
|
|
11
|
+
// src/constants.ts
|
|
12
|
+
var 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", "-", "=", "[", "]", ";", ",", ".", "/", "~", "!", "@", "#", "$", "%", "^", "&", "*", "(", ")", "_", "+", "{", "}", ":", "<", ">", "?"];
|
|
13
|
+
var pairRegex = /(.{1,2})/g;
|
|
14
|
+
|
|
15
|
+
// src/lib/utils.ts
|
|
16
|
+
var byteHex = (n) => ("0" + Number(n).toString(16)).slice(-2);
|
|
17
|
+
var randomNumber = (min, max) => min + Math.floor(Math.random() * (max - min + 1));
|
|
18
|
+
var randomElement = (array) => array[randomNumber(0, array.length - 1)];
|
|
19
|
+
function genSalt(length) {
|
|
20
|
+
let salt = randomElement(characters);
|
|
21
|
+
for (let i = 1; i < length; i++) salt += randomElement(characters);
|
|
22
|
+
return salt;
|
|
23
|
+
}
|
|
24
|
+
var stringFromCode = (code) => String.fromCharCode(code);
|
|
25
|
+
var textToChars = (text) => text.split("").map((c) => c.charCodeAt(0));
|
|
26
|
+
|
|
27
|
+
// src/lib/cipher.ts
|
|
28
|
+
var encoder = new TextEncoder();
|
|
29
|
+
var decoder = new TextDecoder();
|
|
30
|
+
function encode(data, secret, mode) {
|
|
31
|
+
if (mode === 0) {
|
|
32
|
+
const applySecret = (code) => textToChars(secret).reduce((a, b) => a ^ b, code);
|
|
33
|
+
return textToChars(data.toString()).map(applySecret).map(byteHex).join("");
|
|
34
|
+
}
|
|
35
|
+
const dataBytes = encoder.encode(data);
|
|
36
|
+
const secretBytes = encoder.encode(secret);
|
|
37
|
+
const dataLength = dataBytes.length;
|
|
38
|
+
const secretLength = secretBytes.length;
|
|
39
|
+
const tokenBytes = new Uint8Array(dataLength);
|
|
40
|
+
for (let i = 0; i < dataLength; i++) tokenBytes[i] = dataBytes[i] ^ secretBytes[i % secretLength];
|
|
41
|
+
return Array.from(tokenBytes).map((byte) => byte.toString(16).padStart(2, "0")).join("");
|
|
42
|
+
}
|
|
43
|
+
function decode(token, secret, mode) {
|
|
44
|
+
const hexPairs = token.match(pairRegex);
|
|
45
|
+
if (!hexPairs) return "";
|
|
46
|
+
if (mode === 0) {
|
|
47
|
+
const applySecret = (code) => textToChars(secret).reduce((a, b) => a ^ b, code);
|
|
48
|
+
return hexPairs.map((hex) => parseInt(hex, 16)).map(applySecret).map(stringFromCode).join("");
|
|
49
|
+
}
|
|
50
|
+
const tokenBytes = new Uint8Array(hexPairs.map((byte) => parseInt(byte, 16)));
|
|
51
|
+
const secretBytes = encoder.encode(secret);
|
|
52
|
+
const tokenLength = tokenBytes.length;
|
|
53
|
+
const secretLength = secretBytes.length;
|
|
54
|
+
const decodedBytes = new Uint8Array(tokenLength);
|
|
55
|
+
for (let i = 0; i < tokenLength; i++) decodedBytes[i] = tokenBytes[i] ^ secretBytes[i % secretLength];
|
|
56
|
+
return decoder.decode(decodedBytes);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// src/lib/token.ts
|
|
60
|
+
function validateData(token, secret, getData) {
|
|
61
|
+
try {
|
|
62
|
+
const { data, iat, exp } = JSON.parse(getData(token, secret));
|
|
63
|
+
if (exp && Date.now() > iat + exp) throw new Error("Expired token!");
|
|
64
|
+
return data;
|
|
65
|
+
} catch (error) {
|
|
66
|
+
throw new Error("Invalid token or secret!");
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// src/index.ts
|
|
71
|
+
function sign(data, secret, { expiresIn = 0, sl = 32 } = {}) {
|
|
72
|
+
const salt = genSalt(sl);
|
|
73
|
+
const token = encode(JSON.stringify({ data, iat: Date.now(), exp: expiresIn }), salt, 1);
|
|
74
|
+
const signature = encode(salt, secret, 0);
|
|
75
|
+
return `${token}.${signature}`;
|
|
76
|
+
}
|
|
77
|
+
function verify(token, secret) {
|
|
78
|
+
return validateData(token, secret, (token2, secret2) => {
|
|
79
|
+
const [dataStr, signature] = token2.split(".");
|
|
80
|
+
const salt = decode(signature, secret2, 0);
|
|
81
|
+
return decode(dataStr, salt, 1);
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
function encrypt(data, secret, { expiresIn = 0 } = {}, sjclOptions) {
|
|
85
|
+
const encryptedObj = JSON.parse(sjcl__default.default.encrypt(secret, JSON.stringify({ data, iat: Date.now(), exp: expiresIn }), sjclOptions));
|
|
86
|
+
const encryptedArr = Object.entries(encryptedObj).map(([key, value]) => `${key}:${value}`);
|
|
87
|
+
return encryptedArr.join(".");
|
|
88
|
+
}
|
|
89
|
+
function decrypt(token, secret) {
|
|
90
|
+
return validateData(token, secret, (token2, secret2) => {
|
|
91
|
+
const encryptedArr = token2.split(".");
|
|
92
|
+
const encryptedObj = encryptedArr.reduce((obj, str) => {
|
|
93
|
+
const [key, value] = str.split(":");
|
|
94
|
+
obj[key] = isNaN(+value) ? value : +value;
|
|
95
|
+
return obj;
|
|
96
|
+
}, {});
|
|
97
|
+
return sjcl__default.default.decrypt(secret2, JSON.stringify(encryptedObj));
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
exports.decrypt = decrypt;
|
|
102
|
+
exports.encrypt = encrypt;
|
|
103
|
+
exports.sign = sign;
|
|
104
|
+
exports.verify = verify;
|
package/dist/esm/index.d.ts
CHANGED
|
@@ -1,12 +1,9 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
};
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
export
|
|
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;
|
|
1
|
+
import { SignOptions, EncryptOptions, SjclOptions } from './types.js';
|
|
2
|
+
import 'sjcl';
|
|
3
|
+
|
|
4
|
+
declare function sign(data: unknown, secret: string, { expiresIn, sl }?: SignOptions): string;
|
|
5
|
+
declare function verify(token: string, secret: string): unknown;
|
|
6
|
+
declare function encrypt(data: unknown, secret: string, { expiresIn }?: EncryptOptions, sjclOptions?: SjclOptions): string;
|
|
7
|
+
declare function decrypt(token: string, secret: string): unknown;
|
|
8
|
+
|
|
9
|
+
export { decrypt, encrypt, sign, verify };
|
package/dist/esm/index.js
CHANGED
|
@@ -1,91 +1,95 @@
|
|
|
1
|
-
import sjcl from
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
1
|
+
import sjcl from 'sjcl';
|
|
2
|
+
|
|
3
|
+
// src/index.ts
|
|
4
|
+
|
|
5
|
+
// src/constants.ts
|
|
6
|
+
var 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", "-", "=", "[", "]", ";", ",", ".", "/", "~", "!", "@", "#", "$", "%", "^", "&", "*", "(", ")", "_", "+", "{", "}", ":", "<", ">", "?"];
|
|
7
|
+
var pairRegex = /(.{1,2})/g;
|
|
8
|
+
|
|
9
|
+
// src/lib/utils.ts
|
|
10
|
+
var byteHex = (n) => ("0" + Number(n).toString(16)).slice(-2);
|
|
11
|
+
var randomNumber = (min, max) => min + Math.floor(Math.random() * (max - min + 1));
|
|
12
|
+
var randomElement = (array) => array[randomNumber(0, array.length - 1)];
|
|
10
13
|
function genSalt(length) {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
return salt;
|
|
14
|
+
let salt = randomElement(characters);
|
|
15
|
+
for (let i = 1; i < length; i++) salt += randomElement(characters);
|
|
16
|
+
return salt;
|
|
15
17
|
}
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
const
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
18
|
+
var stringFromCode = (code) => String.fromCharCode(code);
|
|
19
|
+
var textToChars = (text) => text.split("").map((c) => c.charCodeAt(0));
|
|
20
|
+
|
|
21
|
+
// src/lib/cipher.ts
|
|
22
|
+
var encoder = new TextEncoder();
|
|
23
|
+
var decoder = new TextDecoder();
|
|
24
|
+
function encode(data, secret, mode) {
|
|
25
|
+
if (mode === 0) {
|
|
26
|
+
const applySecret = (code) => textToChars(secret).reduce((a, b) => a ^ b, code);
|
|
27
|
+
return textToChars(data.toString()).map(applySecret).map(byteHex).join("");
|
|
28
|
+
}
|
|
29
|
+
const dataBytes = encoder.encode(data);
|
|
30
|
+
const secretBytes = encoder.encode(secret);
|
|
31
|
+
const dataLength = dataBytes.length;
|
|
32
|
+
const secretLength = secretBytes.length;
|
|
33
|
+
const tokenBytes = new Uint8Array(dataLength);
|
|
34
|
+
for (let i = 0; i < dataLength; i++) tokenBytes[i] = dataBytes[i] ^ secretBytes[i % secretLength];
|
|
35
|
+
return Array.from(tokenBytes).map((byte) => byte.toString(16).padStart(2, "0")).join("");
|
|
31
36
|
}
|
|
32
|
-
function decode(token, secret,
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
const decodedBytes = new Uint8Array(tokenLength);
|
|
47
|
-
for (let i = 0; i < tokenLength; i++)
|
|
48
|
-
decodedBytes[i] = tokenBytes[i] ^ secretBytes[i % secretLength];
|
|
49
|
-
return decoder.decode(decodedBytes);
|
|
37
|
+
function decode(token, secret, mode) {
|
|
38
|
+
const hexPairs = token.match(pairRegex);
|
|
39
|
+
if (!hexPairs) return "";
|
|
40
|
+
if (mode === 0) {
|
|
41
|
+
const applySecret = (code) => textToChars(secret).reduce((a, b) => a ^ b, code);
|
|
42
|
+
return hexPairs.map((hex) => parseInt(hex, 16)).map(applySecret).map(stringFromCode).join("");
|
|
43
|
+
}
|
|
44
|
+
const tokenBytes = new Uint8Array(hexPairs.map((byte) => parseInt(byte, 16)));
|
|
45
|
+
const secretBytes = encoder.encode(secret);
|
|
46
|
+
const tokenLength = tokenBytes.length;
|
|
47
|
+
const secretLength = secretBytes.length;
|
|
48
|
+
const decodedBytes = new Uint8Array(tokenLength);
|
|
49
|
+
for (let i = 0; i < tokenLength; i++) decodedBytes[i] = tokenBytes[i] ^ secretBytes[i % secretLength];
|
|
50
|
+
return decoder.decode(decodedBytes);
|
|
50
51
|
}
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
52
|
+
|
|
53
|
+
// src/lib/token.ts
|
|
54
|
+
function validateData(token, secret, getData) {
|
|
55
|
+
try {
|
|
56
|
+
const { data, iat, exp } = JSON.parse(getData(token, secret));
|
|
57
|
+
if (exp && Date.now() > iat + exp) throw new Error("Expired token!");
|
|
58
|
+
return data;
|
|
59
|
+
} catch (error) {
|
|
60
|
+
throw new Error("Invalid token or secret!");
|
|
61
|
+
}
|
|
56
62
|
}
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
throw new Error();
|
|
65
|
-
}
|
|
66
|
-
catch (_a) {
|
|
67
|
-
throw new Error("Invalid token or secret!");
|
|
68
|
-
}
|
|
63
|
+
|
|
64
|
+
// src/index.ts
|
|
65
|
+
function sign(data, secret, { expiresIn = 0, sl = 32 } = {}) {
|
|
66
|
+
const salt = genSalt(sl);
|
|
67
|
+
const token = encode(JSON.stringify({ data, iat: Date.now(), exp: expiresIn }), salt, 1);
|
|
68
|
+
const signature = encode(salt, secret, 0);
|
|
69
|
+
return `${token}.${signature}`;
|
|
69
70
|
}
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
const
|
|
73
|
-
|
|
71
|
+
function verify(token, secret) {
|
|
72
|
+
return validateData(token, secret, (token2, secret2) => {
|
|
73
|
+
const [dataStr, signature] = token2.split(".");
|
|
74
|
+
const salt = decode(signature, secret2, 0);
|
|
75
|
+
return decode(dataStr, salt, 1);
|
|
76
|
+
});
|
|
74
77
|
}
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
const [key, value] = str.split(":");
|
|
80
|
-
obj[key] = +value || value;
|
|
81
|
-
return obj;
|
|
82
|
-
}, {});
|
|
83
|
-
const { data, iat, exp } = JSON.parse(sjcl.decrypt(secret, JSON.stringify(encryptedObj)));
|
|
84
|
-
if (!exp || Date.now() < iat + exp)
|
|
85
|
-
return data;
|
|
86
|
-
throw new Error();
|
|
87
|
-
}
|
|
88
|
-
catch (_a) {
|
|
89
|
-
throw new Error("Invalid token or secret!");
|
|
90
|
-
}
|
|
78
|
+
function encrypt(data, secret, { expiresIn = 0 } = {}, sjclOptions) {
|
|
79
|
+
const encryptedObj = JSON.parse(sjcl.encrypt(secret, JSON.stringify({ data, iat: Date.now(), exp: expiresIn }), sjclOptions));
|
|
80
|
+
const encryptedArr = Object.entries(encryptedObj).map(([key, value]) => `${key}:${value}`);
|
|
81
|
+
return encryptedArr.join(".");
|
|
91
82
|
}
|
|
83
|
+
function decrypt(token, secret) {
|
|
84
|
+
return validateData(token, secret, (token2, secret2) => {
|
|
85
|
+
const encryptedArr = token2.split(".");
|
|
86
|
+
const encryptedObj = encryptedArr.reduce((obj, str) => {
|
|
87
|
+
const [key, value] = str.split(":");
|
|
88
|
+
obj[key] = isNaN(+value) ? value : +value;
|
|
89
|
+
return obj;
|
|
90
|
+
}, {});
|
|
91
|
+
return sjcl.decrypt(secret2, JSON.stringify(encryptedObj));
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export { decrypt, encrypt, sign, verify };
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { SjclCipherEncryptParams } from 'sjcl';
|
|
2
|
+
|
|
3
|
+
type CipherMode = 0 | 1;
|
|
4
|
+
type EncryptOptions = {
|
|
5
|
+
expiresIn?: number;
|
|
6
|
+
};
|
|
7
|
+
type SignOptions = EncryptOptions & {
|
|
8
|
+
sl?: number;
|
|
9
|
+
};
|
|
10
|
+
type SjclOptions = SjclCipherEncryptParams;
|
|
11
|
+
|
|
12
|
+
export type { CipherMode, EncryptOptions, SignOptions, SjclOptions };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
package/package.json
CHANGED
|
@@ -1,46 +1,66 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "jssign",
|
|
3
|
-
"version": "0.3.
|
|
4
|
-
"description": "A token generator library to encode and decode data using a secret key",
|
|
5
|
-
"
|
|
6
|
-
"
|
|
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
|
-
"
|
|
45
|
-
|
|
1
|
+
{
|
|
2
|
+
"name": "jssign",
|
|
3
|
+
"version": "0.3.8",
|
|
4
|
+
"description": "A token generator library to encode and decode data using a secret key",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"author": "Sahil Aggarwal <aggarwalsahil2004@gmail.com>",
|
|
7
|
+
"homepage": "https://github.com/SahilAggarwal2004/jssign#readme",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "git+https://github.com/SahilAggarwal2004/jssign.git"
|
|
11
|
+
},
|
|
12
|
+
"bugs": {
|
|
13
|
+
"url": "https://github.com/SahilAggarwal2004/jssign/issues"
|
|
14
|
+
},
|
|
15
|
+
"type": "module",
|
|
16
|
+
"exports": {
|
|
17
|
+
".": {
|
|
18
|
+
"import": "./dist/esm/index.js",
|
|
19
|
+
"require": "./dist/cjs/index.cjs"
|
|
20
|
+
},
|
|
21
|
+
"./types": "./dist/esm/types.js"
|
|
22
|
+
},
|
|
23
|
+
"main": "dist/esm/index.js",
|
|
24
|
+
"files": [
|
|
25
|
+
"dist"
|
|
26
|
+
],
|
|
27
|
+
"sideEffects": false,
|
|
28
|
+
"types": "dist/esm/index.d.ts",
|
|
29
|
+
"dependencies": {
|
|
30
|
+
"sjcl": "^1.0.8"
|
|
31
|
+
},
|
|
32
|
+
"devDependencies": {
|
|
33
|
+
"@release-it/conventional-changelog": "^10.0.4",
|
|
34
|
+
"@types/sjcl": "^1.0.34",
|
|
35
|
+
"prettier-package-json": "^2.8.0",
|
|
36
|
+
"release-it": "^19.2.2",
|
|
37
|
+
"tsup": "^8.5.1",
|
|
38
|
+
"typescript": "^5.9.3"
|
|
39
|
+
},
|
|
40
|
+
"keywords": [
|
|
41
|
+
"decoder",
|
|
42
|
+
"decryption",
|
|
43
|
+
"encoder",
|
|
44
|
+
"encryption",
|
|
45
|
+
"javascript",
|
|
46
|
+
"javascriptsign",
|
|
47
|
+
"js",
|
|
48
|
+
"jsonwebtoken",
|
|
49
|
+
"jss",
|
|
50
|
+
"jssign",
|
|
51
|
+
"jwt-alternative",
|
|
52
|
+
"sign",
|
|
53
|
+
"sjcl",
|
|
54
|
+
"token",
|
|
55
|
+
"typescript"
|
|
56
|
+
],
|
|
57
|
+
"scripts": {
|
|
58
|
+
"build": "pnpm i && pnpm run compile",
|
|
59
|
+
"compile": "tsup",
|
|
60
|
+
"dev": "tsup --watch",
|
|
61
|
+
"dry-release": "release-it --ci --dry-run",
|
|
62
|
+
"prettier": "prettier-package-json --write package.json",
|
|
63
|
+
"pub": "pnpm login && pnpm publish",
|
|
64
|
+
"release": "release-it --ci"
|
|
65
|
+
}
|
|
46
66
|
}
|
package/dist/cjs/index.js
DELETED
|
@@ -1,101 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.decrypt = exports.encrypt = exports.verify = exports.sign = void 0;
|
|
7
|
-
const sjcl_1 = __importDefault(require("sjcl"));
|
|
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
|
-
const encoder = new TextEncoder();
|
|
10
|
-
const decoder = new TextDecoder();
|
|
11
|
-
const randomNumber = (min, max) => min + Math.floor(Math.random() * (max - min + 1));
|
|
12
|
-
const randomElement = (array) => array[randomNumber(0, array.length - 1)];
|
|
13
|
-
const textToChars = (text) => text.split("").map((c) => c.charCodeAt(0));
|
|
14
|
-
const byteHex = (n) => ("0" + Number(n).toString(16)).slice(-2);
|
|
15
|
-
const stringFromCode = (code) => String.fromCharCode(code);
|
|
16
|
-
function genSalt(length) {
|
|
17
|
-
let salt = randomElement(characters);
|
|
18
|
-
for (let i = 1; i < length; i++)
|
|
19
|
-
salt += randomElement(characters);
|
|
20
|
-
return salt;
|
|
21
|
-
}
|
|
22
|
-
function encode(data, secret, type) {
|
|
23
|
-
if (type === 0) {
|
|
24
|
-
const applySecret = (code) => textToChars(secret).reduce((a, b) => a ^ b, code);
|
|
25
|
-
return data.toString().split("").map(textToChars).map(applySecret).map(byteHex).join("");
|
|
26
|
-
}
|
|
27
|
-
const dataBytes = encoder.encode(data);
|
|
28
|
-
const secretBytes = encoder.encode(secret);
|
|
29
|
-
const dataLength = dataBytes.length;
|
|
30
|
-
const secretLength = secretBytes.length;
|
|
31
|
-
const tokenBytes = new Uint8Array(dataLength);
|
|
32
|
-
for (let i = 0; i < dataLength; i++)
|
|
33
|
-
tokenBytes[i] = dataBytes[i] ^ secretBytes[i % secretLength];
|
|
34
|
-
return Array.from(tokenBytes)
|
|
35
|
-
.map((byte) => byte.toString(16).padStart(2, "0"))
|
|
36
|
-
.join("");
|
|
37
|
-
}
|
|
38
|
-
function decode(token, secret, type) {
|
|
39
|
-
if (type === 0) {
|
|
40
|
-
const applySecret = (code) => textToChars(secret).reduce((a, b) => a ^ b, code);
|
|
41
|
-
return token
|
|
42
|
-
.match(/.{1,2}/g)
|
|
43
|
-
.map((hex) => parseInt(hex, 16))
|
|
44
|
-
.map(applySecret)
|
|
45
|
-
.map(stringFromCode)
|
|
46
|
-
.join("");
|
|
47
|
-
}
|
|
48
|
-
const tokenBytes = new Uint8Array(token.match(/.{1,2}/g).map((byte) => parseInt(byte, 16)));
|
|
49
|
-
const secretBytes = encoder.encode(secret);
|
|
50
|
-
const tokenLength = tokenBytes.length;
|
|
51
|
-
const secretLength = secretBytes.length;
|
|
52
|
-
const decodedBytes = new Uint8Array(tokenLength);
|
|
53
|
-
for (let i = 0; i < tokenLength; i++)
|
|
54
|
-
decodedBytes[i] = tokenBytes[i] ^ secretBytes[i % secretLength];
|
|
55
|
-
return decoder.decode(decodedBytes);
|
|
56
|
-
}
|
|
57
|
-
function sign(data, secret, { expiresIn = 0, sl = 32 } = {}) {
|
|
58
|
-
const salt = genSalt(sl);
|
|
59
|
-
const token = encode(JSON.stringify({ data, iat: Date.now(), exp: expiresIn }), salt, 1);
|
|
60
|
-
const signature = encode(salt, secret, 0);
|
|
61
|
-
return `${token}.${signature}`;
|
|
62
|
-
}
|
|
63
|
-
exports.sign = sign;
|
|
64
|
-
function verify(token, secret) {
|
|
65
|
-
try {
|
|
66
|
-
const [dataStr, signature] = token.split(".");
|
|
67
|
-
const salt = decode(signature, secret, 0);
|
|
68
|
-
const { data, iat, exp } = JSON.parse(decode(dataStr, salt, 1));
|
|
69
|
-
if (!exp || Date.now() < iat + exp)
|
|
70
|
-
return data;
|
|
71
|
-
throw new Error();
|
|
72
|
-
}
|
|
73
|
-
catch (_a) {
|
|
74
|
-
throw new Error("Invalid token or secret!");
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
exports.verify = verify;
|
|
78
|
-
function encrypt(data, secret, { expiresIn = 0 } = {}, sjclOptions) {
|
|
79
|
-
const encryptedObj = JSON.parse(sjcl_1.default.encrypt(secret, JSON.stringify({ data, iat: Date.now(), exp: expiresIn }), sjclOptions));
|
|
80
|
-
const encryptedArr = Object.entries(encryptedObj).map(([key, value]) => `${key}:${value}`);
|
|
81
|
-
return encryptedArr.join(".");
|
|
82
|
-
}
|
|
83
|
-
exports.encrypt = encrypt;
|
|
84
|
-
function decrypt(token, secret) {
|
|
85
|
-
try {
|
|
86
|
-
const encryptedArr = token.split(".");
|
|
87
|
-
const encryptedObj = encryptedArr.reduce((obj, str) => {
|
|
88
|
-
const [key, value] = str.split(":");
|
|
89
|
-
obj[key] = +value || value;
|
|
90
|
-
return obj;
|
|
91
|
-
}, {});
|
|
92
|
-
const { data, iat, exp } = JSON.parse(sjcl_1.default.decrypt(secret, JSON.stringify(encryptedObj)));
|
|
93
|
-
if (!exp || Date.now() < iat + exp)
|
|
94
|
-
return data;
|
|
95
|
-
throw new Error();
|
|
96
|
-
}
|
|
97
|
-
catch (_a) {
|
|
98
|
-
throw new Error("Invalid token or secret!");
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
exports.decrypt = decrypt;
|