@loaders.gl/crypto 4.0.0-alpha.9 → 4.0.0-beta.2

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 (96) hide show
  1. package/dist/crypto-worker-node.js +1 -1
  2. package/dist/crypto-worker-node.js.map +2 -2
  3. package/dist/crypto-worker.js +1 -1
  4. package/dist/crypto-worker.js.map +2 -2
  5. package/dist/dist.min.js +1 -1
  6. package/dist/dist.min.js.map +2 -2
  7. package/dist/es5/index.js +24 -5
  8. package/dist/es5/index.js.map +1 -1
  9. package/dist/es5/lib/algorithms/md5-wasm.js +3 -3
  10. package/dist/es5/lib/algorithms/md5-wasm.js.map +1 -1
  11. package/dist/es5/lib/crc32-hash.js +11 -13
  12. package/dist/es5/lib/crc32-hash.js.map +1 -1
  13. package/dist/es5/lib/crc32c-hash.js +11 -13
  14. package/dist/es5/lib/crc32c-hash.js.map +1 -1
  15. package/dist/es5/lib/crypto-hash.js +12 -9
  16. package/dist/es5/lib/crypto-hash.js.map +1 -1
  17. package/dist/es5/lib/hash.js +2 -1
  18. package/dist/es5/lib/hash.js.map +1 -1
  19. package/dist/es5/lib/md5-hash.js +3 -3
  20. package/dist/es5/lib/md5-hash.js.map +1 -1
  21. package/dist/es5/lib/node-hash.js +20 -12
  22. package/dist/es5/lib/node-hash.js.map +1 -1
  23. package/dist/es5/lib/utils/base64-utils.js +41 -2
  24. package/dist/es5/lib/utils/base64-utils.js.map +1 -1
  25. package/dist/es5/lib/utils/digest-utils.js +45 -7
  26. package/dist/es5/lib/utils/digest-utils.js.map +1 -1
  27. package/dist/es5/workers/worker.js +6 -4
  28. package/dist/es5/workers/worker.js.map +1 -1
  29. package/dist/esm/index.js +3 -2
  30. package/dist/esm/index.js.map +1 -1
  31. package/dist/esm/lib/algorithms/md5-wasm.js +3 -3
  32. package/dist/esm/lib/algorithms/md5-wasm.js.map +1 -1
  33. package/dist/esm/lib/crc32-hash.js +24 -19
  34. package/dist/esm/lib/crc32-hash.js.map +1 -1
  35. package/dist/esm/lib/crc32c-hash.js +24 -19
  36. package/dist/esm/lib/crc32c-hash.js.map +1 -1
  37. package/dist/esm/lib/crypto-hash.js +23 -13
  38. package/dist/esm/lib/crypto-hash.js.map +1 -1
  39. package/dist/esm/lib/hash.js +19 -11
  40. package/dist/esm/lib/hash.js.map +1 -1
  41. package/dist/esm/lib/md5-hash.js +3 -3
  42. package/dist/esm/lib/md5-hash.js.map +1 -1
  43. package/dist/esm/lib/node-hash.js +29 -15
  44. package/dist/esm/lib/node-hash.js.map +1 -1
  45. package/dist/esm/lib/utils/base64-utils.js +39 -1
  46. package/dist/esm/lib/utils/base64-utils.js.map +1 -1
  47. package/dist/esm/lib/utils/digest-utils.js +39 -6
  48. package/dist/esm/lib/utils/digest-utils.js.map +1 -1
  49. package/dist/esm/workers/worker.js +5 -4
  50. package/dist/esm/workers/worker.js.map +1 -1
  51. package/dist/index.d.ts +2 -1
  52. package/dist/index.d.ts.map +1 -1
  53. package/dist/lib/crc32-hash.d.ts +3 -3
  54. package/dist/lib/crc32-hash.d.ts.map +1 -1
  55. package/dist/lib/crc32c-hash.d.ts +3 -3
  56. package/dist/lib/crc32c-hash.d.ts.map +1 -1
  57. package/dist/lib/crypto-hash.d.ts +2 -2
  58. package/dist/lib/crypto-hash.d.ts.map +1 -1
  59. package/dist/lib/hash.d.ts +2 -2
  60. package/dist/lib/hash.d.ts.map +1 -1
  61. package/dist/lib/md5-hash.d.ts +1 -1
  62. package/dist/lib/md5-hash.d.ts.map +1 -1
  63. package/dist/lib/node-hash.d.ts +2 -2
  64. package/dist/lib/node-hash.d.ts.map +1 -1
  65. package/dist/lib/utils/base64-utils.d.ts +8 -1
  66. package/dist/lib/utils/base64-utils.d.ts.map +1 -1
  67. package/dist/lib/utils/digest-utils.d.ts +6 -6
  68. package/dist/lib/utils/digest-utils.d.ts.map +1 -1
  69. package/package.json +4 -4
  70. package/src/index.ts +10 -7
  71. package/src/lib/algorithms/md5-wasm.ts +4 -3
  72. package/src/lib/crc32-hash.ts +10 -12
  73. package/src/lib/crc32c-hash.ts +10 -12
  74. package/src/lib/crypto-hash.ts +10 -5
  75. package/src/lib/hash.ts +4 -3
  76. package/src/lib/md5-hash.ts +3 -3
  77. package/src/lib/node-hash.ts +14 -6
  78. package/src/lib/utils/base64-utils.ts +97 -1
  79. package/src/lib/utils/digest-utils.ts +57 -8
  80. package/src/workers/worker.ts +4 -4
  81. package/dist/bundle.js +0 -5
  82. package/dist/index.js +0 -47
  83. package/dist/lib/algorithms/crc32.js +0 -30
  84. package/dist/lib/algorithms/crc32c.js +0 -38
  85. package/dist/lib/algorithms/md5-wasm.js +0 -435
  86. package/dist/lib/crc32-hash.js +0 -47
  87. package/dist/lib/crc32c-hash.js +0 -51
  88. package/dist/lib/crypto-hash.js +0 -61
  89. package/dist/lib/hash.js +0 -29
  90. package/dist/lib/md5-hash.js +0 -32
  91. package/dist/lib/node-hash.js +0 -42
  92. package/dist/lib/sha256-hash.js +0 -14
  93. package/dist/lib/utils/base64-utils.js +0 -67
  94. package/dist/lib/utils/digest-utils.js +0 -27
  95. package/dist/types.js +0 -2
  96. package/dist/workers/worker.js +0 -23
package/src/index.ts CHANGED
@@ -1,4 +1,8 @@
1
- // import type {WorkerObject} from '@loaders.gl/worker-utils';
1
+ // loaders.gl, MIT license
2
+
3
+ // __VERSION__ is injected by babel-plugin-version-inline
4
+ // @ts-ignore TS2304: Cannot find name '__VERSION__'.
5
+ const VERSION = typeof __VERSION__ !== 'undefined' ? __VERSION__ : 'latest';
2
6
 
3
7
  export {CRC32Hash} from './lib/crc32-hash';
4
8
  export {CRC32CHash} from './lib/crc32c-hash';
@@ -8,12 +12,6 @@ export {SHA256Hash} from './lib/sha256-hash';
8
12
  export {CryptoHash} from './lib/crypto-hash';
9
13
  export {NodeHash} from './lib/node-hash';
10
14
 
11
- export {hexToBase64 as _hexToBase64, toHex as _toHex} from './lib/utils/digest-utils';
12
-
13
- // __VERSION__ is injected by babel-plugin-version-inline
14
- // @ts-ignore TS2304: Cannot find name '__VERSION__'.
15
- const VERSION = typeof __VERSION__ !== 'undefined' ? __VERSION__ : 'latest';
16
-
17
15
  /**
18
16
  * Small, fast worker for CRC32, CRC32c and MD5 Hashes
19
17
  */
@@ -40,3 +38,8 @@ export const CryptoJSWorker = {
40
38
  cryptojs: {}
41
39
  }
42
40
  };
41
+
42
+ // EXPERIMENTAL
43
+
44
+ export {encodeNumber, encodeHex, encodeBase64} from './lib/utils/digest-utils';
45
+ export {asciiToBase64, base64ToAscii} from './lib/utils/base64-utils';
@@ -326,7 +326,7 @@ function makeMD5WA() {
326
326
  setX = exports.setX;
327
327
  memView = mView;
328
328
  digestbytes = crypt.wordsToBytes(md5WA(message));
329
- return options && options.asBytes ? digestbytes : crypt.bytesToHex(digestbytes);
329
+ return options && options.asBytes ? digestbytes : crypt.bytesconvertNumberToHex(digestbytes);
330
330
  };
331
331
  }
332
332
 
@@ -482,7 +482,8 @@ function makeMD5JS() {
482
482
 
483
483
  return function (message, options) {
484
484
  var digestbytes = crypt.wordsToBytes(md5JS(message, options)),
485
- result = options && options.asBytes ? digestbytes : crypt.bytesToHex(digestbytes);
485
+ result =
486
+ options && options.asBytes ? digestbytes : crypt.bytesconvertNumberToHex(digestbytes);
486
487
  return result;
487
488
  };
488
489
  }
@@ -534,7 +535,7 @@ function makeCrypt() {
534
535
  return bytes;
535
536
  },
536
537
 
537
- bytesToHex: function (bytes) {
538
+ bytesconvertNumberToHex: function (bytes) {
538
539
  for (var hex = [], i = 0; i < bytes.length; i++) {
539
540
  hex.push((bytes[i] >>> 4).toString(16));
540
541
  hex.push((bytes[i] & 0xf).toString(16));
@@ -1,7 +1,7 @@
1
1
  // CRC32
2
2
  import {Hash} from './hash';
3
3
  import CRC32 from './algorithms/crc32';
4
- import {toHex, hexToBase64} from './utils/digest-utils';
4
+ import {encodeNumber} from './utils/digest-utils';
5
5
 
6
6
  /**
7
7
  * Calculates CRC32 Cryptographic Hash
@@ -23,28 +23,26 @@ export class CRC32Hash extends Hash {
23
23
  * Atomic hash calculation
24
24
  * @returns base64 encoded hash
25
25
  */
26
- async hash(input: ArrayBuffer): Promise<string> {
27
- return this.hashSync(input);
26
+ async hash(input: ArrayBuffer, encoding: 'hex' | 'base64'): Promise<string> {
27
+ return this.hashSync(input, encoding);
28
28
  }
29
29
 
30
- hashSync(input: ArrayBuffer): string {
30
+ hashSync(input: ArrayBuffer, encoding: 'hex' | 'base64'): string {
31
31
  this._hash.update(input);
32
- const hashValue = this._hash.finalize();
33
- const hex = toHex(hashValue);
34
- const hash = hexToBase64(hex);
35
- return hash;
32
+ const digest = this._hash.finalize();
33
+ return encodeNumber(digest, encoding);
36
34
  }
37
35
 
38
36
  async *hashBatches(
39
- asyncIterator: AsyncIterable<ArrayBuffer> | Iterable<ArrayBuffer>
37
+ asyncIterator: AsyncIterable<ArrayBuffer> | Iterable<ArrayBuffer>,
38
+ encoding: 'hex' | 'base64' = 'base64'
40
39
  ): AsyncIterable<ArrayBuffer> {
41
40
  for await (const chunk of asyncIterator) {
42
41
  this._hash.update(chunk);
43
42
  yield chunk;
44
43
  }
45
- const hashValue = this._hash.finalize();
46
- const hex = toHex(hashValue);
47
- const hash = hexToBase64(hex);
44
+ const digest = this._hash.finalize();
45
+ const hash = encodeNumber(digest, encoding);
48
46
  this.options.crypto?.onEnd?.({hash});
49
47
  }
50
48
  }
@@ -1,7 +1,7 @@
1
1
  // CRC32c
2
2
  import {Hash} from './hash';
3
3
  import CRC32C from './algorithms/crc32c';
4
- import {toHex, hexToBase64} from './utils/digest-utils';
4
+ import {encodeNumber} from './utils/digest-utils';
5
5
 
6
6
  /**
7
7
  * A transform that calculates CRC32c Hash
@@ -26,30 +26,28 @@ export class CRC32CHash extends Hash {
26
26
  * Atomic hash calculation
27
27
  * @returns base64 encoded hash
28
28
  */
29
- async hash(input: ArrayBuffer): Promise<string> {
30
- return this.hashSync(input);
29
+ async hash(input: ArrayBuffer, encoding: 'hex' | 'base64'): Promise<string> {
30
+ return this.hashSync(input, encoding);
31
31
  }
32
32
 
33
- hashSync(input: ArrayBuffer): string {
33
+ hashSync(input: ArrayBuffer, encoding: 'hex' | 'base64'): string {
34
34
  this._hash.update(input);
35
- const hashValue = this._hash.finalize();
36
- const hex = toHex(hashValue);
37
- const hash = hexToBase64(hex);
38
- return hash;
35
+ const digest = this._hash.finalize();
36
+ return encodeNumber(digest, encoding);
39
37
  }
40
38
 
41
39
  // runInBatches inherited
42
40
 
43
41
  async *hashBatches(
44
- asyncIterator: AsyncIterable<ArrayBuffer> | Iterable<ArrayBuffer>
42
+ asyncIterator: AsyncIterable<ArrayBuffer> | Iterable<ArrayBuffer>,
43
+ encoding: 'hex' | 'base64' = 'base64'
45
44
  ): AsyncIterable<ArrayBuffer> {
46
45
  for await (const chunk of asyncIterator) {
47
46
  this._hash.update(chunk);
48
47
  yield chunk;
49
48
  }
50
- const hashValue = this._hash.finalize();
51
- const hex = toHex(hashValue);
52
- const hash = hexToBase64(hex);
49
+ const digest = this._hash.finalize();
50
+ const hash = encodeNumber(digest, encoding);
53
51
  this.options.crypto?.onEnd?.({hash});
54
52
  }
55
53
  }
@@ -53,17 +53,20 @@ export class CryptoHash extends Hash {
53
53
  * Atomic hash calculation
54
54
  * @returns base64 encoded hash
55
55
  */
56
- async hash(input: ArrayBuffer): Promise<string> {
56
+ async hash(input: ArrayBuffer, encoding: 'hex' | 'base64'): Promise<string> {
57
57
  await this.preload();
58
58
  // arrayBuffer is accepted, even though types and docs say no
59
59
  // https://stackoverflow.com/questions/25567468/how-to-decrypt-an-arraybuffer
60
60
  // @ts-expect-error
61
61
  const typedWordArray = CryptoJS.lib.WordArray.create(input);
62
- return this._hash.update(typedWordArray).finalize().toString(CryptoJS.enc.Base64);
62
+ // Map our encoding constant to Crypto library
63
+ const enc = encoding === 'base64' ? CryptoJS.enc.Base64 : CryptoJS.enc.Hex;
64
+ return this._hash.update(typedWordArray).finalize().toString(enc);
63
65
  }
64
66
 
65
67
  async *hashBatches(
66
- asyncIterator: AsyncIterable<ArrayBuffer> | Iterable<ArrayBuffer>
68
+ asyncIterator: AsyncIterable<ArrayBuffer> | Iterable<ArrayBuffer>,
69
+ encoding: 'hex' | 'base64' = 'base64'
67
70
  ): AsyncIterable<ArrayBuffer> {
68
71
  await this.preload();
69
72
  for await (const chunk of asyncIterator) {
@@ -74,7 +77,9 @@ export class CryptoHash extends Hash {
74
77
  this._hash.update(typedWordArray);
75
78
  yield chunk;
76
79
  }
77
- const hash = this._hash.finalize().toString(CryptoJS.enc.Base64);
78
- this.options?.crypto?.onEnd?.({hash});
80
+ // Map our encoding constant to Crypto library
81
+ const enc = encoding === 'base64' ? CryptoJS.enc.Base64 : CryptoJS.enc.Hex;
82
+ const digest = this._hash.finalize().toString(enc);
83
+ this.options?.crypto?.onEnd?.({hash: digest});
79
84
  }
80
85
  }
package/src/lib/hash.ts CHANGED
@@ -21,10 +21,11 @@ export abstract class Hash {
21
21
  return;
22
22
  }
23
23
 
24
- abstract hash(arrayBuffer: ArrayBuffer): Promise<string>;
24
+ abstract hash(arrayBuffer: ArrayBuffer, encoding: 'hex' | 'base64'): Promise<string>;
25
25
 
26
26
  async *hashBatches(
27
- asyncIterator: AsyncIterable<ArrayBuffer> | Iterable<ArrayBuffer>
27
+ asyncIterator: AsyncIterable<ArrayBuffer> | Iterable<ArrayBuffer>,
28
+ encoding: 'hex' | 'base64' = 'base64'
28
29
  ): AsyncIterable<ArrayBuffer> {
29
30
  const arrayBuffers: ArrayBuffer[] = [];
30
31
  for await (const arrayBuffer of asyncIterator) {
@@ -32,7 +33,7 @@ export abstract class Hash {
32
33
  yield arrayBuffer;
33
34
  }
34
35
  const output = await this.concatenate(arrayBuffers);
35
- const hash = await this.hash(output);
36
+ const hash = await this.hash(output, encoding);
36
37
  this.options.crypto?.onEnd?.({hash});
37
38
  }
38
39
 
@@ -1,7 +1,7 @@
1
1
  // Fork of https://github.com/briantbutton/md5-wasm under MIT license
2
2
  import {Hash} from './hash';
3
3
  import md5WASM from './algorithms/md5-wasm';
4
- import {hexToBase64} from './utils/digest-utils';
4
+ import {encodeHex} from './utils/digest-utils';
5
5
 
6
6
  /**
7
7
  * A transform that calculates MD5 hashes, passing data through
@@ -19,12 +19,12 @@ export class MD5Hash extends Hash {
19
19
  * Atomic hash calculation
20
20
  * @returns base64 encoded hash
21
21
  */
22
- async hash(input: ArrayBuffer): Promise<string> {
22
+ async hash(input: ArrayBuffer, encoding: 'hex' | 'base64'): Promise<string> {
23
23
  const md5Promise = new Promise<string>((resolve, reject) =>
24
24
  // @ts-expect-error
25
25
  md5WASM(input).then(resolve).catch(reject)
26
26
  );
27
27
  const hex = await md5Promise;
28
- return hexToBase64(hex);
28
+ return encodeHex(hex, encoding);
29
29
  }
30
30
  }
@@ -34,15 +34,21 @@ export class NodeHash extends Hash {
34
34
  * Atomic hash calculation
35
35
  * @returns base64 encoded hash
36
36
  */
37
- async hash(input: ArrayBuffer): Promise<string> {
37
+ async hash(input: ArrayBuffer, encoding: 'hex' | 'base64'): Promise<string> {
38
38
  await this.preload();
39
- const hash = createHash(this.options?.crypto?.algorithm?.toLowerCase());
40
- const inputArray = new Uint8Array(input);
41
- return hash.update(inputArray).digest('base64');
39
+ const algorithm = this.options?.crypto?.algorithm?.toLowerCase();
40
+ try {
41
+ const hash = createHash(algorithm);
42
+ const inputArray = new Uint8Array(input);
43
+ return hash.update(inputArray).digest('base64');
44
+ } catch (error) {
45
+ throw Error(`${algorithm} hash not available. ${error}`);
46
+ }
42
47
  }
43
48
 
44
49
  async *hashBatches(
45
- asyncIterator: AsyncIterable<ArrayBuffer> | Iterable<ArrayBuffer>
50
+ asyncIterator: AsyncIterable<ArrayBuffer> | Iterable<ArrayBuffer>,
51
+ encoding: 'hex' | 'base64' = 'base64'
46
52
  ): AsyncIterable<ArrayBuffer> {
47
53
  await this.preload();
48
54
  const hash = createHash(this.options?.crypto?.algorithm?.toLowerCase());
@@ -52,6 +58,8 @@ export class NodeHash extends Hash {
52
58
  hash.update(inputArray);
53
59
  yield chunk;
54
60
  }
55
- this.options?.crypto?.onEnd?.({hash: hash.digest('base64')});
61
+ // We can pass our encoding constant directly to Node.js digest as it already supports `hex` and `base64`
62
+ const digest = hash.digest(encoding);
63
+ this.options?.crypto?.onEnd?.({hash: digest});
56
64
  }
57
65
  }
@@ -2,7 +2,7 @@
2
2
  * `btoa()` polyfill as defined by the HTML and Infra specs, which mostly just references
3
3
  * RFC 4648.
4
4
  */
5
- export function toBase64(string: string): string | null {
5
+ export function asciiToBase64(string: string): string | null {
6
6
  // String conversion as required by Web IDL.
7
7
  string = `${string}`;
8
8
  // "The btoa() method must throw an "InvalidCharacterError" DOMException if
@@ -38,6 +38,102 @@ export function toBase64(string: string): string | null {
38
38
  return out;
39
39
  }
40
40
 
41
+ /**
42
+ * Implementation of atob() according to the HTML and Infra specs, except that
43
+ * instead of throwing INVALID_CHARACTER_ERR we return null.
44
+ *
45
+ * @note Forked from https://github.com/jsdom/abab under BSD 3 clause license
46
+ */
47
+ export function base64ToAscii(data: string): string {
48
+ // Web IDL requires DOMStrings to just be converted using ECMAScript
49
+ // ToString, which in our case amounts to using a template literal.
50
+ data = `${data}`;
51
+ // "Remove all ASCII whitespace from data."
52
+ data = data.replace(/[ \t\n\f\r]/g, '');
53
+ // "If data's code point length divides by 4 leaving no remainder, then: if data ends
54
+ // with one or two U+003D (=) code points, then remove them from data."
55
+ if (data.length % 4 === 0) {
56
+ data = data.replace(/[=]=?$/, '');
57
+ }
58
+ // "If data's code point length divides by 4 leaving a remainder of 1, then return
59
+ // failure."
60
+ //
61
+ // "If data contains a code point that is not one of
62
+ //
63
+ // U+002B (+)
64
+ // U+002F (/)
65
+ // ASCII alphanumeric
66
+ //
67
+ // then return failure."
68
+ if (data.length % 4 === 1 || /[^+/0-9A-Za-z]/.test(data)) {
69
+ return '';
70
+ }
71
+ // "Let output be an empty byte sequence."
72
+ let output = '';
73
+ // "Let buffer be an empty buffer that can have bits appended to it."
74
+ //
75
+ // We append bits via left-shift and or. accumulatedBits is used to track
76
+ // when we've gotten to 24 bits.
77
+ let buffer = 0;
78
+ let accumulatedBits = 0;
79
+ // "Let position be a position variable for data, initially pointing at the
80
+ // start of data."
81
+ //
82
+ // "While position does not point past the end of data:"
83
+ for (let i = 0; i < data.length; i++) {
84
+ // "Find the code point pointed to by position in the second column of
85
+ // Table 1: The Base 64 Alphabet of RFC 4648. Let n be the number given in
86
+ // the first cell of the same row.
87
+ //
88
+ // "Append to buffer the six bits corresponding to n, most significant bit
89
+ // first."
90
+ //
91
+ // atobLookup() implements the table from RFC 4648.
92
+ buffer <<= 6;
93
+ // @ts-expect-error
94
+ buffer |= atobLookup(data[i]);
95
+ accumulatedBits += 6;
96
+ // "If buffer has accumulated 24 bits, interpret them as three 8-bit
97
+ // big-endian numbers. Append three bytes with values equal to those
98
+ // numbers to output, in the same order, and then empty buffer."
99
+ if (accumulatedBits === 24) {
100
+ output += String.fromCharCode((buffer & 0xff0000) >> 16);
101
+ output += String.fromCharCode((buffer & 0xff00) >> 8);
102
+ output += String.fromCharCode(buffer & 0xff);
103
+ buffer = accumulatedBits = 0;
104
+ }
105
+ // "Advance position by 1."
106
+ }
107
+ // "If buffer is not empty, it contains either 12 or 18 bits. If it contains
108
+ // 12 bits, then discard the last four and interpret the remaining eight as
109
+ // an 8-bit big-endian number. If it contains 18 bits, then discard the last
110
+ // two and interpret the remaining 16 as two 8-bit big-endian numbers. Append
111
+ // the one or two bytes with values equal to those one or two numbers to
112
+ // output, in the same order."
113
+ if (accumulatedBits === 12) {
114
+ buffer >>= 4;
115
+ output += String.fromCharCode(buffer);
116
+ } else if (accumulatedBits === 18) {
117
+ buffer >>= 2;
118
+ output += String.fromCharCode((buffer & 0xff00) >> 8);
119
+ output += String.fromCharCode(buffer & 0xff);
120
+ }
121
+ // "Return output."
122
+ return output;
123
+ }
124
+ /**
125
+ * A lookup table for atob(), which converts an ASCII character to the
126
+ * corresponding six-bit number.
127
+ */
128
+
129
+ const keystr = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
130
+
131
+ function atobLookup(chr: string): number | undefined {
132
+ const index = keystr.indexOf(chr);
133
+ // Throw exception if character is not in the lookup string; should not be hit in tests
134
+ return index < 0 ? undefined : index;
135
+ }
136
+
41
137
  /**
42
138
  * Lookup table for btoa(), which converts a six-bit number into the
43
139
  * corresponding ASCII character.
@@ -1,17 +1,49 @@
1
- import {toBase64} from './base64-utils';
1
+ // loaders.gl, MIT license
2
+
3
+ import {asciiToBase64, base64ToAscii} from './base64-utils';
2
4
 
3
5
  /**
4
- *
6
+ * Encode a number (usually a digest from a hash function / cipher) as either hex or base64
7
+ * Suitable for hashes like CRC32 where the number of required bits fit withing a JavaScript number.
5
8
  */
6
- export function toHex(cipher: number): string {
7
- const hexString = cipher.toString(16);
8
- return hexString === '0' ? `0${hexString}` : hexString;
9
+ export function encodeNumber(number: number, encoding: 'hex' | 'base64'): string {
10
+ switch (encoding) {
11
+ case 'hex':
12
+ return convertNumberToHex(number);
13
+ case 'base64':
14
+ return convertHexToBase64(convertNumberToHex(number));
15
+ default:
16
+ throw new Error(encoding);
17
+ }
18
+ }
19
+
20
+ /** Encode a hex string, aeither return hex or re-encode as base64 */
21
+ export function encodeHex(hex: string, encoding: 'hex' | 'base64'): string {
22
+ switch (encoding) {
23
+ case 'hex':
24
+ return hex;
25
+ case 'base64':
26
+ return convertHexToBase64(hex);
27
+ default:
28
+ throw new Error(encoding);
29
+ }
30
+ }
31
+
32
+ export function encodeBase64(base64: string, encoding: 'hex' | 'base64'): string {
33
+ switch (encoding) {
34
+ case 'hex':
35
+ return convertBase64ToHex(base64);
36
+ case 'base64':
37
+ return base64;
38
+ default:
39
+ throw new Error(encoding);
40
+ }
9
41
  }
10
42
 
11
43
  /**
12
- * @see https://stackoverflow.com/questions/23190056/hex-to-base64-converter-for-javascript
44
+ * Convert a hexadecimal string to base64 encoded string representation
13
45
  */
14
- export function hexToBase64(hexstring: string): string {
46
+ function convertHexToBase64(hexstring: string): string {
15
47
  // Add leading zero to keep even count of digits
16
48
  // eg. f85d741 => 0f85d741
17
49
  if (hexstring.length % 2 !== 0) {
@@ -20,5 +52,22 @@ export function hexToBase64(hexstring: string): string {
20
52
  const matches = hexstring.match(/\w{2}/g) || [];
21
53
  const string = matches.map((a) => String.fromCharCode(parseInt(a, 16))).join('');
22
54
  // TODO - define how to handle failures
23
- return toBase64(string) || '';
55
+ return asciiToBase64(string) || '';
56
+ }
57
+
58
+ /**
59
+ * Convert a base64 encoded string to hexadecimal encoded string representation
60
+ */
61
+ function convertBase64ToHex(base64String: string): string {
62
+ return [...base64ToAscii(base64String)]
63
+ .map((c) => c.charCodeAt(0).toString(16).padStart(2, '0'))
64
+ .join('');
65
+ }
66
+
67
+ /**
68
+ * Converts a number to hex
69
+ */
70
+ function convertNumberToHex(cipher: number): string {
71
+ const hexString = cipher.toString(16);
72
+ return hexString === '0' ? `0${hexString}` : hexString;
24
73
  }
@@ -8,15 +8,15 @@ export {CRC32Hash, CRC32CHash};
8
8
 
9
9
  createWorker(async (data, options = {}) => {
10
10
  // @ts-ignore
11
- const {operation} = options;
11
+ const {operation, encoding = 'base64'} = options;
12
12
 
13
13
  switch (operation) {
14
14
  case 'crc32':
15
- return await new CRC32Hash(options).hash(data);
15
+ return await new CRC32Hash(options).hash(data, encoding);
16
16
  case 'crc32c':
17
- return await new CRC32CHash(options).hash(data);
17
+ return await new CRC32CHash(options).hash(data, encoding);
18
18
  case 'md5':
19
- return await new MD5Hash(options).hash(data);
19
+ return await new MD5Hash(options).hash(data, encoding);
20
20
  default:
21
21
  throw new Error(`invalid option: ${operation}`);
22
22
  }
package/dist/bundle.js DELETED
@@ -1,5 +0,0 @@
1
- "use strict";
2
- // @ts-nocheck
3
- const moduleExports = require('./index');
4
- globalThis.loaders = globalThis.loaders || {};
5
- module.exports = Object.assign(globalThis.loaders, moduleExports);
package/dist/index.js DELETED
@@ -1,47 +0,0 @@
1
- "use strict";
2
- // import type {WorkerObject} from '@loaders.gl/worker-utils';
3
- Object.defineProperty(exports, "__esModule", { value: true });
4
- exports.CryptoJSWorker = exports.CryptoWorker = exports._toHex = exports._hexToBase64 = exports.NodeHash = exports.CryptoHash = exports.SHA256Hash = exports.MD5Hash = exports.CRC32CHash = exports.CRC32Hash = void 0;
5
- var crc32_hash_1 = require("./lib/crc32-hash");
6
- Object.defineProperty(exports, "CRC32Hash", { enumerable: true, get: function () { return crc32_hash_1.CRC32Hash; } });
7
- var crc32c_hash_1 = require("./lib/crc32c-hash");
8
- Object.defineProperty(exports, "CRC32CHash", { enumerable: true, get: function () { return crc32c_hash_1.CRC32CHash; } });
9
- var md5_hash_1 = require("./lib/md5-hash");
10
- Object.defineProperty(exports, "MD5Hash", { enumerable: true, get: function () { return md5_hash_1.MD5Hash; } });
11
- var sha256_hash_1 = require("./lib/sha256-hash");
12
- Object.defineProperty(exports, "SHA256Hash", { enumerable: true, get: function () { return sha256_hash_1.SHA256Hash; } });
13
- var crypto_hash_1 = require("./lib/crypto-hash");
14
- Object.defineProperty(exports, "CryptoHash", { enumerable: true, get: function () { return crypto_hash_1.CryptoHash; } });
15
- var node_hash_1 = require("./lib/node-hash");
16
- Object.defineProperty(exports, "NodeHash", { enumerable: true, get: function () { return node_hash_1.NodeHash; } });
17
- var digest_utils_1 = require("./lib/utils/digest-utils");
18
- Object.defineProperty(exports, "_hexToBase64", { enumerable: true, get: function () { return digest_utils_1.hexToBase64; } });
19
- Object.defineProperty(exports, "_toHex", { enumerable: true, get: function () { return digest_utils_1.toHex; } });
20
- // __VERSION__ is injected by babel-plugin-version-inline
21
- // @ts-ignore TS2304: Cannot find name '__VERSION__'.
22
- const VERSION = typeof __VERSION__ !== 'undefined' ? __VERSION__ : 'latest';
23
- /**
24
- * Small, fast worker for CRC32, CRC32c and MD5 Hashes
25
- */
26
- exports.CryptoWorker = {
27
- id: 'crypto',
28
- name: 'CRC32, CRC32c and MD5 Hashes',
29
- module: 'crypto',
30
- version: VERSION,
31
- options: {
32
- crypto: {}
33
- }
34
- };
35
- /**
36
- * Large worker for full complement of Cryptographic Hashes
37
- * bundles the full crypto.js library
38
- */
39
- exports.CryptoJSWorker = {
40
- id: 'cryptojs',
41
- name: 'Cryptographic Hashes',
42
- module: 'crypto',
43
- version: VERSION,
44
- options: {
45
- cryptojs: {}
46
- }
47
- };
@@ -1,30 +0,0 @@
1
- "use strict";
2
- // Inspired by https://gist.github.com/wqli78/1330293/6d85cc967f32cccfcbad94ae7d088a3dcfc14bd9
3
- Object.defineProperty(exports, "__esModule", { value: true });
4
- /**
5
- * Calculates the CRC32 checksum of a string.
6
- */
7
- class CRC32 {
8
- constructor() {
9
- this.crc = ~0;
10
- }
11
- update(arrayBuffer) {
12
- const CRC32_TABLE = getCRC32Table();
13
- const byteArray = new Uint8Array(arrayBuffer);
14
- for (let i = 0; i < byteArray.byteLength; i++) {
15
- this.crc = (this.crc >>> 8) ^ CRC32_TABLE[(this.crc ^ byteArray[i]) & 0xff];
16
- // strings: crc = (crc >>> 8) ^ CRC32TAB[(crc ^ str.charCodeAt(i)) & 0xff];
17
- }
18
- return this;
19
- }
20
- finalize() {
21
- this.crc = Math.abs(this.crc ^ -1);
22
- return this.crc;
23
- }
24
- }
25
- exports.default = CRC32;
26
- // Note: Using a typed array here doubles the speed of the cipher
27
- const CRC32TAB = Uint32Array.of(0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f, 0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988, 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91, 0x1db71064, 0x6ab020f2, 0xf3b97148, 0x84be41de, 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7, 0x136c9856, 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9, 0xfa0f3d63, 0x8d080df5, 0x3b6e20c8, 0x4c69105e, 0xd56041e4, 0xa2677172, 0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b, 0x35b5a8fa, 0x42b2986c, 0xdbbbc9d6, 0xacbcf940, 0x32d86ce3, 0x45df5c75, 0xdcd60dcf, 0xabd13d59, 0x26d930ac, 0x51de003a, 0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423, 0xcfba9599, 0xb8bda50f, 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924, 0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d, 0x76dc4190, 0x01db7106, 0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f, 0x9fbfe4a5, 0xe8b8d433, 0x7807c9a2, 0x0f00f934, 0x9609a88e, 0xe10e9818, 0x7f6a0dbb, 0x086d3d2d, 0x91646c97, 0xe6635c01, 0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e, 0x6c0695ed, 0x1b01a57b, 0x8208f4c1, 0xf50fc457, 0x65b0d9c6, 0x12b7e950, 0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, 0xfbd44c65, 0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2, 0x4adfa541, 0x3dd895d7, 0xa4d1c46d, 0xd3d6f4fb, 0x4369e96a, 0x346ed9fc, 0xad678846, 0xda60b8d0, 0x44042d73, 0x33031de5, 0xaa0a4c5f, 0xdd0d7cc9, 0x5005713c, 0x270241aa, 0xbe0b1010, 0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f, 0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17, 0x2eb40d81, 0xb7bd5c3b, 0xc0ba6cad, 0xedb88320, 0x9abfb3b6, 0x03b6e20c, 0x74b1d29a, 0xead54739, 0x9dd277af, 0x04db2615, 0x73dc1683, 0xe3630b12, 0x94643b84, 0x0d6d6a3e, 0x7a6a5aa8, 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1, 0xf00f9344, 0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb, 0x196c3671, 0x6e6b06e7, 0xfed41b76, 0x89d32be0, 0x10da7a5a, 0x67dd4acc, 0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5, 0xd6d6a3e8, 0xa1d1937e, 0x38d8c2c4, 0x4fdff252, 0xd1bb67f1, 0xa6bc5767, 0x3fb506dd, 0x48b2364b, 0xd80d2bda, 0xaf0a1b4c, 0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55, 0x316e8eef, 0x4669be79, 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236, 0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f, 0xc5ba3bbe, 0xb2bd0b28, 0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31, 0x2cd99e8b, 0x5bdeae1d, 0x9b64c2b0, 0xec63f226, 0x756aa39c, 0x026d930a, 0x9c0906a9, 0xeb0e363f, 0x72076785, 0x05005713, 0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38, 0x92d28e9b, 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21, 0x86d3d2d4, 0xf1d4e242, 0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1, 0x18b74777, 0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c, 0x8f659eff, 0xf862ae69, 0x616bffd3, 0x166ccf45, 0xa00ae278, 0xd70dd2ee, 0x4e048354, 0x3903b3c2, 0xa7672661, 0xd06016f7, 0x4969474d, 0x3e6e77db, 0xaed16a4a, 0xd9d65adc, 0x40df0b66, 0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9, 0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605, 0xcdd70693, 0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94, 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d);
28
- function getCRC32Table() {
29
- return CRC32TAB;
30
- }
@@ -1,38 +0,0 @@
1
- "use strict";
2
- // From: https://gist.github.com/wqli78/1330293/6d85cc967f32cccfcbad94ae7d088a3dcfc14bd9
3
- // CRC32 doesn't appear to be supported natively by crypto-js
4
- Object.defineProperty(exports, "__esModule", { value: true });
5
- // import {toBuffer} from '@loaders.gl/loader-utils';
6
- // import {calculate as sse4calculate} from 'sse4_crc32';
7
- /**
8
- * Calculates the CRC32C checksum of a string.
9
- */
10
- class CRC32C {
11
- constructor(options = {}) {
12
- this.options = options;
13
- this.crc = ~0;
14
- }
15
- update(arrayBuffer) {
16
- const byteArray = new Uint8Array(arrayBuffer);
17
- const CRC32_TABLE = getCRC32Table();
18
- for (let i = 0; i < byteArray.length; i++) {
19
- this.crc = CRC32_TABLE[(this.crc ^ byteArray[i]) & 0xff] ^ (this.crc >>> 8);
20
- }
21
- return this;
22
- }
23
- finalize() {
24
- this.crc = (this.crc ^ -1) >>> 0;
25
- return this.crc;
26
- }
27
- }
28
- exports.default = CRC32C;
29
- /**
30
- * This code is a manual javascript translation of c code generated by
31
- * pycrc 0.7.1 (http://www.tty1.net/pycrc/). Command line used:
32
- * './pycrc.py --model=crc-32c --generate c --algorithm=table-driven'
33
- */
34
- // Note: Using a typed array here doubles the speed of the cipher
35
- const CRC32C_TABLE = Int32Array.of(0x00000000, 0xf26b8303, 0xe13b70f7, 0x1350f3f4, 0xc79a971f, 0x35f1141c, 0x26a1e7e8, 0xd4ca64eb, 0x8ad958cf, 0x78b2dbcc, 0x6be22838, 0x9989ab3b, 0x4d43cfd0, 0xbf284cd3, 0xac78bf27, 0x5e133c24, 0x105ec76f, 0xe235446c, 0xf165b798, 0x030e349b, 0xd7c45070, 0x25afd373, 0x36ff2087, 0xc494a384, 0x9a879fa0, 0x68ec1ca3, 0x7bbcef57, 0x89d76c54, 0x5d1d08bf, 0xaf768bbc, 0xbc267848, 0x4e4dfb4b, 0x20bd8ede, 0xd2d60ddd, 0xc186fe29, 0x33ed7d2a, 0xe72719c1, 0x154c9ac2, 0x061c6936, 0xf477ea35, 0xaa64d611, 0x580f5512, 0x4b5fa6e6, 0xb93425e5, 0x6dfe410e, 0x9f95c20d, 0x8cc531f9, 0x7eaeb2fa, 0x30e349b1, 0xc288cab2, 0xd1d83946, 0x23b3ba45, 0xf779deae, 0x05125dad, 0x1642ae59, 0xe4292d5a, 0xba3a117e, 0x4851927d, 0x5b016189, 0xa96ae28a, 0x7da08661, 0x8fcb0562, 0x9c9bf696, 0x6ef07595, 0x417b1dbc, 0xb3109ebf, 0xa0406d4b, 0x522bee48, 0x86e18aa3, 0x748a09a0, 0x67dafa54, 0x95b17957, 0xcba24573, 0x39c9c670, 0x2a993584, 0xd8f2b687, 0x0c38d26c, 0xfe53516f, 0xed03a29b, 0x1f682198, 0x5125dad3, 0xa34e59d0, 0xb01eaa24, 0x42752927, 0x96bf4dcc, 0x64d4cecf, 0x77843d3b, 0x85efbe38, 0xdbfc821c, 0x2997011f, 0x3ac7f2eb, 0xc8ac71e8, 0x1c661503, 0xee0d9600, 0xfd5d65f4, 0x0f36e6f7, 0x61c69362, 0x93ad1061, 0x80fde395, 0x72966096, 0xa65c047d, 0x5437877e, 0x4767748a, 0xb50cf789, 0xeb1fcbad, 0x197448ae, 0x0a24bb5a, 0xf84f3859, 0x2c855cb2, 0xdeeedfb1, 0xcdbe2c45, 0x3fd5af46, 0x7198540d, 0x83f3d70e, 0x90a324fa, 0x62c8a7f9, 0xb602c312, 0x44694011, 0x5739b3e5, 0xa55230e6, 0xfb410cc2, 0x092a8fc1, 0x1a7a7c35, 0xe811ff36, 0x3cdb9bdd, 0xceb018de, 0xdde0eb2a, 0x2f8b6829, 0x82f63b78, 0x709db87b, 0x63cd4b8f, 0x91a6c88c, 0x456cac67, 0xb7072f64, 0xa457dc90, 0x563c5f93, 0x082f63b7, 0xfa44e0b4, 0xe9141340, 0x1b7f9043, 0xcfb5f4a8, 0x3dde77ab, 0x2e8e845f, 0xdce5075c, 0x92a8fc17, 0x60c37f14, 0x73938ce0, 0x81f80fe3, 0x55326b08, 0xa759e80b, 0xb4091bff, 0x466298fc, 0x1871a4d8, 0xea1a27db, 0xf94ad42f, 0x0b21572c, 0xdfeb33c7, 0x2d80b0c4, 0x3ed04330, 0xccbbc033, 0xa24bb5a6, 0x502036a5, 0x4370c551, 0xb11b4652, 0x65d122b9, 0x97baa1ba, 0x84ea524e, 0x7681d14d, 0x2892ed69, 0xdaf96e6a, 0xc9a99d9e, 0x3bc21e9d, 0xef087a76, 0x1d63f975, 0x0e330a81, 0xfc588982, 0xb21572c9, 0x407ef1ca, 0x532e023e, 0xa145813d, 0x758fe5d6, 0x87e466d5, 0x94b49521, 0x66df1622, 0x38cc2a06, 0xcaa7a905, 0xd9f75af1, 0x2b9cd9f2, 0xff56bd19, 0x0d3d3e1a, 0x1e6dcdee, 0xec064eed, 0xc38d26c4, 0x31e6a5c7, 0x22b65633, 0xd0ddd530, 0x0417b1db, 0xf67c32d8, 0xe52cc12c, 0x1747422f, 0x49547e0b, 0xbb3ffd08, 0xa86f0efc, 0x5a048dff, 0x8ecee914, 0x7ca56a17, 0x6ff599e3, 0x9d9e1ae0, 0xd3d3e1ab, 0x21b862a8, 0x32e8915c, 0xc083125f, 0x144976b4, 0xe622f5b7, 0xf5720643, 0x07198540, 0x590ab964, 0xab613a67, 0xb831c993, 0x4a5a4a90, 0x9e902e7b, 0x6cfbad78, 0x7fab5e8c, 0x8dc0dd8f, 0xe330a81a, 0x115b2b19, 0x020bd8ed, 0xf0605bee, 0x24aa3f05, 0xd6c1bc06, 0xc5914ff2, 0x37faccf1, 0x69e9f0d5, 0x9b8273d6, 0x88d28022, 0x7ab90321, 0xae7367ca, 0x5c18e4c9, 0x4f48173d, 0xbd23943e, 0xf36e6f75, 0x0105ec76, 0x12551f82, 0xe03e9c81, 0x34f4f86a, 0xc69f7b69, 0xd5cf889d, 0x27a40b9e, 0x79b737ba, 0x8bdcb4b9, 0x988c474d, 0x6ae7c44e, 0xbe2da0a5, 0x4c4623a6, 0x5f16d052, 0xad7d5351);
36
- function getCRC32Table() {
37
- return CRC32C_TABLE;
38
- }