@sebastienrousseau/crypto-lib 0.0.35 → 0.0.36

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 (174) hide show
  1. package/README.md +17 -2
  2. package/dist/accel/index.d.ts +5 -4
  3. package/dist/accel/index.d.ts.map +1 -1
  4. package/dist/accel/index.js +5 -5
  5. package/dist/accel/kdf-backend.d.ts +13 -0
  6. package/dist/accel/kdf-backend.d.ts.map +1 -0
  7. package/dist/accel/kdf-backend.js +81 -0
  8. package/dist/accel/native-pqc-bridge.d.ts.map +1 -1
  9. package/dist/accel/native-pqc-bridge.js +12 -52
  10. package/dist/accel/pq-backend.d.ts +3652 -0
  11. package/dist/accel/pq-backend.d.ts.map +1 -0
  12. package/dist/accel/pq-backend.js +215 -0
  13. package/dist/accel/runtime.d.ts +3 -0
  14. package/dist/accel/runtime.d.ts.map +1 -0
  15. package/dist/accel/runtime.js +9 -0
  16. package/dist/accel/wasm-bridge.d.ts.map +1 -1
  17. package/dist/accel/wasm-bridge.js +14 -13
  18. package/dist/accel/webcrypto-bridge.d.ts.map +1 -1
  19. package/dist/accel/webcrypto-bridge.js +14 -13
  20. package/dist/accel/webcrypto-modern.d.ts.map +1 -1
  21. package/dist/accel/webcrypto-modern.js +15 -14
  22. package/dist/accel/worker-pool.d.ts +18 -0
  23. package/dist/accel/worker-pool.d.ts.map +1 -1
  24. package/dist/accel/worker-pool.js +113 -37
  25. package/dist/bin/cryptolib.d.ts +7 -4
  26. package/dist/bin/cryptolib.d.ts.map +1 -1
  27. package/dist/bin/cryptolib.js +6 -6
  28. package/dist/crypto.d.ts.map +1 -1
  29. package/dist/crypto.js +2 -1
  30. package/dist/encoding.d.ts +10 -0
  31. package/dist/encoding.d.ts.map +1 -0
  32. package/dist/encoding.js +84 -0
  33. package/dist/errors.d.ts +11 -0
  34. package/dist/errors.d.ts.map +1 -1
  35. package/dist/errors.js +20 -1
  36. package/dist/high-level/key-wrap.d.ts.map +1 -1
  37. package/dist/high-level/key-wrap.js +12 -17
  38. package/dist/high-level/multi-recipient.d.ts.map +1 -1
  39. package/dist/high-level/multi-recipient.js +15 -28
  40. package/dist/high-level/password-encrypt.d.ts.map +1 -1
  41. package/dist/high-level/password-encrypt.js +11 -11
  42. package/dist/high-level/sealedbox.d.ts.map +1 -1
  43. package/dist/high-level/sealedbox.js +16 -28
  44. package/dist/high-level/secretbox.d.ts.map +1 -1
  45. package/dist/high-level/secretbox.js +11 -10
  46. package/dist/index.d.ts +1 -1
  47. package/dist/index.d.ts.map +1 -1
  48. package/dist/index.js +3 -1
  49. package/dist/key/keystore.d.ts.map +1 -1
  50. package/dist/key/keystore.js +2 -1
  51. package/dist/keys/keygen.d.ts.map +1 -1
  52. package/dist/keys/keygen.js +13 -14
  53. package/dist/keys/keyring.d.ts.map +1 -1
  54. package/dist/keys/keyring.js +4 -2
  55. package/dist/keys/serialize.d.ts.map +1 -1
  56. package/dist/keys/serialize.js +18 -16
  57. package/dist/lib/decrypt.d.ts.map +1 -1
  58. package/dist/lib/decrypt.js +2 -1
  59. package/dist/lib/generate.d.ts +2 -1
  60. package/dist/lib/generate.d.ts.map +1 -1
  61. package/dist/lib/generate.js +7 -6
  62. package/dist/lib/key-output.d.ts +4 -1
  63. package/dist/lib/key-output.d.ts.map +1 -1
  64. package/dist/lib/key-output.js +2 -2
  65. package/dist/lib/reformat.d.ts +2 -1
  66. package/dist/lib/reformat.d.ts.map +1 -1
  67. package/dist/lib/reformat.js +2 -2
  68. package/dist/lib/revoke.d.ts +2 -1
  69. package/dist/lib/revoke.d.ts.map +1 -1
  70. package/dist/lib/revoke.js +2 -2
  71. package/dist/lib/verify.d.ts +6 -2
  72. package/dist/lib/verify.d.ts.map +1 -1
  73. package/dist/lib/verify.js +28 -9
  74. package/dist/modern/aead.d.ts.map +1 -1
  75. package/dist/modern/aead.js +11 -10
  76. package/dist/modern/aes.d.ts.map +1 -1
  77. package/dist/modern/aes.js +10 -9
  78. package/dist/modern/cost-limits.d.ts.map +1 -1
  79. package/dist/modern/cost-limits.js +3 -2
  80. package/dist/modern/curves.d.ts +10 -5
  81. package/dist/modern/curves.d.ts.map +1 -1
  82. package/dist/modern/curves.js +43 -36
  83. package/dist/modern/ecdh.d.ts +2 -1
  84. package/dist/modern/ecdh.d.ts.map +1 -1
  85. package/dist/modern/ecdh.js +10 -9
  86. package/dist/modern/fn-dsa.d.ts.map +1 -1
  87. package/dist/modern/fn-dsa.js +9 -8
  88. package/dist/modern/hash.d.ts.map +1 -1
  89. package/dist/modern/hash.js +5 -3
  90. package/dist/modern/hpke-xwing.d.ts +10 -0
  91. package/dist/modern/hpke-xwing.d.ts.map +1 -0
  92. package/dist/modern/hpke-xwing.js +26 -0
  93. package/dist/modern/hpke.d.ts +1 -1
  94. package/dist/modern/hpke.d.ts.map +1 -1
  95. package/dist/modern/hpke.js +60 -28
  96. package/dist/modern/hybrid-combiner.d.ts +20 -0
  97. package/dist/modern/hybrid-combiner.d.ts.map +1 -0
  98. package/dist/modern/hybrid-combiner.js +79 -0
  99. package/dist/modern/index.d.ts +10 -10
  100. package/dist/modern/index.d.ts.map +1 -1
  101. package/dist/modern/index.js +17 -7
  102. package/dist/modern/kdf.d.ts.map +1 -1
  103. package/dist/modern/kdf.js +12 -12
  104. package/dist/modern/mac.d.ts +1 -0
  105. package/dist/modern/mac.d.ts.map +1 -1
  106. package/dist/modern/mac.js +24 -15
  107. package/dist/modern/password.d.ts.map +1 -1
  108. package/dist/modern/password.js +19 -18
  109. package/dist/modern/pq-hash-sign.d.ts.map +1 -1
  110. package/dist/modern/pq-hash-sign.js +23 -22
  111. package/dist/modern/pq-kem.d.ts +8 -0
  112. package/dist/modern/pq-kem.d.ts.map +1 -1
  113. package/dist/modern/pq-kem.js +71 -79
  114. package/dist/modern/pq-sign.d.ts +9 -2
  115. package/dist/modern/pq-sign.d.ts.map +1 -1
  116. package/dist/modern/pq-sign.js +60 -23
  117. package/dist/modern/pq.d.ts +0 -17
  118. package/dist/modern/pq.d.ts.map +1 -1
  119. package/dist/modern/pq.js +15 -43
  120. package/dist/modern/signing.d.ts +2 -1
  121. package/dist/modern/signing.d.ts.map +1 -1
  122. package/dist/modern/signing.js +14 -13
  123. package/dist/protocols/iso20022.d.ts.map +1 -1
  124. package/dist/protocols/iso20022.js +12 -6
  125. package/dist/protocols/opaque/ksf.js +2 -2
  126. package/dist/protocols/opaque/suite.d.ts +1 -8
  127. package/dist/protocols/opaque/suite.d.ts.map +1 -1
  128. package/dist/protocols/opaque/suite.js +5 -10
  129. package/dist/protocols/pqxdh.d.ts +9 -2
  130. package/dist/protocols/pqxdh.d.ts.map +1 -1
  131. package/dist/protocols/pqxdh.js +99 -94
  132. package/dist/protocols/ratchet.d.ts.map +1 -1
  133. package/dist/protocols/ratchet.js +14 -15
  134. package/dist/protocols/threshold.d.ts.map +1 -1
  135. package/dist/protocols/threshold.js +16 -15
  136. package/dist/registry.d.ts.map +1 -1
  137. package/dist/registry.js +26 -0
  138. package/dist/streaming/chunks.d.ts +31 -0
  139. package/dist/streaming/chunks.d.ts.map +1 -1
  140. package/dist/streaming/chunks.js +149 -13
  141. package/dist/streaming/stream-aead.d.ts +9 -0
  142. package/dist/streaming/stream-aead.d.ts.map +1 -1
  143. package/dist/streaming/stream-aead.js +27 -53
  144. package/dist/streaming/stream-hash.d.ts.map +1 -1
  145. package/dist/streaming/stream-hash.js +6 -4
  146. package/dist/streaming/stream-multi-pq-aead.d.ts +12 -0
  147. package/dist/streaming/stream-multi-pq-aead.d.ts.map +1 -1
  148. package/dist/streaming/stream-multi-pq-aead.js +82 -106
  149. package/dist/streaming/stream-pq-aead.d.ts +7 -2
  150. package/dist/streaming/stream-pq-aead.d.ts.map +1 -1
  151. package/dist/streaming/stream-pq-aead.js +29 -79
  152. package/dist/streaming/web-streams.d.ts +5 -2
  153. package/dist/streaming/web-streams.d.ts.map +1 -1
  154. package/dist/streaming/web-streams.js +90 -151
  155. package/dist/testing/index.d.ts +5 -0
  156. package/dist/testing/index.d.ts.map +1 -0
  157. package/dist/testing/index.js +13 -0
  158. package/dist/tokens/index.d.ts.map +1 -1
  159. package/dist/tokens/key-rotation.d.ts.map +1 -1
  160. package/dist/tokens/key-rotation.js +4 -2
  161. package/dist/tokens/paseto.d.ts +2 -0
  162. package/dist/tokens/paseto.d.ts.map +1 -1
  163. package/dist/tokens/paseto.js +56 -29
  164. package/dist/types/types.d.ts +1 -0
  165. package/dist/types/types.d.ts.map +1 -1
  166. package/dist/utils.d.ts.map +1 -1
  167. package/dist/utils.js +3 -2
  168. package/package.json +12 -2
  169. package/dist/config/config.d.ts +0 -8
  170. package/dist/config/config.d.ts.map +0 -1
  171. package/dist/config/config.js +0 -13
  172. package/dist/key/key.d.ts +0 -10
  173. package/dist/key/key.d.ts.map +0 -1
  174. package/dist/key/key.js +0 -25
@@ -1,20 +1,23 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.WEB_STREAM_HASH_ALGORITHMS = void 0;
4
+ exports.createHashStream = createHashStream;
4
5
  exports.createEncryptStream = createEncryptStream;
5
6
  exports.createDecryptStream = createDecryptStream;
6
- exports.createHashStream = createHashStream;
7
7
  exports.createPqEncryptStream = createPqEncryptStream;
8
8
  exports.createPqDecryptStream = createPqDecryptStream;
9
9
  exports.createMultiPqEncryptStream = createMultiPqEncryptStream;
10
10
  exports.createMultiPqDecryptStream = createMultiPqDecryptStream;
11
11
  const chacha_js_1 = require("@noble/ciphers/chacha.js");
12
- const utils_js_1 = require("@noble/ciphers/utils.js");
13
12
  const sha2_js_1 = require("@noble/hashes/sha2.js");
14
13
  const sha3_js_1 = require("@noble/hashes/sha3.js");
15
14
  const blake3_js_1 = require("@noble/hashes/blake3.js");
15
+ const stream_aead_1 = require("./stream-aead");
16
16
  const stream_pq_aead_1 = require("./stream-pq-aead");
17
17
  const stream_multi_pq_aead_1 = require("./stream-multi-pq-aead");
18
+ const chunks_1 = require("./chunks");
19
+ const errors_1 = require("../errors");
20
+ const encoding_1 = require("../encoding");
18
21
  const TS = globalThis
19
22
  .TransformStream;
20
23
  const NONCE_LEN = 24;
@@ -25,22 +28,6 @@ exports.WEB_STREAM_HASH_ALGORITHMS = [
25
28
  "sha3-512",
26
29
  "blake3",
27
30
  ];
28
- const HEX_RE = /^[0-9a-fA-F]*$/;
29
- function toKey(key) {
30
- if (key instanceof Uint8Array) {
31
- if (key.length !== 32) {
32
- throw new Error(`Key must be 32 bytes, got ${key.length}`);
33
- }
34
- return key;
35
- }
36
- if (!HEX_RE.test(key))
37
- throw new Error("Invalid hex key");
38
- const buf = Buffer.from(key, "hex");
39
- if (buf.length !== 32) {
40
- throw new Error(`Key must be 32 bytes (256 bits), got ${buf.length}`);
41
- }
42
- return buf;
43
- }
44
31
  const hashFactories = {
45
32
  sha256: () => sha2_js_1.sha256.create(),
46
33
  sha512: () => sha2_js_1.sha512.create(),
@@ -48,63 +35,12 @@ const hashFactories = {
48
35
  "sha3-512": () => sha3_js_1.sha3_512.create(),
49
36
  blake3: () => blake3_js_1.blake3.create(),
50
37
  };
51
- function createEncryptStream(options) {
52
- const keyBytes = toKey(options.key);
53
- const chunks = [];
54
- let totalLen = 0;
55
- return new TS({
56
- transform(chunk) {
57
- chunks.push(chunk);
58
- totalLen += chunk.length;
59
- },
60
- flush(controller) {
61
- const plaintext = new Uint8Array(totalLen);
62
- let offset = 0;
63
- for (const c of chunks) {
64
- plaintext.set(c, offset);
65
- offset += c.length;
66
- }
67
- const nonce = (0, utils_js_1.randomBytes)(NONCE_LEN);
68
- const cipher = (0, chacha_js_1.xchacha20poly1305)(keyBytes, nonce);
69
- const encrypted = cipher.encrypt(plaintext);
70
- const output = new Uint8Array(NONCE_LEN + encrypted.length);
71
- output.set(nonce, 0);
72
- output.set(encrypted, NONCE_LEN);
73
- controller.enqueue(output);
74
- },
75
- });
76
- }
77
- function createDecryptStream(options) {
78
- const keyBytes = toKey(options.key);
79
- const chunks = [];
80
- let totalLen = 0;
81
- return new TS({
82
- transform(chunk) {
83
- chunks.push(chunk);
84
- totalLen += chunk.length;
85
- },
86
- flush(controller) {
87
- const ciphertext = new Uint8Array(totalLen);
88
- let offset = 0;
89
- for (const c of chunks) {
90
- ciphertext.set(c, offset);
91
- offset += c.length;
92
- }
93
- if (ciphertext.length < NONCE_LEN + 16) {
94
- throw new Error("Ciphertext too short — missing nonce or auth tag");
95
- }
96
- const nonce = ciphertext.subarray(0, NONCE_LEN);
97
- const encrypted = ciphertext.subarray(NONCE_LEN);
98
- const cipher = (0, chacha_js_1.xchacha20poly1305)(keyBytes, nonce);
99
- const plaintext = cipher.decrypt(encrypted);
100
- controller.enqueue(plaintext);
101
- },
102
- });
103
- }
104
38
  function createHashStream(algorithm) {
105
- const factory = hashFactories[algorithm];
39
+ const factory = Object.hasOwn(hashFactories, algorithm)
40
+ ? hashFactories[algorithm]
41
+ : undefined;
106
42
  if (!factory) {
107
- throw new Error(`Unsupported hash algorithm: ${algorithm}`);
43
+ throw new errors_1.CryptoError(`Unsupported hash algorithm: ${algorithm}`, errors_1.CryptoErrorCode.UNSUPPORTED_ALGORITHM);
108
44
  }
109
45
  const hasher = factory();
110
46
  return new TS({
@@ -112,109 +48,112 @@ function createHashStream(algorithm) {
112
48
  hasher.update(chunk);
113
49
  },
114
50
  flush(controller) {
115
- const digest = Buffer.from(hasher.digest()).toString("hex");
51
+ const digest = (0, encoding_1.toHex)(hasher.digest());
116
52
  controller.enqueue({ digest, algorithm });
117
53
  },
118
54
  });
119
55
  }
120
- function createPqEncryptStream(options) {
121
- const chunks = [];
122
- let totalLen = 0;
56
+ function chunkedEncryptStream(start, finish = () => undefined) {
57
+ let encryptor;
123
58
  return new TS({
124
- transform(chunk) {
125
- chunks.push(chunk);
126
- totalLen += chunk.length;
59
+ start(controller) {
60
+ const started = start();
61
+ encryptor = started.encryptor;
62
+ controller.enqueue(started.header);
63
+ },
64
+ transform(chunk, controller) {
65
+ for (const sealed of encryptor.push(chunk))
66
+ controller.enqueue(sealed);
127
67
  },
128
68
  flush(controller) {
129
- const plaintext = new Uint8Array(totalLen);
130
- let offset = 0;
131
- for (const c of chunks) {
132
- plaintext.set(c, offset);
133
- offset += c.length;
134
- }
135
- const encrypted = (0, stream_pq_aead_1.streamPqEncrypt)({
136
- recipientX25519Public: options.recipientX25519Public,
137
- recipientMlKemPublic: options.recipientMlKemPublic,
138
- plaintext,
139
- chunkSize: options.chunkSize,
140
- });
141
- controller.enqueue(encrypted.ciphertext);
69
+ controller.enqueue(encryptor.finish());
70
+ finish();
142
71
  },
143
72
  });
144
73
  }
145
- function createPqDecryptStream(options) {
146
- const chunks = [];
147
- let totalLen = 0;
74
+ function chunkedDecryptStream(headerLength, open) {
75
+ let pending = new Uint8Array(0);
76
+ let opened;
148
77
  return new TS({
149
- transform(chunk) {
150
- chunks.push(chunk);
151
- totalLen += chunk.length;
78
+ transform(chunk, controller) {
79
+ if (opened === undefined) {
80
+ pending = Uint8Array.from((0, encoding_1.concat)([pending, chunk]));
81
+ const length = headerLength(pending);
82
+ if (length === undefined)
83
+ return;
84
+ opened = open(pending.subarray(0, length));
85
+ chunk = pending.subarray(length);
86
+ }
87
+ for (const plain of opened.decryptor.push(chunk)) {
88
+ controller.enqueue(plain);
89
+ }
152
90
  },
153
91
  flush(controller) {
154
- const ciphertext = new Uint8Array(totalLen);
155
- let offset = 0;
156
- for (const c of chunks) {
157
- ciphertext.set(c, offset);
158
- offset += c.length;
92
+ if (opened === undefined) {
93
+ throw new errors_1.CryptoError("Ciphertext too short — missing header or chunk data", errors_1.CryptoErrorCode.INVALID_CIPHERTEXT);
94
+ }
95
+ try {
96
+ controller.enqueue(opened.decryptor.finish());
97
+ }
98
+ finally {
99
+ opened.wipe?.();
159
100
  }
160
- const decrypted = (0, stream_pq_aead_1.streamPqDecrypt)({
161
- recipientX25519Secret: options.recipientX25519Secret,
162
- recipientMlKemSecret: options.recipientMlKemSecret,
163
- ciphertext,
164
- chunkSize: options.chunkSize,
165
- });
166
- controller.enqueue(decrypted);
167
101
  },
168
102
  });
169
103
  }
170
- function createMultiPqEncryptStream(options) {
171
- const chunks = [];
172
- let totalLen = 0;
104
+ function createEncryptStream(options) {
105
+ const key = (0, stream_aead_1.toKey)(options.key);
106
+ const chunkSize = options.chunkSize ?? stream_pq_aead_1.DEFAULT_CHUNK_SIZE;
107
+ (0, chunks_1.assertChunkSize)(chunkSize);
108
+ return chunkedEncryptStream(() => (0, stream_aead_1.aeadStreamSender)(key, chunkSize));
109
+ }
110
+ function createDecryptStream(options) {
111
+ const key = (0, stream_aead_1.toKey)(options.key);
112
+ if (options.format === "single-message")
113
+ return singleMessageDecrypt(key);
114
+ const chunkSize = options.chunkSize ?? stream_pq_aead_1.DEFAULT_CHUNK_SIZE;
115
+ (0, chunks_1.assertChunkSize)(chunkSize);
116
+ return chunkedDecryptStream((bytes) => bytes.length >= stream_aead_1.AEAD_STREAM_HEADER_LEN
117
+ ? stream_aead_1.AEAD_STREAM_HEADER_LEN
118
+ : undefined, (header) => ({ decryptor: (0, stream_aead_1.aeadStreamReceiver)(key, header, chunkSize) }));
119
+ }
120
+ function singleMessageDecrypt(key) {
121
+ const parts = [];
173
122
  return new TS({
174
123
  transform(chunk) {
175
- chunks.push(chunk);
176
- totalLen += chunk.length;
124
+ parts.push(chunk);
177
125
  },
178
126
  flush(controller) {
179
- const plaintext = new Uint8Array(totalLen);
180
- let offset = 0;
181
- for (const c of chunks) {
182
- plaintext.set(c, offset);
183
- offset += c.length;
127
+ const message = Uint8Array.from((0, encoding_1.concat)(parts));
128
+ if (message.length < NONCE_LEN + 16) {
129
+ throw new errors_1.CryptoError("Ciphertext too short — missing nonce or auth tag", errors_1.CryptoErrorCode.INVALID_CIPHERTEXT);
184
130
  }
185
- const encrypted = (0, stream_multi_pq_aead_1.streamMultiPqEncrypt)({
186
- recipients: options.recipients,
187
- plaintext,
188
- chunkSize: options.chunkSize,
189
- });
190
- controller.enqueue(encrypted.ciphertext);
131
+ const nonce = message.subarray(0, NONCE_LEN);
132
+ controller.enqueue((0, chacha_js_1.xchacha20poly1305)(key, nonce).decrypt(message.subarray(NONCE_LEN)));
191
133
  },
192
134
  });
193
135
  }
136
+ function createPqEncryptStream(options) {
137
+ const recipX25519Pub = (0, stream_pq_aead_1.toBytes)(options.recipientX25519Public, 32, "recipientX25519Public");
138
+ const recipMlKemPub = (0, stream_pq_aead_1.toBytes)(options.recipientMlKemPublic, 1184, "recipientMlKemPublic");
139
+ const chunkSize = options.chunkSize ?? stream_pq_aead_1.DEFAULT_CHUNK_SIZE;
140
+ (0, chunks_1.assertChunkSize)(chunkSize);
141
+ return chunkedEncryptStream(() => (0, stream_pq_aead_1.pqStreamSender)(recipX25519Pub, recipMlKemPub, chunkSize));
142
+ }
143
+ function createPqDecryptStream(options) {
144
+ const recipX25519Sec = (0, stream_pq_aead_1.toBytes)(options.recipientX25519Secret, 32, "recipientX25519Secret");
145
+ const recipMlKemSec = (0, stream_pq_aead_1.toBytes)(options.recipientMlKemSecret, 2400, "recipientMlKemSecret");
146
+ const chunkSize = options.chunkSize ?? stream_pq_aead_1.DEFAULT_CHUNK_SIZE;
147
+ (0, chunks_1.assertChunkSize)(chunkSize);
148
+ return chunkedDecryptStream((bytes) => bytes.length >= stream_pq_aead_1.PQ_STREAM_HEADER_LEN ? stream_pq_aead_1.PQ_STREAM_HEADER_LEN : undefined, (header) => ({
149
+ decryptor: (0, stream_pq_aead_1.pqStreamReceiver)(recipX25519Sec, recipMlKemSec, header, chunkSize),
150
+ }));
151
+ }
152
+ function createMultiPqEncryptStream(options) {
153
+ let started;
154
+ return chunkedEncryptStream(() => (started = (0, stream_multi_pq_aead_1.multiPqStreamSender)(options)), () => started.wipe());
155
+ }
194
156
  function createMultiPqDecryptStream(options) {
195
- const chunks = [];
196
- let totalLen = 0;
197
- return new TS({
198
- transform(chunk) {
199
- chunks.push(chunk);
200
- totalLen += chunk.length;
201
- },
202
- flush(controller) {
203
- const ciphertext = new Uint8Array(totalLen);
204
- let offset = 0;
205
- for (const c of chunks) {
206
- ciphertext.set(c, offset);
207
- offset += c.length;
208
- }
209
- const decrypted = (0, stream_multi_pq_aead_1.streamMultiPqDecrypt)({
210
- recipientId: options.recipientId,
211
- recipientX25519Secret: options.recipientX25519Secret,
212
- recipientMlKemSecret: options.recipientMlKemSecret,
213
- ciphertext,
214
- chunkSize: options.chunkSize,
215
- });
216
- controller.enqueue(decrypted.plaintext);
217
- },
218
- });
157
+ return chunkedDecryptStream(stream_multi_pq_aead_1.multiPqHeaderLength, (header) => (0, stream_multi_pq_aead_1.multiPqStreamReceiver)(options, header));
219
158
  }
220
159
  //# sourceMappingURL=web-streams.js.map
@@ -0,0 +1,5 @@
1
+ export { _resetKeystoreForTests as resetKeystore } from "../key/keystore";
2
+ export { resetNativePqcCache, _forceNativePqcDetected as forceNativePqcDetected, } from "../accel/native-pqc-bridge";
3
+ export { _resetWasmDetection as resetWasmDetection } from "../accel/wasm-bridge";
4
+ export { _resetModernWebCryptoDetection as resetModernWebCryptoDetection } from "../accel/webcrypto-modern";
5
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/testing/index.ts"],"names":[],"mappings":"AAmBA,OAAO,EAAE,sBAAsB,IAAI,aAAa,EAAE,MAAM,iBAAiB,CAAC;AAC1E,OAAO,EACL,mBAAmB,EACnB,uBAAuB,IAAI,sBAAsB,GAClD,MAAM,4BAA4B,CAAC;AACpC,OAAO,EAAE,mBAAmB,IAAI,kBAAkB,EAAE,MAAM,sBAAsB,CAAC;AACjF,OAAO,EAAE,8BAA8B,IAAI,6BAA6B,EAAE,MAAM,2BAA2B,CAAC"}
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.resetModernWebCryptoDetection = exports.resetWasmDetection = exports.forceNativePqcDetected = exports.resetNativePqcCache = exports.resetKeystore = void 0;
4
+ var keystore_1 = require("../key/keystore");
5
+ Object.defineProperty(exports, "resetKeystore", { enumerable: true, get: function () { return keystore_1._resetKeystoreForTests; } });
6
+ var native_pqc_bridge_1 = require("../accel/native-pqc-bridge");
7
+ Object.defineProperty(exports, "resetNativePqcCache", { enumerable: true, get: function () { return native_pqc_bridge_1.resetNativePqcCache; } });
8
+ Object.defineProperty(exports, "forceNativePqcDetected", { enumerable: true, get: function () { return native_pqc_bridge_1._forceNativePqcDetected; } });
9
+ var wasm_bridge_1 = require("../accel/wasm-bridge");
10
+ Object.defineProperty(exports, "resetWasmDetection", { enumerable: true, get: function () { return wasm_bridge_1._resetWasmDetection; } });
11
+ var webcrypto_modern_1 = require("../accel/webcrypto-modern");
12
+ Object.defineProperty(exports, "resetModernWebCryptoDetection", { enumerable: true, get: function () { return webcrypto_modern_1._resetModernWebCryptoDetection; } });
13
+ //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/tokens/index.ts"],"names":[],"mappings":"AAcA,OAAO,EAAE,OAAO,EAAE,QAAQ,EAAE,GAAG,EAAE,MAAM,UAAU,CAAC;AAElD,YAAY,EACV,yBAAyB,EACzB,yBAAyB,EACzB,uBAAuB,EACvB,yBAAyB,EACzB,WAAW,EACX,aAAa,GACd,MAAM,UAAU,CAAC;AAElB,OAAO,EACL,aAAa,EACb,SAAS,EACT,gBAAgB,EAChB,gBAAgB,EAChB,kBAAkB,EAClB,kBAAkB,GACnB,MAAM,gBAAgB,CAAC;AAExB,YAAY,EAAE,YAAY,EAAE,OAAO,EAAE,MAAM,gBAAgB,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/tokens/index.ts"],"names":[],"mappings":"AAYA,OAAO,EAAE,OAAO,EAAE,QAAQ,EAAE,GAAG,EAAE,MAAM,UAAU,CAAC;AAElD,YAAY,EACV,yBAAyB,EACzB,yBAAyB,EACzB,uBAAuB,EACvB,yBAAyB,EACzB,WAAW,EACX,aAAa,GACd,MAAM,UAAU,CAAC;AAElB,OAAO,EACL,aAAa,EACb,SAAS,EACT,gBAAgB,EAChB,gBAAgB,EAChB,kBAAkB,EAClB,kBAAkB,GACnB,MAAM,gBAAgB,CAAC;AAExB,YAAY,EAAE,YAAY,EAAE,OAAO,EAAE,MAAM,gBAAgB,CAAC"}
@@ -1 +1 @@
1
- {"version":3,"file":"key-rotation.d.ts","sourceRoot":"","sources":["../../src/tokens/key-rotation.ts"],"names":[],"mappings":"AA8BA,MAAM,WAAW,YAAY;IAE3B,OAAO,EAAE,MAAM,CAAC;IAEhB,GAAG,EAAE,MAAM,CAAC;IAEZ,WAAW,EAAE,IAAI,CAAC;IAElB,SAAS,CAAC,EAAE,IAAI,CAAC;CAClB;AAMD,MAAM,WAAW,OAAO;IAEtB,OAAO,EAAE,YAAY,CAAC;IAEtB,QAAQ,EAAE,YAAY,EAAE,CAAC;CAC1B;AAaD,wBAAgB,aAAa,CAAC,OAAO,EAAE,YAAY,GAAG,OAAO,CAE5D;AAeD,wBAAgB,SAAS,CAAC,IAAI,EAAE,OAAO,EAAE,MAAM,EAAE,YAAY,GAAG,OAAO,CAKtE;AAcD,wBAAgB,gBAAgB,CAC9B,IAAI,EAAE,OAAO,EACb,OAAO,EAAE,MAAM,GACd,YAAY,GAAG,SAAS,CAG1B;AAiBD,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,OAAO,EAAE,GAAG,CAAC,EAAE,IAAI,GAAG,OAAO,CAMnE;AAeD,wBAAgB,kBAAkB,CAChC,IAAI,EAAE,OAAO,EACb,SAAS,EAAE,MAAM,GAChB;IAED,UAAU,EAAE,MAAM,CAAC;IAEnB,OAAO,EAAE,MAAM,CAAC;CACjB,CAGA;AAgBD,wBAAgB,kBAAkB,CAChC,IAAI,EAAE,OAAO,EACb,UAAU,EAAE,MAAM,EAClB,OAAO,EAAE,MAAM,GACd,MAAM,CAOR"}
1
+ {"version":3,"file":"key-rotation.d.ts","sourceRoot":"","sources":["../../src/tokens/key-rotation.ts"],"names":[],"mappings":"AAgCA,MAAM,WAAW,YAAY;IAE3B,OAAO,EAAE,MAAM,CAAC;IAEhB,GAAG,EAAE,MAAM,CAAC;IAEZ,WAAW,EAAE,IAAI,CAAC;IAElB,SAAS,CAAC,EAAE,IAAI,CAAC;CAClB;AAMD,MAAM,WAAW,OAAO;IAEtB,OAAO,EAAE,YAAY,CAAC;IAEtB,QAAQ,EAAE,YAAY,EAAE,CAAC;CAC1B;AAaD,wBAAgB,aAAa,CAAC,OAAO,EAAE,YAAY,GAAG,OAAO,CAE5D;AAeD,wBAAgB,SAAS,CAAC,IAAI,EAAE,OAAO,EAAE,MAAM,EAAE,YAAY,GAAG,OAAO,CAKtE;AAcD,wBAAgB,gBAAgB,CAC9B,IAAI,EAAE,OAAO,EACb,OAAO,EAAE,MAAM,GACd,YAAY,GAAG,SAAS,CAG1B;AAiBD,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,OAAO,EAAE,GAAG,CAAC,EAAE,IAAI,GAAG,OAAO,CAMnE;AAeD,wBAAgB,kBAAkB,CAChC,IAAI,EAAE,OAAO,EACb,SAAS,EAAE,MAAM,GAChB;IAED,UAAU,EAAE,MAAM,CAAC;IAEnB,OAAO,EAAE,MAAM,CAAC;CACjB,CAGA;AAgBD,wBAAgB,kBAAkB,CAChC,IAAI,EAAE,OAAO,EACb,UAAU,EAAE,MAAM,EAClB,OAAO,EAAE,MAAM,GACd,MAAM,CAUR"}
@@ -40,6 +40,8 @@ exports.pruneExpiredKeys = pruneExpiredKeys;
40
40
  exports.encryptWithVersion = encryptWithVersion;
41
41
  exports.decryptWithVersion = decryptWithVersion;
42
42
  const secretbox = __importStar(require("../high-level/secretbox"));
43
+ const errors_1 = require("../errors");
44
+ const encoding_1 = require("../encoding");
43
45
  function createKeyRing(current) {
44
46
  return { current, previous: [] };
45
47
  }
@@ -68,9 +70,9 @@ function encryptWithVersion(ring, plaintext) {
68
70
  function decryptWithVersion(ring, ciphertext, version) {
69
71
  const vk = findKeyByVersion(ring, version);
70
72
  if (!vk) {
71
- throw new Error(`Key version "${version}" not found in key ring`);
73
+ throw new errors_1.CryptoError(`Key version "${version}" not found in key ring`, errors_1.CryptoErrorCode.INVALID_KEY);
72
74
  }
73
75
  const pt = secretbox.open(vk.key, ciphertext);
74
- return Buffer.from(pt).toString("utf8");
76
+ return (0, encoding_1.fromUtf8)(pt);
75
77
  }
76
78
  //# sourceMappingURL=key-rotation.js.map
@@ -9,6 +9,7 @@ export interface PasetoLocalDecryptOptions {
9
9
  token: string;
10
10
  footer?: string;
11
11
  implicit?: string;
12
+ now?: Date;
12
13
  }
13
14
  export interface PasetoPublicSignOptions {
14
15
  secretKey: string;
@@ -21,6 +22,7 @@ export interface PasetoPublicVerifyOptions {
21
22
  token: string;
22
23
  footer?: string;
23
24
  implicit?: string;
25
+ now?: Date;
24
26
  }
25
27
  export interface PasetoToken {
26
28
  token: string;
@@ -1 +1 @@
1
- {"version":3,"file":"paseto.d.ts","sourceRoot":"","sources":["../../src/tokens/paseto.ts"],"names":[],"mappings":"AAyDA,MAAM,WAAW,yBAAyB;IAExC,GAAG,EAAE,MAAM,CAAC;IAEZ,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAEjC,MAAM,CAAC,EAAE,MAAM,CAAC;IAEhB,QAAQ,CAAC,EAAE,MAAM,CAAC;CACnB;AAGD,MAAM,WAAW,yBAAyB;IAExC,GAAG,EAAE,MAAM,CAAC;IAEZ,KAAK,EAAE,MAAM,CAAC;IAEd,MAAM,CAAC,EAAE,MAAM,CAAC;IAEhB,QAAQ,CAAC,EAAE,MAAM,CAAC;CACnB;AAGD,MAAM,WAAW,uBAAuB;IAEtC,SAAS,EAAE,MAAM,CAAC;IAElB,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAEjC,MAAM,CAAC,EAAE,MAAM,CAAC;IAEhB,QAAQ,CAAC,EAAE,MAAM,CAAC;CACnB;AAGD,MAAM,WAAW,yBAAyB;IAExC,SAAS,EAA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("@noble/ciphers/chacha.js");
7
7
  const utils_js_1 = require("@noble/ciphers/utils.js");
8
8
  const ed25519_js_1 = require("@noble/curves/ed25519.js");
9
9
  const blake2_js_1 = require("@noble/hashes/blake2.js");
10
+ const errors_1 = require("../errors");
11
+ const encoding_1 = require("../encoding");
10
12
  const V4_LOCAL_HEADER = "v4.local.";
11
13
  const V4_PUBLIC_HEADER = "v4.public.";
12
14
  const NONCE_LEN = 32;
@@ -15,7 +17,31 @@ const N2_LEN = 24;
15
17
  const TAG_LEN = 32;
16
18
  const SIG_LEN = 64;
17
19
  const BASE64URL_RE = /^[A-Za-z0-9_-]*$/;
18
- const HEX_RE = /^[0-9a-fA-F]*$/;
20
+ const RFC3339_RE = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d+)?(Z|[+-]\d{2}:\d{2})$/;
21
+ function timeClaim(payload, name) {
22
+ if (!(name in payload))
23
+ return undefined;
24
+ const value = payload[name];
25
+ const ms = typeof value === "string" && RFC3339_RE.test(value)
26
+ ? Date.parse(value)
27
+ : NaN;
28
+ if (Number.isNaN(ms))
29
+ throw new errors_1.CryptoError(`Invalid ${name} claim`, errors_1.CryptoErrorCode.INVALID_TOKEN);
30
+ return ms;
31
+ }
32
+ function checkTimeClaims(payload, now) {
33
+ const t = now.getTime();
34
+ const exp = timeClaim(payload, "exp");
35
+ const nbf = timeClaim(payload, "nbf");
36
+ const iat = timeClaim(payload, "iat");
37
+ if (exp !== undefined && t >= exp)
38
+ throw new errors_1.CryptoError("Token expired", errors_1.CryptoErrorCode.INVALID_TOKEN);
39
+ if (nbf !== undefined && t < nbf)
40
+ throw new errors_1.CryptoError("Token not yet valid", errors_1.CryptoErrorCode.INVALID_TOKEN);
41
+ if (iat !== undefined && t < iat) {
42
+ throw new errors_1.CryptoError("Token issued in the future", errors_1.CryptoErrorCode.INVALID_TOKEN);
43
+ }
44
+ }
19
45
  function pae(...pieces) {
20
46
  const le64 = (n) => {
21
47
  const buf = new Uint8Array(8);
@@ -38,25 +64,24 @@ function pae(...pieces) {
38
64
  return result;
39
65
  }
40
66
  function hexToBytes(hex) {
41
- if (!HEX_RE.test(hex))
42
- throw new Error("Invalid hex string");
43
- return Buffer.from(hex, "hex");
44
- }
45
- function toBase64url(buf) {
46
- return Buffer.from(buf).toString("base64url");
67
+ if (!(0, encoding_1.isHex)(hex))
68
+ throw new errors_1.CryptoError("Invalid hex string", errors_1.CryptoErrorCode.INVALID_HEX);
69
+ return (0, encoding_1.decodeHex)(hex);
47
70
  }
48
71
  function fromBase64url(s) {
49
- const bytes = Buffer.from(s, "base64url");
50
- if (!BASE64URL_RE.test(s) || bytes.toString("base64url") !== s) {
51
- throw new Error("Invalid base64url encoding");
52
- }
72
+ const invalid = () => new errors_1.CryptoError("Invalid base64url encoding", errors_1.CryptoErrorCode.INVALID_TOKEN);
73
+ if (!BASE64URL_RE.test(s))
74
+ throw invalid();
75
+ const bytes = (0, encoding_1.fromBase64)(s);
76
+ if ((0, encoding_1.toBase64url)(bytes) !== s)
77
+ throw invalid();
53
78
  return bytes;
54
79
  }
55
80
  const encoder = new TextEncoder();
56
81
  function localKey(key) {
57
82
  const keyBytes = hexToBytes(key);
58
83
  if (keyBytes.length !== 32) {
59
- throw new Error(`Key must be 32 bytes, got ${keyBytes.length}`);
84
+ throw new errors_1.CryptoError(`Key must be 32 bytes, got ${keyBytes.length}`, errors_1.CryptoErrorCode.INVALID_KEY);
60
85
  }
61
86
  return keyBytes;
62
87
  }
@@ -83,7 +108,7 @@ function localEncryptWithNonce(opts, nonce) {
83
108
  const { key, payload, footer = "", implicit = "" } = opts;
84
109
  const keyBytes = localKey(key);
85
110
  if (nonce.length !== NONCE_LEN) {
86
- throw new Error(`Nonce must be ${NONCE_LEN} bytes, got ${nonce.length}`);
111
+ throw new errors_1.CryptoError(`Nonce must be ${NONCE_LEN} bytes, got ${nonce.length}`, errors_1.CryptoErrorCode.INVALID_INPUT);
87
112
  }
88
113
  const footerBytes = encoder.encode(footer);
89
114
  const { ek, n2, ak } = localSubkeys(keyBytes, nonce);
@@ -93,9 +118,9 @@ function localEncryptWithNonce(opts, nonce) {
93
118
  body.set(nonce);
94
119
  body.set(ciphertext, NONCE_LEN);
95
120
  body.set(tag, NONCE_LEN + ciphertext.length);
96
- let token = V4_LOCAL_HEADER + toBase64url(body);
121
+ let token = V4_LOCAL_HEADER + (0, encoding_1.toBase64url)(body);
97
122
  if (footer) {
98
- token += "." + toBase64url(footerBytes);
123
+ token += "." + (0, encoding_1.toBase64url)(footerBytes);
99
124
  }
100
125
  return { token };
101
126
  }
@@ -105,20 +130,20 @@ function localEncrypt(opts) {
105
130
  function assertFooter(footerB64, footer) {
106
131
  const expected = encoder.encode(footer);
107
132
  if (!(0, utils_js_1.equalBytes)(fromBase64url(footerB64), expected)) {
108
- throw new Error("Footer mismatch");
133
+ throw new errors_1.CryptoError("Footer mismatch", errors_1.CryptoErrorCode.INVALID_TOKEN);
109
134
  }
110
135
  }
111
136
  function localDecrypt(opts) {
112
137
  const { key, token, footer = "", implicit = "" } = opts;
113
138
  const keyBytes = localKey(key);
114
139
  if (!token.startsWith(V4_LOCAL_HEADER)) {
115
- throw new Error(`Invalid token header: expected "${V4_LOCAL_HEADER}"`);
140
+ throw new errors_1.CryptoError(`Invalid token header: expected "${V4_LOCAL_HEADER}"`, errors_1.CryptoErrorCode.INVALID_TOKEN);
116
141
  }
117
142
  const parts = token.slice(V4_LOCAL_HEADER.length).split(".");
118
143
  assertFooter(parts[1] ?? "", footer);
119
144
  const body = fromBase64url(parts[0]);
120
145
  if (body.length < NONCE_LEN + TAG_LEN) {
121
- throw new Error("Token body too short");
146
+ throw new errors_1.CryptoError("Token body too short", errors_1.CryptoErrorCode.INVALID_TOKEN);
122
147
  }
123
148
  const nonce = body.subarray(0, NONCE_LEN);
124
149
  const ciphertext = body.subarray(NONCE_LEN, body.length - TAG_LEN);
@@ -126,17 +151,18 @@ function localDecrypt(opts) {
126
151
  const { ek, n2, ak } = localSubkeys(keyBytes, nonce);
127
152
  const expected = localTag(ak, nonce, ciphertext, encoder.encode(footer), encoder.encode(implicit));
128
153
  if (!(0, utils_js_1.equalBytes)(tag, expected)) {
129
- throw new Error("Invalid token: authentication failed");
154
+ throw new errors_1.CryptoError("Invalid token: authentication failed", errors_1.CryptoErrorCode.AUTH_FAILED);
130
155
  }
131
156
  const plaintext = (0, chacha_js_1.xchacha20)(ek, n2, ciphertext);
132
- const payload = JSON.parse(Buffer.from(plaintext).toString("utf8"));
157
+ const payload = JSON.parse((0, encoding_1.fromUtf8)(plaintext));
158
+ checkTimeClaims(payload, opts.now ?? new Date());
133
159
  return { payload, ...(footer ? { footer } : {}) };
134
160
  }
135
161
  function publicSign(opts) {
136
162
  const { secretKey, payload, footer = "", implicit = "" } = opts;
137
163
  const skBytes = hexToBytes(secretKey);
138
164
  if (skBytes.length !== 64 && skBytes.length !== 32) {
139
- throw new Error(`Secret key must be 32 or 64 bytes, got ${skBytes.length}`);
165
+ throw new errors_1.CryptoError(`Secret key must be 32 or 64 bytes, got ${skBytes.length}`, errors_1.CryptoErrorCode.INVALID_KEY);
140
166
  }
141
167
  const header = encoder.encode(V4_PUBLIC_HEADER);
142
168
  const message = encoder.encode(JSON.stringify(payload));
@@ -148,9 +174,9 @@ function publicSign(opts) {
148
174
  const body = new Uint8Array(message.length + sig.length);
149
175
  body.set(message);
150
176
  body.set(sig, message.length);
151
- let token = V4_PUBLIC_HEADER + toBase64url(body);
177
+ let token = V4_PUBLIC_HEADER + (0, encoding_1.toBase64url)(body);
152
178
  if (footer) {
153
- token += "." + toBase64url(footerBytes);
179
+ token += "." + (0, encoding_1.toBase64url)(footerBytes);
154
180
  }
155
181
  return { token };
156
182
  }
@@ -158,10 +184,10 @@ function publicVerify(opts) {
158
184
  const { publicKey, token, footer = "", implicit = "" } = opts;
159
185
  const pkBytes = hexToBytes(publicKey);
160
186
  if (pkBytes.length !== 32) {
161
- throw new Error(`Public key must be 32 bytes, got ${pkBytes.length}`);
187
+ throw new errors_1.CryptoError(`Public key must be 32 bytes, got ${pkBytes.length}`, errors_1.CryptoErrorCode.INVALID_KEY);
162
188
  }
163
189
  if (!token.startsWith(V4_PUBLIC_HEADER)) {
164
- throw new Error(`Invalid token header: expected "${V4_PUBLIC_HEADER}"`);
190
+ throw new errors_1.CryptoError(`Invalid token header: expected "${V4_PUBLIC_HEADER}"`, errors_1.CryptoErrorCode.INVALID_TOKEN);
165
191
  }
166
192
  const withoutHeader = token.slice(V4_PUBLIC_HEADER.length);
167
193
  const parts = withoutHeader.split(".");
@@ -171,19 +197,20 @@ function publicVerify(opts) {
171
197
  const footerBytes = encoder.encode(footer);
172
198
  const body = fromBase64url(bodyB64);
173
199
  if (body.length < SIG_LEN) {
174
- throw new Error("Token body too short");
200
+ throw new errors_1.CryptoError("Token body too short", errors_1.CryptoErrorCode.INVALID_TOKEN);
175
201
  }
176
202
  const message = body.subarray(0, body.length - SIG_LEN);
177
203
  const sig = body.subarray(body.length - SIG_LEN);
178
204
  const header = encoder.encode(V4_PUBLIC_HEADER);
179
205
  const implicitBytes = encoder.encode(implicit);
180
206
  const m2 = pae(header, message, footerBytes, implicitBytes);
181
- const valid = ed25519_js_1.ed25519.verify(sig, m2, pkBytes);
207
+ const valid = ed25519_js_1.ed25519.verify(sig, m2, pkBytes, { zip215: false });
182
208
  if (!valid) {
183
- throw new Error("Invalid signature");
209
+ throw new errors_1.CryptoError("Invalid signature", errors_1.CryptoErrorCode.AUTH_FAILED);
184
210
  }
185
- const payloadStr = Buffer.from(message).toString("utf8");
211
+ const payloadStr = (0, encoding_1.fromUtf8)(message);
186
212
  const payload = JSON.parse(payloadStr);
213
+ checkTimeClaims(payload, opts.now ?? new Date());
187
214
  return { payload, ...(footer ? { footer } : {}) };
188
215
  }
189
216
  exports.v4local = {
@@ -40,6 +40,7 @@ export type dataSign = {
40
40
  export type dataVerify = {
41
41
  date: Date;
42
42
  message: string;
43
+ signature?: string;
43
44
  verificationKeys: string | string[];
44
45
  };
45
46
  export type data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,EAAE,CAAC,EAAE,UAAU,GAAG,OAAO,CAOrE;AAqBD,qBAAa,YAAY;IACvB,OAAO,CAAC,GAAG,CAAa;IACxB,OAAO,CAAC,SAAS,CAAS;gBAYd,IAAI,EAAE,UAAU,GAAG,MAAM;IASrC,IAAI,MAAM,IAAI,MAAM,CAEnB;IAGD,IAAI,WAAW,IAAI,OAAO,CAEzB;IAcD,MAAM,IAAI,UAAU;IAsBpB,KAAK,IAAI,MAAM;IAuBf,OAAO,IAAI,IAAI;CAMhB;AAiBD,wBAAgB,UAAU,CAAC,MAAM,EAAE,UAAU,GAAG,IAAI,CAEnD"}
1
+ {"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAgCA,wBAAgB,eAAe,CAAC,CAAC,EAAE,UAAU,EAAE,CAAC,EAAE,UAAU,GAAG,OAAO,CAOrE;AAqBD,qBAAa,YAAY;IACvB,OAAO,CAAC,GAAG,CAAa;IACxB,OAAO,CAAC,SAAS,CAAS;gBAYd,IAAI,EAAE,UAAU,GAAG,MAAM;IASrC,IAAI,MAAM,IAAI,MAAM,CAEnB;IAGD,IAAI,WAAW,IAAI,OAAO,CAEzB;IAcD,MAAM,IAAI,UAAU;IAsBpB,KAAK,IAAI,MAAM;IAuBf,OAAO,IAAI,IAAI;CAMhB;AAiBD,wBAAgB,UAAU,CAAC,MAAM,EAAE,UAAU,GAAG,IAAI,CAEnD"}
package/dist/utils.js CHANGED
@@ -4,6 +4,7 @@ exports.SecureBuffer = void 0;
4
4
  exports.timingSafeEqual = timingSafeEqual;
5
5
  exports.wipeMemory = wipeMemory;
6
6
  const errors_1 = require("./errors");
7
+ const encoding_1 = require("./encoding");
7
8
  function timingSafeEqual(a, b) {
8
9
  if (a.length !== b.length)
9
10
  return false;
@@ -18,7 +19,7 @@ class SecureBuffer {
18
19
  destroyed = false;
19
20
  constructor(data) {
20
21
  if (typeof data === "string") {
21
- this.buf = Buffer.from(data, "hex");
22
+ this.buf = (0, encoding_1.decodeHex)(data);
22
23
  }
23
24
  else {
24
25
  this.buf = new Uint8Array(data);
@@ -40,7 +41,7 @@ class SecureBuffer {
40
41
  if (this.destroyed) {
41
42
  throw new errors_1.CryptoError("SecureBuffer has been destroyed", errors_1.CryptoErrorCode.BUFFER_DESTROYED);
42
43
  }
43
- return Buffer.from(this.buf).toString("hex");
44
+ return (0, encoding_1.toHex)(this.buf);
44
45
  }
45
46
  destroy() {
46
47
  if (!this.destroyed) {