jssign 0.1.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/LICENSE +9 -0
- package/README.md +61 -0
- package/build/cjs/index.d.ts +10 -0
- package/build/cjs/index.js +68 -0
- package/build/esm/index.d.ts +10 -0
- package/build/esm/index.js +59 -0
- package/package.json +29 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023 Sahil Aggawal, <aggarwalsahil2004@gmail.com>
|
|
4
|
+
|
|
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
|
+
|
|
7
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
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.
|
package/README.md
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
# JSSign
|
|
2
|
+
A better, faster, lighter and more secure alternative to [jsonwebtoken](https://www.npmjs.com/package/jsonwebtoken)
|
|
3
|
+
## Features
|
|
4
|
+
- Encrypt data using a secret
|
|
5
|
+
- Decrypt a token with secret to retrive data back
|
|
6
|
+
## Installation
|
|
7
|
+
To install JSSign
|
|
8
|
+
```bash
|
|
9
|
+
# with npm:
|
|
10
|
+
npm install jssign --save
|
|
11
|
+
|
|
12
|
+
# with yarn:
|
|
13
|
+
yarn add jssign
|
|
14
|
+
```
|
|
15
|
+
## Usage
|
|
16
|
+
`JSSign` exports different functions for data encryption for different use cases:
|
|
17
|
+
### Faster Usage
|
|
18
|
+
For a faster (but less secure) encoding and decoding of data using a secret, `JSSign` exports the following functions:
|
|
19
|
+
- `sign(data, secret, options)`: returns encoded token
|
|
20
|
+
- `verify(token, secret)`: returns decoded data
|
|
21
|
+
```javascript
|
|
22
|
+
import { sign, verify } from 'jssign'
|
|
23
|
+
|
|
24
|
+
const secret = 'top-secret'
|
|
25
|
+
const token = sign({ foo: 'bar' }, secret, { sl: 16 }) // no expiration
|
|
26
|
+
const data = verify(token, secret)
|
|
27
|
+
|
|
28
|
+
console.log(data) // { foo: 'bar' }
|
|
29
|
+
```
|
|
30
|
+
`data` can be an object literal, buffer or string representing valid JSON.
|
|
31
|
+
|
|
32
|
+
`secret` can be a string
|
|
33
|
+
|
|
34
|
+
`options`:
|
|
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 `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
|
+
|
|
38
|
+
### More secure Usage
|
|
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
|
+
- `encrypt(data, secret, options)`: return encrypted token
|
|
41
|
+
- `decrypt(token, secret)`: returns decrypted data
|
|
42
|
+
```javascript
|
|
43
|
+
import { encrypt, decrypt } from 'jssign'
|
|
44
|
+
|
|
45
|
+
const secret = 'top-secret'
|
|
46
|
+
const token = encrypt({ id: 'confidential_data' }, secret, { expiresIn: 180000 }) // will expire after 30 minutes of token creation
|
|
47
|
+
const data = decrypt(token, secret)
|
|
48
|
+
|
|
49
|
+
console.log(data) // { id: 'confidential_data' }
|
|
50
|
+
```
|
|
51
|
+
`data` can be an object literal, buffer or string representing valid JSON.
|
|
52
|
+
|
|
53
|
+
`secret` can be a string
|
|
54
|
+
|
|
55
|
+
`options`:
|
|
56
|
+
- `expiresIn` can be a numeric value representing time in ms (no expiration by default).
|
|
57
|
+
## Used By
|
|
58
|
+
- [CloudBreeze](https://cloudbreeze.vercel.app/)
|
|
59
|
+
- [NewsDose](https://newsdoseweb.netlify.app/)
|
|
60
|
+
## Author
|
|
61
|
+
[Sahil Aggarwal](https://www.github.com/SahilAggarwal2004)
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
declare function sign(data: any, secret: string, { expiresIn, sl }?: {
|
|
2
|
+
expiresIn: number;
|
|
3
|
+
sl: number;
|
|
4
|
+
}): string;
|
|
5
|
+
declare function verify(token: string, secret: string): any;
|
|
6
|
+
declare function encrypt(data: any, secret: string, { expiresIn }?: {
|
|
7
|
+
expiresIn: number;
|
|
8
|
+
}): string;
|
|
9
|
+
declare function decrypt(token: string, secret: string): any;
|
|
10
|
+
export { sign, verify, encrypt, decrypt };
|
|
@@ -0,0 +1,68 @@
|
|
|
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 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', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '-', '/', '*', '~', '!', '@', '#', '$', '%', '^', '&'];
|
|
10
|
+
const randomNumber = (min = 0, max = Number.MAX_SAFE_INTEGER) => min + Math.floor(Math.random() * (max - min + 1));
|
|
11
|
+
const randomElement = (array) => array[randomNumber(0, array.length - 1)];
|
|
12
|
+
const textToChars = (text) => text.split('').map(c => c.charCodeAt(0));
|
|
13
|
+
const byteHex = (n) => ("0" + Number(n).toString(16)).slice(-2);
|
|
14
|
+
const stringFromCode = (code) => String.fromCharCode(code);
|
|
15
|
+
function genSalt(length) {
|
|
16
|
+
let salt = randomElement(characters);
|
|
17
|
+
for (let i = 1; i < length; i++)
|
|
18
|
+
salt += randomElement(characters);
|
|
19
|
+
return salt;
|
|
20
|
+
}
|
|
21
|
+
function encode(data, secret) {
|
|
22
|
+
const applySecret = (code) => textToChars(secret).reduce((a, b) => a ^ b, code);
|
|
23
|
+
return data.toString().split('').map(textToChars).map(applySecret).map(byteHex).join('');
|
|
24
|
+
}
|
|
25
|
+
function decode(token, secret) {
|
|
26
|
+
const applySecret = (code) => textToChars(secret).reduce((a, b) => a ^ b, code);
|
|
27
|
+
return token.match(/.{1,2}/g).map((hex) => parseInt(hex, 16)).map(applySecret).map(stringFromCode).join('');
|
|
28
|
+
}
|
|
29
|
+
function sign(data, secret, { expiresIn = 0, sl = 8 } = { expiresIn: 0, sl: 8 }) {
|
|
30
|
+
const salt = genSalt(sl);
|
|
31
|
+
const token = encode(JSON.stringify({ data, iat: Date.now(), exp: expiresIn }), secret + salt);
|
|
32
|
+
const signature = encode(salt, secret);
|
|
33
|
+
return `${token}.${signature}`;
|
|
34
|
+
}
|
|
35
|
+
exports.sign = sign;
|
|
36
|
+
function verify(token, secret) {
|
|
37
|
+
try {
|
|
38
|
+
const [dataStr, signature] = token.split('.');
|
|
39
|
+
const salt = decode(signature, secret);
|
|
40
|
+
const { data, iat, exp } = JSON.parse(decode(dataStr, secret + salt));
|
|
41
|
+
if (!exp || Date.now() < iat + exp)
|
|
42
|
+
return data;
|
|
43
|
+
throw new Error();
|
|
44
|
+
}
|
|
45
|
+
catch (_a) {
|
|
46
|
+
throw new Error('Invalid token or secret!');
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
exports.verify = verify;
|
|
50
|
+
function encrypt(data, secret, { expiresIn = 0 } = { expiresIn: 0 }) {
|
|
51
|
+
const { ct, iv, salt } = JSON.parse(sjcl_1.default.encrypt(secret, JSON.stringify({ data, iat: Date.now(), exp: expiresIn })));
|
|
52
|
+
return `${ct}.${iv}.${salt}`;
|
|
53
|
+
}
|
|
54
|
+
exports.encrypt = encrypt;
|
|
55
|
+
function decrypt(token, secret) {
|
|
56
|
+
try {
|
|
57
|
+
const [ct, iv, salt] = token.split('.');
|
|
58
|
+
token = JSON.stringify(Object.assign({ ct, iv, salt }, defaults));
|
|
59
|
+
const { data, iat, exp } = JSON.parse(sjcl_1.default.decrypt(secret, token));
|
|
60
|
+
if (!exp || Date.now() < iat + exp)
|
|
61
|
+
return data;
|
|
62
|
+
throw new Error();
|
|
63
|
+
}
|
|
64
|
+
catch (_a) {
|
|
65
|
+
throw new Error('Invalid token or secret!');
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
exports.decrypt = decrypt;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
declare function sign(data: any, secret: string, { expiresIn, sl }?: {
|
|
2
|
+
expiresIn: number;
|
|
3
|
+
sl: number;
|
|
4
|
+
}): string;
|
|
5
|
+
declare function verify(token: string, secret: string): any;
|
|
6
|
+
declare function encrypt(data: any, secret: string, { expiresIn }?: {
|
|
7
|
+
expiresIn: number;
|
|
8
|
+
}): string;
|
|
9
|
+
declare function decrypt(token: string, secret: string): any;
|
|
10
|
+
export { sign, verify, encrypt, decrypt };
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import sjcl from 'sjcl';
|
|
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', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '-', '/', '*', '~', '!', '@', '#', '$', '%', '^', '&'];
|
|
4
|
+
const randomNumber = (min = 0, max = Number.MAX_SAFE_INTEGER) => min + Math.floor(Math.random() * (max - min + 1));
|
|
5
|
+
const randomElement = (array) => array[randomNumber(0, array.length - 1)];
|
|
6
|
+
const textToChars = (text) => text.split('').map(c => c.charCodeAt(0));
|
|
7
|
+
const byteHex = (n) => ("0" + Number(n).toString(16)).slice(-2);
|
|
8
|
+
const stringFromCode = (code) => String.fromCharCode(code);
|
|
9
|
+
function genSalt(length) {
|
|
10
|
+
let salt = randomElement(characters);
|
|
11
|
+
for (let i = 1; i < length; i++)
|
|
12
|
+
salt += randomElement(characters);
|
|
13
|
+
return salt;
|
|
14
|
+
}
|
|
15
|
+
function encode(data, secret) {
|
|
16
|
+
const applySecret = (code) => textToChars(secret).reduce((a, b) => a ^ b, code);
|
|
17
|
+
return data.toString().split('').map(textToChars).map(applySecret).map(byteHex).join('');
|
|
18
|
+
}
|
|
19
|
+
function decode(token, secret) {
|
|
20
|
+
const applySecret = (code) => textToChars(secret).reduce((a, b) => a ^ b, code);
|
|
21
|
+
return token.match(/.{1,2}/g).map((hex) => parseInt(hex, 16)).map(applySecret).map(stringFromCode).join('');
|
|
22
|
+
}
|
|
23
|
+
function sign(data, secret, { expiresIn = 0, sl = 8 } = { expiresIn: 0, sl: 8 }) {
|
|
24
|
+
const salt = genSalt(sl);
|
|
25
|
+
const token = encode(JSON.stringify({ data, iat: Date.now(), exp: expiresIn }), secret + salt);
|
|
26
|
+
const signature = encode(salt, secret);
|
|
27
|
+
return `${token}.${signature}`;
|
|
28
|
+
}
|
|
29
|
+
function verify(token, secret) {
|
|
30
|
+
try {
|
|
31
|
+
const [dataStr, signature] = token.split('.');
|
|
32
|
+
const salt = decode(signature, secret);
|
|
33
|
+
const { data, iat, exp } = JSON.parse(decode(dataStr, secret + salt));
|
|
34
|
+
if (!exp || Date.now() < iat + exp)
|
|
35
|
+
return data;
|
|
36
|
+
throw new Error();
|
|
37
|
+
}
|
|
38
|
+
catch (_a) {
|
|
39
|
+
throw new Error('Invalid token or secret!');
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
function encrypt(data, secret, { expiresIn = 0 } = { expiresIn: 0 }) {
|
|
43
|
+
const { ct, iv, salt } = JSON.parse(sjcl.encrypt(secret, JSON.stringify({ data, iat: Date.now(), exp: expiresIn })));
|
|
44
|
+
return `${ct}.${iv}.${salt}`;
|
|
45
|
+
}
|
|
46
|
+
function decrypt(token, secret) {
|
|
47
|
+
try {
|
|
48
|
+
const [ct, iv, salt] = token.split('.');
|
|
49
|
+
token = JSON.stringify(Object.assign({ ct, iv, salt }, defaults));
|
|
50
|
+
const { data, iat, exp } = JSON.parse(sjcl.decrypt(secret, token));
|
|
51
|
+
if (!exp || Date.now() < iat + exp)
|
|
52
|
+
return data;
|
|
53
|
+
throw new Error();
|
|
54
|
+
}
|
|
55
|
+
catch (_a) {
|
|
56
|
+
throw new Error('Invalid token or secret!');
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
export { sign, verify, encrypt, decrypt };
|
package/package.json
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "jssign",
|
|
3
|
+
"version": "0.1.0",
|
|
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": ["jssign", "jss", "javascript", "sign", "javascriptsign", "token", "encryption", "decryption", "jsonwebtoken", "sjcl"],
|
|
17
|
+
"author": "Sahil Aggarwal",
|
|
18
|
+
"license": "MIT",
|
|
19
|
+
"bugs": {
|
|
20
|
+
"url": "https://github.com/SahilAggarwal2004/jssign/issues"
|
|
21
|
+
},
|
|
22
|
+
"homepage": "https://github.com/SahilAggarwal2004/jssign#readme",
|
|
23
|
+
"dependencies": {
|
|
24
|
+
"sjcl": "^1.0.8"
|
|
25
|
+
},
|
|
26
|
+
"devDependencies": {
|
|
27
|
+
"@types/sjcl": "^1.0.30"
|
|
28
|
+
}
|
|
29
|
+
}
|