@lumjs/encode 2.4.1 → 2.6.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.
Files changed (49) hide show
  1. package/CHANGELOG.md +16 -1
  2. package/jsdoc.js +6 -0
  3. package/lib/base64.js +30 -17
  4. package/lib/hash.js +1 -1
  5. package/lib/hotp.js +3 -0
  6. package/lib/index.js +16 -0
  7. package/lib/polyfill.js +135 -0
  8. package/lib/sets/_inc.js +18 -0
  9. package/lib/sets/all.js +14 -0
  10. package/lib/sets/base.js +20 -0
  11. package/lib/sets/digest.js +16 -0
  12. package/lib/sets/otp.js +18 -0
  13. package/lib/sets/sign.js +20 -0
  14. package/lib/totp.js +3 -0
  15. package/lib/util.js +103 -84
  16. package/lum.build.js +24 -0
  17. package/package.json +73 -24
  18. package/tsconfig.json +12 -0
  19. package/types/auto/base32.d.ts +72 -0
  20. package/types/auto/base32.d.ts.map +1 -0
  21. package/types/auto/base64.d.ts +258 -0
  22. package/types/auto/base64.d.ts.map +1 -0
  23. package/types/auto/base91.d.ts +3 -0
  24. package/types/auto/base91.d.ts.map +1 -0
  25. package/types/auto/hash.d.ts +216 -0
  26. package/types/auto/hash.d.ts.map +1 -0
  27. package/types/auto/hmac.d.ts +35 -0
  28. package/types/auto/hmac.d.ts.map +1 -0
  29. package/types/auto/hotp.d.ts +42 -0
  30. package/types/auto/hotp.d.ts.map +1 -0
  31. package/types/auto/index.d.ts +2 -0
  32. package/types/auto/index.d.ts.map +1 -0
  33. package/types/auto/pem.d.ts +62 -0
  34. package/types/auto/pem.d.ts.map +1 -0
  35. package/types/auto/polyfill.d.ts +72 -0
  36. package/types/auto/polyfill.d.ts.map +1 -0
  37. package/types/auto/signature.d.ts +25 -0
  38. package/types/auto/signature.d.ts.map +1 -0
  39. package/types/auto/totp.d.ts +22 -0
  40. package/types/auto/totp.d.ts.map +1 -0
  41. package/types/auto/util.d.ts +26 -0
  42. package/types/auto/util.d.ts.map +1 -0
  43. package/types/sets/_inc.d.ts +6 -0
  44. package/types/sets/all.d.ts +2 -0
  45. package/types/sets/base.d.ts +6 -0
  46. package/types/sets/digest.d.ts +4 -0
  47. package/types/sets/otp.d.ts +5 -0
  48. package/types/sets/sign.d.ts +6 -0
  49. package/jsdoc.json +0 -33
package/lib/util.js CHANGED
@@ -6,6 +6,8 @@
6
6
 
7
7
  const { TypedArray } = require('@lumjs/core/types');
8
8
 
9
+ const UI8_FHEX = (typeof Uint8Array.fromHex === 'function');
10
+
9
11
  /**
10
12
  * Return the ASCII/Unicode number for a character.
11
13
  *
@@ -18,8 +20,7 @@ const { TypedArray } = require('@lumjs/core/types');
18
20
  * @param {string} string
19
21
  * @returns {number}
20
22
  */
21
- exports.ord = function (string)
22
- {
23
+ exports.ord = function (string) {
23
24
  // discuss at: https://locutus.io/php/ord/
24
25
  // original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
25
26
  // bugfixed by: Onno Marsman
@@ -32,12 +33,10 @@ exports.ord = function (string)
32
33
 
33
34
  var str = string + '',
34
35
  code = str.charCodeAt(0);
35
- if (0xD800 <= code && code <= 0xDBFF)
36
- {
36
+ if (0xD800 <= code && code <= 0xDBFF) {
37
37
  // High surrogate (could change last hex to 0xDB7F to treat high private surrogates as single characters)
38
38
  var hi = code;
39
- if (str.length === 1)
40
- {
39
+ if (str.length === 1) {
41
40
  // This is just a high surrogate with no following low surrogate, so we return its value;
42
41
  return code;
43
42
  // we could also throw an error as it is not a complete character, but someone may want to know
@@ -45,8 +44,7 @@ exports.ord = function (string)
45
44
  var low = str.charCodeAt(1);
46
45
  return ((hi - 0xD800) * 0x400) + (low - 0xDC00) + 0x10000;
47
46
  }
48
- if (0xDC00 <= code && code <= 0xDFFF)
49
- {
47
+ if (0xDC00 <= code && code <= 0xDFFF) {
50
48
  // Low surrogate
51
49
  // This is just a low surrogate with no preceding high surrogate, so we return its value;
52
50
  return code;
@@ -94,67 +92,56 @@ exports.ord = function (string)
94
92
  *
95
93
  * @returns {Array} An array of integers.
96
94
  */
97
- exports.numByteArray = function (numStr, options={})
98
- {
99
- if (typeof options === 'number')
100
- {
101
- options = {size: options};
95
+ exports.numByteArray = function (numStr, options = {}) {
96
+ if (typeof options === 'number') {
97
+ options = { size: options };
102
98
  }
103
99
 
104
- const numOpt = (name, defval, min, max) =>
105
- {
100
+ const numOpt = (name, defval, min, max) => {
106
101
  const optval = options[name];
107
- if (typeof optval !== 'number')
108
- { // Was not a number, skip it.
102
+ if (typeof optval !== 'number') { // Was not a number, skip it.
109
103
  return defval;
110
104
  }
111
- if (optval < min)
112
- { // Value was lower than minimum.
105
+ if (optval < min) { // Value was lower than minimum.
113
106
  throw new RangeError(`${name} value ${optval} is less than ${min}`);
114
107
  }
115
- if (max !== 0 && optval > max)
116
- { // Value was higher than maximum.
108
+ if (max !== 0 && optval > max) { // Value was higher than maximum.
117
109
  throw new RangeError(`${name} value ${optval} is more than ${max}`);
118
110
  }
119
111
 
120
112
  return optval;
121
113
  }
122
114
 
123
- const len = numOpt('size', 2, 1, 0);
115
+ const len = numOpt('size', 2, 1, 0);
124
116
  const base = numOpt('base', 16, 2, 36);
125
117
 
126
118
  let strLen = numStr.length;
127
119
  let remainder = strLen % len;
128
120
 
129
- if (options.strict && remainder !== 0)
130
- {
121
+ if (options.strict && remainder !== 0) {
131
122
  throw new RangeError(`string length ${strLen} is not divisible by ${len}`);
132
123
  }
133
- else if (options.pad && remainder !== 0)
134
- {
124
+ else if (options.pad && remainder !== 0) {
135
125
  const padSize = len - remainder;
136
- numStr = numStr.padStart(strLen+padSize, '0');
126
+ numStr = numStr.padStart(strLen + padSize, '0');
137
127
  strLen = numStr.length;
138
128
  remainder = strLen % len;
139
- if (remainder !== 0)
140
- { // Something is wrong in the universe...
141
- console.error({numStr, strLen, len, remainder, arguments});
129
+ if (remainder !== 0) { // Something is wrong in the universe...
130
+ console.error({ numStr, strLen, len, remainder, arguments });
142
131
  throw new Error("string has remainder after padding");
143
132
  }
144
133
  }
145
134
 
146
- const endOf = strLen - (len-1);
135
+ const endOf = strLen - (len - 1);
147
136
  const bytes = [];
148
137
 
149
- const getBytes = (a,b) => bytes.push(parseInt(numStr.substring(a, b), base));
138
+ const getBytes = (a, b) => bytes.push(parseInt(numStr.substring(a, b), base));
150
139
 
151
- for(let i=0; i < endOf; i+=len)
152
- {
153
- getBytes(i, i+len);
140
+ for (let i = 0; i < endOf; i += len) {
141
+ getBytes(i, i + len);
154
142
  }
155
143
 
156
- if (remainder !== 0)
157
- {
144
+ if (remainder !== 0) {
158
145
  getBytes(strLen - remainder, strLen);
159
146
  }
160
147
 
@@ -167,27 +154,26 @@ exports.numByteArray = function (numStr, options={})
167
154
  * @param {WordArray} wordArray
168
155
  * @returns {Uint8Array}
169
156
  */
170
- exports.wordArrayToUint8Array = function(wordArray)
171
- {
172
- const l = wordArray.sigBytes;
173
- const words = wordArray.words;
174
- const result = new Uint8Array(l);
175
- var i=0 /*dst*/, j=0 /*src*/;
176
- while(true) {
177
- // here i is a multiple of 4
178
- if (i==l)
179
- break;
180
- var w = words[j++];
181
- result[i++] = (w & 0xff000000) >>> 24;
182
- if (i==l)
183
- break;
184
- result[i++] = (w & 0x00ff0000) >>> 16;
185
- if (i==l)
186
- break;
187
- result[i++] = (w & 0x0000ff00) >>> 8;
188
- if (i==l)
189
- break;
190
- result[i++] = (w & 0x000000ff);
157
+ exports.wordArrayToUint8Array = function (wordArray) {
158
+ const l = wordArray.sigBytes;
159
+ const words = wordArray.words;
160
+ const result = new Uint8Array(l);
161
+ var i = 0 /*dst*/, j = 0 /*src*/;
162
+ while (true) {
163
+ // here i is a multiple of 4
164
+ if (i == l)
165
+ break;
166
+ var w = words[j++];
167
+ result[i++] = (w & 0xff000000) >>> 24;
168
+ if (i == l)
169
+ break;
170
+ result[i++] = (w & 0x00ff0000) >>> 16;
171
+ if (i == l)
172
+ break;
173
+ result[i++] = (w & 0x0000ff00) >>> 8;
174
+ if (i == l)
175
+ break;
176
+ result[i++] = (w & 0x000000ff);
191
177
  }
192
178
  return result;
193
179
  }
@@ -199,35 +185,70 @@ exports.wordArrayToUint8Array = function(wordArray)
199
185
  * and was borrowed from the `notp` package.
200
186
  *
201
187
  * @param {Integer} num
202
- * @return {Array} bytes
188
+ * @returns {Array} bytes
203
189
  */
204
- exports.intToBytes = function(num)
205
- {
206
- let bytes = [];
190
+ exports.intToBytes = function (num) {
191
+ let bytes = [];
207
192
 
208
- for(let i=7 ; i>=0 ; --i)
209
- {
210
- bytes[i] = num & (255);
211
- num = num >> 8;
212
- }
193
+ for (let i = 7; i >= 0; --i) {
194
+ bytes[i] = num & (255);
195
+ num = num >> 8;
196
+ }
213
197
 
214
- return bytes;
198
+ return bytes;
215
199
  }
216
200
 
217
201
  /**
218
- * Convert a hex value to a byte array.
202
+ * Convert a hex string to a byte array.
219
203
  *
220
- * Also taken from the `notp` package.
204
+ * Original version was also from the `notp` package,
205
+ * with some of my own enhancements added.
221
206
  *
222
- * @param {String} hex string of hex to convert to a byte array
223
- * @return {Array} bytes
207
+ * @param {string} hex - Hex string to convert to a byte array.
208
+ * @param {boolean} [uint8=false] Return a Uint8Array?
209
+ * @param {boolean} [native=true] Use Uint8Array.fromHex() if it exists?
210
+ *
211
+ * Obviously only applicable if `unit8` is true, this will use the native
212
+ * method if it exists. The only reason I could think of to disable this
213
+ * is if you are using this function as a polyfill.
214
+ *
215
+ * @returns {(number[]|Uint8Array)}
216
+ */
217
+ exports.hexToBytes = function (hex, uint8 = false, native = true) {
218
+
219
+ if (uint8 && native && UI8_FHEX) {
220
+ // A shortcut for modern JS runtimes.
221
+ return Uint8Array.fromHex(hex);
222
+ }
223
+
224
+ let bytes = [];
225
+ for (let c = 0, C = hex.length; c < C; c += 2) {
226
+ bytes.push(parseInt(hex.substring(c, c+2), 16));
227
+ }
228
+ return uint8 ? new Uint8Array(bytes) : bytes;
229
+ }
230
+
231
+ /**
232
+ * Converts a byte array to a hex string.
233
+ *
234
+ * @param {(Uint8Array|number[])} bytes - Byte array.
235
+ * @param {boolean} [native=true] Use bytes.toHex() if it exists?
236
+ *
237
+ * Like the same-named option in hexToBytes, the only reason I could
238
+ * think of to disable this is if you are using this as a polyfill
239
+ * for the `Uint8Array.prototype.toHex` method.
240
+ *
241
+ * @returns {string}
224
242
  */
225
- exports.hexToBytes = function(hex) {
226
- var bytes = [];
227
- for(var c = 0, C = hex.length; c < C; c += 2) {
228
- bytes.push(parseInt(hex.substr(c, 2), 16));
229
- }
230
- return bytes;
243
+ exports.bytesToHex = function (bytes, native = true) {
244
+
245
+ if (native && typeof bytes.toHex === 'function') {
246
+ return bytes.toHex();
247
+ }
248
+
249
+ return Array.from(bytes, function (byte) {
250
+ return ('0' + (byte & 0xFF).toString(16)).slice(-2);
251
+ }).join('');
231
252
  }
232
253
 
233
254
  /**
@@ -248,21 +269,19 @@ exports.hexToBytes = function(hex) {
248
269
  * @returns {TypedArray} Will be an instance of `typeClass`.
249
270
  * @throws {TypeError} If invalid arguments were passed.
250
271
  */
251
- function str2ta(str, typeClass=Uint8Array)
252
- {
272
+ function str2ta(str, typeClass = Uint8Array) {
253
273
  let valid = {
254
274
  str: (typeof str === 'string'),
255
275
  typeClass: (TypedArray.isPrototypeOf(typeClass)),
256
276
  }
257
277
  if (!valid.str || !valid.typeClass) {
258
- console.error('str2ab valid:', valid, 'values: ', {str, typeClass});
278
+ console.error('str2ab valid:', valid, 'values: ', { str, typeClass });
259
279
  throw new TypeError("invalid arguments");
260
280
  }
261
281
 
262
- let buf = new ArrayBuffer(str.length*typeClass.BYTES_PER_ELEMENT);
282
+ let buf = new ArrayBuffer(str.length * typeClass.BYTES_PER_ELEMENT);
263
283
  let bufView = new typeClass(buf);
264
- for (let i = 0, strLen = str.length; i < strLen; i++)
265
- {
284
+ for (let i = 0, strLen = str.length; i < strLen; i++) {
266
285
  bufView[i] = str.charCodeAt(i);
267
286
  }
268
287
 
package/lum.build.js ADDED
@@ -0,0 +1,24 @@
1
+ module.exports = function(rb)
2
+ {
3
+ rb.project
4
+ .add('index')
5
+ .types('./types/auto')
6
+ .add('base32')
7
+ .add('base64')
8
+ .add('base91')
9
+ .add('hash')
10
+ .add('hmac')
11
+ .add('hotp')
12
+ .add('polyfill')
13
+ .add('signature')
14
+ .add('totp')
15
+ .add('util')
16
+ .src('./lib/sets')
17
+ .types('./types/sets')
18
+ .add('all')
19
+ .add('base')
20
+ .add('digest')
21
+ .add('otp')
22
+ .add('sign')
23
+ .save();
24
+ }
package/package.json CHANGED
@@ -1,39 +1,88 @@
1
1
  {
2
2
  "name": "@lumjs/encode",
3
- "version": "2.4.1",
3
+ "version": "2.6.0",
4
4
  "main": "lib/index.js",
5
- "exports":
6
- {
5
+ "exports": {
7
6
  ".": "./lib/index.js",
8
- "./base32": "./lib/base32.js",
9
- "./base64": "./lib/base64.js",
10
- "./base91": "./lib/base91.js",
11
- "./hash": "./lib/hash.js",
12
- "./hmac": "./lib/hmac.js",
13
- "./hotp": "./lib/hotp.js",
14
- "./pem": "./lib/pem.js",
15
- "./signature": "./lib/signature.js",
16
- "./totp": "./lib/totp.js",
17
- "./util": "./lib/util.js",
7
+ "./base32": {
8
+ "types": "./types/auto/base32.d.ts",
9
+ "default": "./lib/base32.js"
10
+ },
11
+ "./base64": {
12
+ "types": "./types/auto/base64.d.ts",
13
+ "default": "./lib/base64.js"
14
+ },
15
+ "./base91": {
16
+ "types": "./types/auto/base91.d.ts",
17
+ "default": "./lib/base91.js"
18
+ },
19
+ "./hash": {
20
+ "types": "./types/auto/hash.d.ts",
21
+ "default": "./lib/hash.js"
22
+ },
23
+ "./hmac": {
24
+ "types": "./types/auto/hmac.d.ts",
25
+ "default": "./lib/hmac.js"
26
+ },
27
+ "./hotp": {
28
+ "types": "./types/auto/hotp.d.ts",
29
+ "default": "./lib/hotp.js"
30
+ },
31
+ "./polyfill": {
32
+ "types": "./types/auto/polyfill.d.ts",
33
+ "default": "./lib/polyfill.js"
34
+ },
35
+ "./signature": {
36
+ "types": "./types/auto/signature.d.ts",
37
+ "default": "./lib/signature.js"
38
+ },
39
+ "./totp": {
40
+ "types": "./types/auto/totp.d.ts",
41
+ "default": "./lib/totp.js"
42
+ },
43
+ "./util": {
44
+ "types": "./types/auto/util.d.ts",
45
+ "default": "./lib/util.js"
46
+ },
47
+ "./all": {
48
+ "types": "./types/sets/all.d.ts",
49
+ "default": "./lib/sets/all.js"
50
+ },
51
+ "./base": {
52
+ "types": "./types/sets/base.d.ts",
53
+ "default": "./lib/sets/base.js"
54
+ },
55
+ "./digest": {
56
+ "types": "./types/sets/digest.d.ts",
57
+ "default": "./lib/sets/digest.js"
58
+ },
59
+ "./otp": {
60
+ "types": "./types/sets/otp.d.ts",
61
+ "default": "./lib/sets/otp.js"
62
+ },
63
+ "./sign": {
64
+ "types": "./types/sets/sign.d.ts",
65
+ "default": "./lib/sets/sign.js"
66
+ },
18
67
  "./package.json": "./package.json"
19
68
  },
20
69
  "license": "MIT",
21
- "repository":
22
- {
70
+ "repository": {
23
71
  "type": "git",
24
72
  "url": "https://github.com/supernovus/lum.encode.js.git"
25
73
  },
26
- "dependencies":
27
- {
74
+ "dependencies": {
28
75
  "@lumjs/core": "^1.28.0"
29
76
  },
30
- "devDependencies":
31
- {
77
+ "devDependencies": {
78
+ "@lumjs/build": "^1.2.0",
32
79
  "@lumjs/tests": "^2.0.0"
33
80
  },
34
- "scripts":
35
- {
36
- "test": "node ./node_modules/@lumjs/tests/bin/lumtest.js",
37
- "build-docs": "jsdoc -c ./jsdoc.json"
81
+ "scripts": {
82
+ "build": "npm run build-types && npm run build-meta && npm run build-docs",
83
+ "build-docs": "jsdoc -c ./jsdoc.js",
84
+ "build-meta": "lum-build",
85
+ "build-types": "npx -p typescript tsc",
86
+ "test": "node ./node_modules/@lumjs/tests/bin/lumtest.js"
38
87
  }
39
- }
88
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,12 @@
1
+ {
2
+ // I would use ["lib/**/*"] except I have no idea how to get typescript
3
+ // to recognise 'sets' aliases, so I'm building those .d.ts files by hand.
4
+ "include": ["lib/*.js"],
5
+ "compilerOptions": {
6
+ "allowJs": true,
7
+ "declaration": true,
8
+ "emitDeclarationOnly": true,
9
+ "outDir": "types/auto",
10
+ "declarationMap": true
11
+ }
12
+ }
@@ -0,0 +1,72 @@
1
+ export namespace Base32Types {
2
+ export default Base32Types.rfc4648;
3
+ }
4
+ /**
5
+ * Encode a value into a Base32 string.
6
+ *
7
+ * @param {(ArrayBuffer|TypedArray|string)} data - Data to be encoded.
8
+ *
9
+ * If this is a string it will be converted into a Uint8Array using a
10
+ * TextEncoder instance.
11
+ *
12
+ * @param {(object|string|boolean)} [opts] Options.
13
+ *
14
+ * If this is a boolean it will be used as `opts.pad`.
15
+ *
16
+ * If this is a string or one of the `Base32Types` properties,
17
+ * it will be used as `opts.type`.
18
+ *
19
+ * @param {(string|Array)} [opts.type] Base32 variant to use.
20
+ *
21
+ * If this is a string it must be the name (uppercase or lowercase)
22
+ * of one of the properties in the `Base32Types` object.
23
+ *
24
+ * If it is an Array it is expected that it is one of the properties
25
+ * from the Base32Types, or a compatible type def where the first
26
+ * item in the array is a string consisting of exactly 32 unique characters.
27
+ *
28
+ * @param {boolean} [opts.pad=true] Add `=` padding characters?
29
+ *
30
+ * Base32 (like Base64) uses `=` as a padding character to ensure the length
31
+ * of the encoded strings are divisible by 8. As that is a part of
32
+ * the standard specification, this option defaults to true.
33
+ *
34
+ * If you explicitly set this to false the string won't have any padding
35
+ * added to it, regardless of its length.
36
+ *
37
+ * @returns {string}
38
+ * @alias module:@lumjs/encode/base32.encode
39
+ */
40
+ export function base32Encode(buf: any, opts?: (object | string | boolean)): string;
41
+ /**
42
+ * Decode a Base32-encoded string.
43
+ * @param {string} str - Base32 string.
44
+ * @param {(object|string)} [opts] Options.
45
+ *
46
+ * If this is a boolean it will be used as `opts.string`.
47
+ *
48
+ * If this is a string or one of the `Base32Types` properties,
49
+ * it will be used as `opts.type`.
50
+ *
51
+ * @param {(string|Array)} [opts.type] Base32 variant to use.
52
+ *
53
+ * If this is a string it must be the name (uppercase or lowercase)
54
+ * of one of the properties in the `Base32Types` object.
55
+ *
56
+ * If it is an Array it is expected that it is one of the properties
57
+ * from the Base32Types, or a compatible type def where the first
58
+ * item in the array is a string consisting of exactly 32 unique characters.
59
+ *
60
+ * @param {boolean} [opts.string=false] Return a UTF-8 string?
61
+ *
62
+ * If this is true then we will assume the original encoded value was a UTF-8
63
+ * string, and will decode it as such using a TextDecoder instance.
64
+ *
65
+ * If this is false (the default) we will return a Uint8Array.
66
+ *
67
+ * @returns {(Uint8Array|string)}
68
+ * @alias module:@lumjs/encode/base32.decode
69
+ */
70
+ export function base32Decode(str: string, opts?: (object | string)): (Uint8Array | string);
71
+ export { Base32Types as TYPES, base32Encode as encode, base32Decode as decode };
72
+ //# sourceMappingURL=base32.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"base32.d.ts","sourceRoot":"","sources":["../../lib/base32.js"],"names":[],"mappings":";;;AA0JA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAmCG;AACH,8CA5BW,CAAC,MAAM,GAAC,MAAM,GAAC,OAAO,CAAC,GAyBrB,MAAM,CAiClB;AAvHD;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AACH,kCA3BW,MAAM,SACN,CAAC,MAAM,GAAC,MAAM,CAAC,GAuBb,CAAC,UAAU,GAAC,MAAM,CAAC,CAyB/B"}