jssign 0.2.7 → 0.3.1

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 CHANGED
@@ -38,12 +38,12 @@ console.log(data) // { foo: 'bar' }
38
38
  `secret` can be a string
39
39
 
40
40
  `options`:
41
- - `expiresIn` can be a numeric value representing time in ms (no expiration by default).
41
+ - `expiresIn` can be a numeric value representing time in ms (default value is `0` which represents no expiration).
42
42
  - `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.
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'
@@ -59,6 +59,8 @@ console.log(data) // { id: 'confidential_data' }
59
59
  `secret` can be a string
60
60
 
61
61
  `options`:
62
- - `expiresIn` can be a numeric value representing time in ms (no expiration by default).
62
+ - `expiresIn` can be a numeric value representing time in ms (default value is `0` which represents no expiration).
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 { ct, iv, salt } = JSON.parse(sjcl_1.default.encrypt(secret, JSON.stringify({ data, iat: Date.now(), exp: expiresIn })));
74
- return `${ct}.${iv}.${salt}`;
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 [ct, iv, salt] = token.split('.');
80
- token = JSON.stringify(Object.assign({ ct, iv, salt }, defaults));
81
- const { data, iat, exp } = JSON.parse(sjcl_1.default.decrypt(secret, token));
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();
@@ -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
- declare function sign(data: any, secret: string, { expiresIn, sl }?: SignOptions): string;
8
- declare function verify(token: string, secret: string): any;
9
- declare function encrypt(data: any, secret: string, { expiresIn }?: EncryptOptions): string;
10
- declare function decrypt(token: string, secret: string): any;
11
- export { sign, verify, encrypt, decrypt };
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 { ct, iv, salt } = JSON.parse(sjcl.encrypt(secret, JSON.stringify({ data, iat: Date.now(), exp: expiresIn })));
66
- return `${ct}.${iv}.${salt}`;
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 [ct, iv, salt] = token.split('.');
71
- token = JSON.stringify(Object.assign({ ct, iv, salt }, defaults));
72
- const { data, iat, exp } = JSON.parse(sjcl.decrypt(secret, token));
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.2.7",
3
+ "version": "0.3.1",
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.31"
38
+ "@types/sjcl": "^1.0.34"
39
39
  },
40
40
  "scripts": {
41
41
  "esm": "tsc",