jssign 0.1.0 → 0.2.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 +5 -5
- package/build/cjs/index.js +33 -11
- package/build/esm/index.js +33 -11
- package/package.json +13 -2
package/README.md
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
#
|
|
1
|
+
# jssign
|
|
2
2
|
A better, faster, lighter and more secure alternative to [jsonwebtoken](https://www.npmjs.com/package/jsonwebtoken)
|
|
3
3
|
## Features
|
|
4
4
|
- Encrypt data using a secret
|
|
5
5
|
- Decrypt a token with secret to retrive data back
|
|
6
6
|
## Installation
|
|
7
|
-
To install
|
|
7
|
+
To install jssign
|
|
8
8
|
```bash
|
|
9
9
|
# with npm:
|
|
10
10
|
npm install jssign --save
|
|
@@ -13,9 +13,9 @@ To install JSSign
|
|
|
13
13
|
yarn add jssign
|
|
14
14
|
```
|
|
15
15
|
## Usage
|
|
16
|
-
`
|
|
16
|
+
`jssign` exports different functions for data encryption for different use cases:
|
|
17
17
|
### Faster Usage
|
|
18
|
-
For a faster (but less secure) encoding and decoding of data using a secret, `
|
|
18
|
+
For a faster (but less secure) encoding and decoding of data using a secret, `jssign` exports the following functions:
|
|
19
19
|
- `sign(data, secret, options)`: returns encoded token
|
|
20
20
|
- `verify(token, secret)`: returns decoded data
|
|
21
21
|
```javascript
|
|
@@ -36,7 +36,7 @@ console.log(data) // { foo: 'bar' }
|
|
|
36
36
|
- `sl` can be a numberic value representing salt length (default value is `8`). 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
|
-
For a more secure (but slower) encryption and decryption of data using a secret, `
|
|
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:
|
|
40
40
|
- `encrypt(data, secret, options)`: return encrypted token
|
|
41
41
|
- `decrypt(token, secret)`: returns decrypted data
|
|
42
42
|
```javascript
|
package/build/cjs/index.js
CHANGED
|
@@ -7,7 +7,9 @@ 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
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', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '-', '/', '*', '~', '!', '@', '#', '$', '%', '^', '&'];
|
|
10
|
-
const
|
|
10
|
+
const encoder = new TextEncoder();
|
|
11
|
+
const decoder = new TextDecoder();
|
|
12
|
+
const randomNumber = (min, max) => min + Math.floor(Math.random() * (max - min + 1));
|
|
11
13
|
const randomElement = (array) => array[randomNumber(0, array.length - 1)];
|
|
12
14
|
const textToChars = (text) => text.split('').map(c => c.charCodeAt(0));
|
|
13
15
|
const byteHex = (n) => ("0" + Number(n).toString(16)).slice(-2);
|
|
@@ -18,26 +20,46 @@ function genSalt(length) {
|
|
|
18
20
|
salt += randomElement(characters);
|
|
19
21
|
return salt;
|
|
20
22
|
}
|
|
21
|
-
function encode(data, secret) {
|
|
22
|
-
|
|
23
|
-
|
|
23
|
+
function encode(data, secret, type) {
|
|
24
|
+
if (type === 0) {
|
|
25
|
+
const applySecret = (code) => textToChars(secret).reduce((a, b) => a ^ b, code);
|
|
26
|
+
return data.toString().split('').map(textToChars).map(applySecret).map(byteHex).join('');
|
|
27
|
+
}
|
|
28
|
+
const dataBytes = encoder.encode(data);
|
|
29
|
+
const secretBytes = encoder.encode(secret);
|
|
30
|
+
const dataLength = dataBytes.length;
|
|
31
|
+
const secretLength = secretBytes.length;
|
|
32
|
+
const tokenBytes = new Uint8Array(dataLength);
|
|
33
|
+
for (let i = 0; i < dataLength; i++)
|
|
34
|
+
tokenBytes[i] = dataBytes[i] ^ secretBytes[i % secretLength];
|
|
35
|
+
return Array.from(tokenBytes).map(byte => byte.toString(16).padStart(2, '0')).join('');
|
|
24
36
|
}
|
|
25
|
-
function decode(token, secret) {
|
|
26
|
-
|
|
27
|
-
|
|
37
|
+
function decode(token, secret, type) {
|
|
38
|
+
if (type === 0) {
|
|
39
|
+
const applySecret = (code) => textToChars(secret).reduce((a, b) => a ^ b, code);
|
|
40
|
+
return token.match(/.{1,2}/g).map((hex) => parseInt(hex, 16)).map(applySecret).map(stringFromCode).join('');
|
|
41
|
+
}
|
|
42
|
+
const tokenBytes = new Uint8Array(token.match(/.{1,2}/g).map(byte => parseInt(byte, 16)));
|
|
43
|
+
const secretBytes = encoder.encode(secret);
|
|
44
|
+
const tokenLength = tokenBytes.length;
|
|
45
|
+
const secretLength = secretBytes.length;
|
|
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);
|
|
28
50
|
}
|
|
29
51
|
function sign(data, secret, { expiresIn = 0, sl = 8 } = { expiresIn: 0, sl: 8 }) {
|
|
30
52
|
const salt = genSalt(sl);
|
|
31
|
-
const token = encode(JSON.stringify({ data, iat: Date.now(), exp: expiresIn }),
|
|
32
|
-
const signature = encode(salt, secret);
|
|
53
|
+
const token = encode(JSON.stringify({ data, iat: Date.now(), exp: expiresIn }), salt, 1);
|
|
54
|
+
const signature = encode(salt, secret, 0);
|
|
33
55
|
return `${token}.${signature}`;
|
|
34
56
|
}
|
|
35
57
|
exports.sign = sign;
|
|
36
58
|
function verify(token, secret) {
|
|
37
59
|
try {
|
|
38
60
|
const [dataStr, signature] = token.split('.');
|
|
39
|
-
const salt = decode(signature, secret);
|
|
40
|
-
const { data, iat, exp } = JSON.parse(decode(dataStr,
|
|
61
|
+
const salt = decode(signature, secret, 0);
|
|
62
|
+
const { data, iat, exp } = JSON.parse(decode(dataStr, salt, 1));
|
|
41
63
|
if (!exp || Date.now() < iat + exp)
|
|
42
64
|
return data;
|
|
43
65
|
throw new Error();
|
package/build/esm/index.js
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
import sjcl from 'sjcl';
|
|
2
2
|
const defaults = { v: 1, iter: 10000, ks: 128, ts: 64, mode: "ccm", adata: "", cipher: "aes" };
|
|
3
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', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '-', '/', '*', '~', '!', '@', '#', '$', '%', '^', '&'];
|
|
4
|
-
const
|
|
4
|
+
const encoder = new TextEncoder();
|
|
5
|
+
const decoder = new TextDecoder();
|
|
6
|
+
const randomNumber = (min, max) => min + Math.floor(Math.random() * (max - min + 1));
|
|
5
7
|
const randomElement = (array) => array[randomNumber(0, array.length - 1)];
|
|
6
8
|
const textToChars = (text) => text.split('').map(c => c.charCodeAt(0));
|
|
7
9
|
const byteHex = (n) => ("0" + Number(n).toString(16)).slice(-2);
|
|
@@ -12,25 +14,45 @@ function genSalt(length) {
|
|
|
12
14
|
salt += randomElement(characters);
|
|
13
15
|
return salt;
|
|
14
16
|
}
|
|
15
|
-
function encode(data, secret) {
|
|
16
|
-
|
|
17
|
-
|
|
17
|
+
function encode(data, secret, type) {
|
|
18
|
+
if (type === 0) {
|
|
19
|
+
const applySecret = (code) => textToChars(secret).reduce((a, b) => a ^ b, code);
|
|
20
|
+
return data.toString().split('').map(textToChars).map(applySecret).map(byteHex).join('');
|
|
21
|
+
}
|
|
22
|
+
const dataBytes = encoder.encode(data);
|
|
23
|
+
const secretBytes = encoder.encode(secret);
|
|
24
|
+
const dataLength = dataBytes.length;
|
|
25
|
+
const secretLength = secretBytes.length;
|
|
26
|
+
const tokenBytes = new Uint8Array(dataLength);
|
|
27
|
+
for (let i = 0; i < dataLength; i++)
|
|
28
|
+
tokenBytes[i] = dataBytes[i] ^ secretBytes[i % secretLength];
|
|
29
|
+
return Array.from(tokenBytes).map(byte => byte.toString(16).padStart(2, '0')).join('');
|
|
18
30
|
}
|
|
19
|
-
function decode(token, secret) {
|
|
20
|
-
|
|
21
|
-
|
|
31
|
+
function decode(token, secret, type) {
|
|
32
|
+
if (type === 0) {
|
|
33
|
+
const applySecret = (code) => textToChars(secret).reduce((a, b) => a ^ b, code);
|
|
34
|
+
return token.match(/.{1,2}/g).map((hex) => parseInt(hex, 16)).map(applySecret).map(stringFromCode).join('');
|
|
35
|
+
}
|
|
36
|
+
const tokenBytes = new Uint8Array(token.match(/.{1,2}/g).map(byte => parseInt(byte, 16)));
|
|
37
|
+
const secretBytes = encoder.encode(secret);
|
|
38
|
+
const tokenLength = tokenBytes.length;
|
|
39
|
+
const secretLength = secretBytes.length;
|
|
40
|
+
const decodedBytes = new Uint8Array(tokenLength);
|
|
41
|
+
for (let i = 0; i < tokenLength; i++)
|
|
42
|
+
decodedBytes[i] = tokenBytes[i] ^ secretBytes[i % secretLength];
|
|
43
|
+
return decoder.decode(decodedBytes);
|
|
22
44
|
}
|
|
23
45
|
function sign(data, secret, { expiresIn = 0, sl = 8 } = { expiresIn: 0, sl: 8 }) {
|
|
24
46
|
const salt = genSalt(sl);
|
|
25
|
-
const token = encode(JSON.stringify({ data, iat: Date.now(), exp: expiresIn }),
|
|
26
|
-
const signature = encode(salt, secret);
|
|
47
|
+
const token = encode(JSON.stringify({ data, iat: Date.now(), exp: expiresIn }), salt, 1);
|
|
48
|
+
const signature = encode(salt, secret, 0);
|
|
27
49
|
return `${token}.${signature}`;
|
|
28
50
|
}
|
|
29
51
|
function verify(token, secret) {
|
|
30
52
|
try {
|
|
31
53
|
const [dataStr, signature] = token.split('.');
|
|
32
|
-
const salt = decode(signature, secret);
|
|
33
|
-
const { data, iat, exp } = JSON.parse(decode(dataStr,
|
|
54
|
+
const salt = decode(signature, secret, 0);
|
|
55
|
+
const { data, iat, exp } = JSON.parse(decode(dataStr, salt, 1));
|
|
34
56
|
if (!exp || Date.now() < iat + exp)
|
|
35
57
|
return data;
|
|
36
58
|
throw new Error();
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "jssign",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "A token generator library to encode and decode data using a secret key",
|
|
5
5
|
"main": "./build/cjs/index.js",
|
|
6
6
|
"module": "./build/esm/index.js",
|
|
@@ -13,7 +13,18 @@
|
|
|
13
13
|
"type": "git",
|
|
14
14
|
"url": "git+https://github.com/SahilAggarwal2004/jssign.git"
|
|
15
15
|
},
|
|
16
|
-
"keywords": [
|
|
16
|
+
"keywords": [
|
|
17
|
+
"jssign",
|
|
18
|
+
"jss",
|
|
19
|
+
"javascript",
|
|
20
|
+
"sign",
|
|
21
|
+
"javascriptsign",
|
|
22
|
+
"token",
|
|
23
|
+
"encryption",
|
|
24
|
+
"decryption",
|
|
25
|
+
"jsonwebtoken",
|
|
26
|
+
"sjcl"
|
|
27
|
+
],
|
|
17
28
|
"author": "Sahil Aggarwal",
|
|
18
29
|
"license": "MIT",
|
|
19
30
|
"bugs": {
|