nish 0.44.0 → 0.45.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (111) hide show
  1. package/README.md +1 -1
  2. package/docs/AI.md +18 -16
  3. package/package.json +5 -5
  4. package/runtime/core/numbers.mjs +3 -1
  5. package/runtime/core/strings.mjs +90 -38
  6. package/runtime/core.d.ts +8 -0
  7. package/runtime/core.mjs +16 -0
  8. package/runtime/nish.d.ts +47 -5
  9. package/runtime/nish.mjs +4 -0
  10. package/std/README.md +3 -3
  11. package/std/array.ts +10 -2
  12. package/std/bytes.ts +239 -26
  13. package/std/cli-args.ts +1072 -606
  14. package/std/cli-flags.ts +180 -98
  15. package/std/cli.ts +21 -13
  16. package/std/collections.ts +245 -127
  17. package/std/compress/checksum.ts +32 -37
  18. package/std/compress/deflate.ts +1102 -542
  19. package/std/compress/gzip.ts +290 -145
  20. package/std/compress/zlib.ts +57 -21
  21. package/std/crypto/aes.ts +153 -75
  22. package/std/crypto/base64url.ts +84 -61
  23. package/std/crypto/bcrypt-batch.ts +164 -90
  24. package/std/crypto/bcrypt.ts +185 -107
  25. package/std/crypto/chacha20poly1305.ts +84 -68
  26. package/std/crypto/ct.ts +9 -7
  27. package/std/crypto/ed25519.ts +64 -27
  28. package/std/crypto/hkdf.ts +105 -63
  29. package/std/crypto/hmac.ts +95 -89
  30. package/std/crypto/jwt.ts +248 -126
  31. package/std/crypto/p256.ts +758 -444
  32. package/std/crypto/p384.ts +20 -13
  33. package/std/crypto/pki-roots.ts +95 -38
  34. package/std/crypto/pki.ts +164 -60
  35. package/std/crypto/rsa.ts +266 -223
  36. package/std/crypto/scram.ts +3 -4
  37. package/std/crypto/sha1.ts +107 -65
  38. package/std/crypto/sha256.ts +502 -422
  39. package/std/crypto/sha512.ts +204 -135
  40. package/std/crypto/x509-random.ts +9 -3
  41. package/std/crypto/x509.ts +331 -171
  42. package/std/json-equal.ts +35 -19
  43. package/std/json-format.ts +215 -108
  44. package/std/json-number.ts +126 -72
  45. package/std/json-pointer.ts +42 -13
  46. package/std/json-tokenizer.ts +498 -307
  47. package/std/json-typed.ts +115 -36
  48. package/std/json-value.ts +341 -152
  49. package/std/json.ts +300 -158
  50. package/std/net/buffer-pool.ts +17 -7
  51. package/std/net/dns-config.ts +427 -236
  52. package/std/net/dns-message.ts +114 -47
  53. package/std/net/dns-resolver.ts +736 -237
  54. package/std/net/dns.ts +205 -52
  55. package/std/net/fetch.ts +320 -184
  56. package/std/net/grpc.ts +948 -0
  57. package/std/net/handler.ts +1275 -217
  58. package/std/net/hpack.ts +395 -180
  59. package/std/net/http-compress.ts +502 -65
  60. package/std/net/http-fields.ts +85 -49
  61. package/std/net/http-server.ts +1764 -602
  62. package/std/net/http1-client-codec.ts +372 -161
  63. package/std/net/http1-codec.ts +665 -331
  64. package/std/net/http1-server.ts +660 -278
  65. package/std/net/http2-cleartext.ts +165 -95
  66. package/std/net/http2-frame.ts +232 -89
  67. package/std/net/http2-tls.ts +42 -12
  68. package/std/net/http2.ts +327 -101
  69. package/std/net/http3-frame.ts +68 -33
  70. package/std/net/http3-server.ts +647 -192
  71. package/std/net/http3.ts +667 -189
  72. package/std/net/oidc.ts +63 -24
  73. package/std/net/outbound.ts +207 -142
  74. package/std/net/postgres-async.ts +1165 -443
  75. package/std/net/postgres-carrier.ts +575 -372
  76. package/std/net/postgres.ts +1459 -559
  77. package/std/net/prefork.ts +254 -98
  78. package/std/net/protobuf.ts +1017 -0
  79. package/std/net/qpack.ts +143 -92
  80. package/std/net/quic-conn-ack.ts +50 -18
  81. package/std/net/quic-conn-cid.ts +66 -26
  82. package/std/net/quic-conn-params.ts +13 -7
  83. package/std/net/quic-datagram.ts +90 -27
  84. package/std/net/quic-frame.ts +225 -124
  85. package/std/net/quic-listener.ts +197 -89
  86. package/std/net/quic-packet.ts +289 -163
  87. package/std/net/quic-recovery.ts +279 -128
  88. package/std/net/quic-stream.ts +466 -167
  89. package/std/net/quic.ts +568 -257
  90. package/std/net/router.ts +450 -99
  91. package/std/net/serve.ts +651 -232
  92. package/std/net/static.ts +1458 -0
  93. package/std/net/tls/client.ts +140 -45
  94. package/std/net/tls/codec.ts +510 -260
  95. package/std/net/tls/record-client.ts +268 -76
  96. package/std/net/tls/record-server.ts +236 -79
  97. package/std/net/tls/record.ts +173 -47
  98. package/std/net/tls/schedule.ts +58 -39
  99. package/std/net/tls/ticket.ts +295 -103
  100. package/std/net/tls/verify.ts +1 -1
  101. package/std/net/tls-tcp.ts +218 -75
  102. package/std/net/tls.ts +260 -152
  103. package/std/net/upstream.ts +283 -86
  104. package/std/net/url.ts +277 -102
  105. package/std/net/websocket.ts +370 -186
  106. package/std/net/webtransport.ts +755 -436
  107. package/std/regex.ts +1659 -829
  108. package/std/sync/ring.ts +320 -156
  109. package/std/sync/shm.ts +53 -6
  110. package/std/text.ts +205 -124
  111. package/std/threads.ts +22 -2
@@ -157,6 +157,9 @@ const BCRYPT_STATE_WORDS: i32 = 1042
157
157
  /** The length of the setting, `$2b$NN$` and the 22 salt characters. */
158
158
  const BCRYPT_SETTING_LENGTH: i32 = 29
159
159
 
160
+ /** The 22 bcrypt base64 characters of a 16-byte salt. */
161
+ const BCRYPT_SALT_CHARS: i32 = 22
162
+
160
163
  /** What the hash needs besides the password, as one argument, since `exposeWith` hands on only one. */
161
164
  interface BcryptSetting {
162
165
  /** The salt as four big-endian words. */
@@ -222,12 +225,12 @@ const bcryptSextetOf = (c: i32): i32 => {
222
225
  }
223
226
 
224
227
  /**
225
- * `BF_encode`: the first `n` bytes of `src` as bcrypt base64, three bytes to
226
- * four characters, and a one- or two-byte tail to two or three.
228
+ * `BF_encode` of a 16-byte salt: its 22 bcrypt base64 characters, three bytes
229
+ * to four and the one-byte tail to two.
227
230
  */
228
- const bcryptEncode = (src: u8[], n: i32): u8[] => {
229
- const out: u8[] = new Array<u8>((n * 4 + 2) / 3)
230
- bcryptEncodeInto(out, 0, src, n)
231
+ const bcryptEncode = (src: u8[]): u8[] => {
232
+ const out: u8[] = new Array<u8>(BCRYPT_SALT_CHARS)
233
+ bcryptEncodeInto(out, 0, src, BCRYPT_SALT_SIZE)
231
234
  return out
232
235
  }
233
236
 
@@ -238,7 +241,7 @@ const bcryptEncode = (src: u8[], n: i32): u8[] => {
238
241
  const bcryptEncodeInto = (out: u8[], at: i32, src: u8[], n: i32): void => {
239
242
  let i: i32 = 0
240
243
  let o: i32 = at
241
- while (i + 2 < n && i + 2 < toI32(src.length) && o >= 0 && o + 3 < toI32(out.length)) {
244
+ while (i < toI32(src.length) - 2 && i + 2 < n && o >= 0 && o < toI32(out.length) - 3) {
242
245
  const c1: i32 = toI32(src[i])
243
246
  const c2: i32 = toI32(src[i + 1])
244
247
  const c3: i32 = toI32(src[i + 2])
@@ -249,10 +252,10 @@ const bcryptEncodeInto = (out: u8[], at: i32, src: u8[], n: i32): void => {
249
252
  i += 3
250
253
  o += 4
251
254
  }
252
- if (i >= 0 && i < n && i < toI32(src.length) && o >= 0 && o + 1 < toI32(out.length)) {
255
+ if (i >= 0 && i < n && i < toI32(src.length) && o >= 0 && o < toI32(out.length) - 1) {
253
256
  const c1: i32 = toI32(src[i])
254
257
  out[o] = bcryptCharOf(c1 >> 2)
255
- if (i + 1 < n && i + 1 < toI32(src.length) && o + 2 < toI32(out.length)) {
258
+ if (i < toI32(src.length) - 1 && i + 1 < n && o < toI32(out.length) - 2) {
256
259
  const c2: i32 = toI32(src[i + 1])
257
260
  out[o + 1] = bcryptCharOf(((c1 & 0x03) << 4) | (c2 >> 4))
258
261
  out[o + 2] = bcryptCharOf((c2 & 0x0f) << 2)
@@ -264,11 +267,12 @@ const bcryptEncodeInto = (out: u8[], at: i32, src: u8[], n: i32): void => {
264
267
 
265
268
  /** `src` copied into `dst` from `at`, as far as `dst` reaches. */
266
269
  const bcryptPut = (dst: u8[], at: i32, src: u8[]): void => {
267
- for (let k: i32 = 0; k < toI32(src.length); k++) {
268
- const o: i32 = at + k
269
- if (o >= 0 && o < toI32(dst.length)) {
270
+ let o: i32 = at
271
+ for (let k: i32 = 0; k < toI32(src.length) && o < toI32(dst.length); k++) {
272
+ if (o >= 0) {
270
273
  dst[o] = src[k]
271
274
  }
275
+ o += 1
272
276
  }
273
277
  }
274
278
 
@@ -279,8 +283,8 @@ const bcryptPut = (dst: u8[], at: i32, src: u8[]): void => {
279
283
  */
280
284
  const bcryptDecodeSalt = (text: string, at: i32): u32[] | null => {
281
285
  const sx: i32[] = new Array<i32>(22)
286
+ let p: i32 = at
282
287
  for (let k: i32 = 0; k < toI32(sx.length); k++) {
283
- const p: i32 = at + k
284
288
  if (p < 0 || p >= toI32(text.length)) {
285
289
  return null
286
290
  }
@@ -289,11 +293,12 @@ const bcryptDecodeSalt = (text: string, at: i32): u32[] | null => {
289
293
  return null
290
294
  }
291
295
  sx[k] = v
296
+ p += 1
292
297
  }
293
- const bytes: u8[] = new Array<u8>(BCRYPT_SALT_SIZE)
298
+ const bytes: u8[] = new Array<u8>(16)
294
299
  let s: i32 = 0
295
300
  let d: i32 = 0
296
- while (s + 3 < toI32(sx.length) && d + 2 < toI32(bytes.length)) {
301
+ while (s < toI32(sx.length) - 3 && d < toI32(bytes.length) - 2) {
297
302
  const c2: i32 = sx[s + 1]
298
303
  const c3: i32 = sx[s + 2]
299
304
  bytes[d] = toU8((sx[s] << 2) | ((c2 & 0x30) >> 4))
@@ -310,7 +315,7 @@ const bcryptDecodeSalt = (text: string, at: i32): u32[] | null => {
310
315
  const bcryptSaltWords = (bytes: u8[]): u32[] => {
311
316
  const words: u32[] = new Array<u32>(4)
312
317
  let w: i32 = 0
313
- for (let b: i32 = 0; b + 3 < toI32(bytes.length) && w < toI32(words.length); b += 4) {
318
+ for (let b: i32 = 0; b < toI32(bytes.length) - 3 && w < toI32(words.length); b += 4) {
314
319
  words[w] =
315
320
  (toU32(bytes[b]) << 24) | (toU32(bytes[b + 1]) << 16) | (toU32(bytes[b + 2]) << 8) | toU32(bytes[b + 3])
316
321
  w++
@@ -320,7 +325,7 @@ const bcryptSaltWords = (bytes: u8[]): u32[] => {
320
325
 
321
326
  /** `$2b$NN$` and the 22 salt characters for a cost and 16 salt bytes. */
322
327
  const bcryptSettingOf = (salt: u8[], cost: i32): u8[] => {
323
- const out: u8[] = new Array<u8>(BCRYPT_SETTING_LENGTH)
328
+ const out: u8[] = new Array<u8>(29)
324
329
  out[0] = 36
325
330
  out[1] = 50
326
331
  out[2] = 98
@@ -328,7 +333,7 @@ const bcryptSettingOf = (salt: u8[], cost: i32): u8[] => {
328
333
  out[4] = toU8(cost / 10 + 48)
329
334
  out[5] = toU8((cost % 10) + 48)
330
335
  out[6] = 36
331
- bcryptPut(out, 7, bcryptEncode(salt, BCRYPT_SALT_SIZE))
336
+ bcryptPut(out, 7, bcryptEncode(salt))
332
337
  return out
333
338
  }
334
339
 
@@ -364,8 +369,8 @@ const bcryptParse = (text: string): Result<BcryptParsed, string> => {
364
369
  if (salt === null) {
365
370
  return Err("bcrypt: the salt is not bcrypt base64")
366
371
  }
367
- const prefix: u8[] = new Array<u8>(BCRYPT_SETTING_LENGTH)
368
- for (let i: i32 = 0; i + 1 < toI32(prefix.length) && i < toI32(text.length); i++) {
372
+ const prefix: u8[] = new Array<u8>(29)
373
+ for (let i: i32 = 0; i < toI32(prefix.length) - 1 && i < toI32(text.length); i++) {
369
374
  prefix[i] = toU8(text.charCodeAt(i))
370
375
  }
371
376
  prefix[28] = bcryptCharOf(bcryptSextetOf(toI32(text.charCodeAt(28))) & 0x30)
@@ -425,7 +430,7 @@ const bcryptBody = (st: u32[]): void => {
425
430
  }
426
431
  let l: u32 = 0
427
432
  let r: u32 = 0
428
- for (let i: i32 = 0; i + 1 < toI32(st.length); i += 2) {
433
+ for (let i: i32 = 0; i < toI32(st.length) - 1; i += 2) {
429
434
  l ^= st[0]
430
435
  for (let n: i32 = 1; n < 17; n += 2) {
431
436
  const la: i32 = toI32(l >>> 24) & 0xff
@@ -487,7 +492,7 @@ const bcryptKeySchedule = (expanded: u32[], password: u8[], safety: u32): void =
487
492
  sign |= buggy & 0x80
488
493
  }
489
494
  // The next position, or 0 after a NUL: 1 if c is nonzero, else 0.
490
- p = (p + 1) * ((c | -c) >>> 31)
495
+ p = (q + 1) * ((c | -c) >>> 31)
491
496
  }
492
497
  diff |= correct ^ buggy
493
498
  expanded[i] = correct
@@ -664,8 +669,10 @@ const bcryptInitState = (st: u32[], at: i32): void => {
664
669
  if (at < 0) {
665
670
  return
666
671
  }
667
- for (let i: i32 = 0; i < toI32(init.length) && at + i < toI32(st.length); i++) {
668
- st[at + i] = init[i]
672
+ let o: i32 = at
673
+ for (let i: i32 = 0; i < toI32(init.length) && o < toI32(st.length); i++) {
674
+ st[o] = init[i]
675
+ o += 1
669
676
  }
670
677
  }
671
678
 
@@ -695,15 +702,25 @@ const bcryptXorKey = (st: u32[], expanded: u32[]): void => {
695
702
 
696
703
  /** The 18 P words at `at` XORed with the salt words, repeated: the second half of a cost round. */
697
704
  const bcryptXorSalt = (st: u32[], at: i32, s0: u32, s1: u32, s2: u32, s3: u32): void => {
698
- if (at < 0 || at + 17 >= toI32(st.length)) {
705
+ if (at < 0 || at >= toI32(st.length) - 17) {
699
706
  return
700
707
  }
701
- for (let i: i32 = 0; i + 3 < 16; i += 4) {
702
- st[at + i] ^= s0
703
- st[at + i + 1] ^= s1
704
- st[at + i + 2] ^= s2
705
- st[at + i + 3] ^= s3
706
- }
708
+ st[at] ^= s0
709
+ st[at + 1] ^= s1
710
+ st[at + 2] ^= s2
711
+ st[at + 3] ^= s3
712
+ st[at + 4] ^= s0
713
+ st[at + 5] ^= s1
714
+ st[at + 6] ^= s2
715
+ st[at + 7] ^= s3
716
+ st[at + 8] ^= s0
717
+ st[at + 9] ^= s1
718
+ st[at + 10] ^= s2
719
+ st[at + 11] ^= s3
720
+ st[at + 12] ^= s0
721
+ st[at + 13] ^= s1
722
+ st[at + 14] ^= s2
723
+ st[at + 15] ^= s3
707
724
  st[at + 16] ^= s0
708
725
  st[at + 17] ^= s1
709
726
  }
@@ -731,7 +748,7 @@ const bcryptExposed = (password: u8[], setting: BcryptSetting, out: u8[]): void
731
748
 
732
749
  // The salted expansion: each block is XORed with a salt half before it is
733
750
  // encrypted, the first half and the second in turn (`BF_crypt`'s two loops).
734
- for (let i: i32 = 0; i < BCRYPT_STATE_WORDS; i += 2) {
751
+ for (let i: i32 = 0; i < 1042 && lr.length >= 2; i += 2) {
735
752
  const odd: boolean = (i & 2) !== 0
736
753
  lr[0] ^= odd ? s2 : s0
737
754
  lr[1] ^= odd ? s3 : s1
@@ -764,6 +781,9 @@ const bcryptExposed = (password: u8[], setting: BcryptSetting, out: u8[]): void
764
781
 
765
782
  /** One pair of the magic string, encrypted 64 times under `st`, into `dst` at `at` as eight big-endian bytes. */
766
783
  const bcryptMagic = (st: u32[], lr: u32[], dst: u8[], at: i32, l: u32, r: u32): void => {
784
+ if (lr.length < 2) {
785
+ return
786
+ }
767
787
  lr[0] = l
768
788
  lr[1] = r
769
789
  for (let k: i32 = 0; k < 64; k++) {
@@ -787,6 +807,21 @@ const bcryptPutBlock = (dst: u8[], at: i32, l: u32, r: u32): void => {
787
807
  dst[at + 7] = toU8(r & 0xff)
788
808
  }
789
809
 
810
+ /**
811
+ * The `n` bytes of `bytes` from `at` as a string, a character each: a hash is
812
+ * ASCII, so this is `String.fromBytes`, whose range check no proof removes.
813
+ * It stops at the end of `bytes`.
814
+ */
815
+ const bcryptText = (bytes: u8[], at: i32, n: i32): string => {
816
+ const parts: string[] = []
817
+ let i: i32 = at
818
+ for (let k: i32 = 0; k < n && i >= 0 && i < toI32(bytes.length); k++) {
819
+ parts.push(String.fromCharCode(bytes[i]))
820
+ i += 1
821
+ }
822
+ return parts.join("")
823
+ }
824
+
790
825
  /**
791
826
  * The 60-byte hash of the plain password under `job`'s setting into `out`,
792
827
  * which the caller made and wipes: the 29-byte setting, then the digest.
@@ -853,7 +888,7 @@ export const bcryptHash = (password: Secret<u8[]>, salt: u8[], cost: i32): Resul
853
888
  const setting: BcryptSetting = { salt: bcryptSaltWords(salt), cost: cost, safety: toU32(0) }
854
889
  const job: BcryptJob = { setting: setting, prefix: bcryptSettingOf(salt, cost), hash: "" }
855
890
  const out: u8[] = exposeWith(password, job, bcryptHashExposed)
856
- return Ok(String.fromBytes(out, 0, BCRYPT_HASH_LENGTH))
891
+ return Ok(bcryptText(out, 0, BCRYPT_HASH_LENGTH))
857
892
  }
858
893
 
859
894
  /**
@@ -930,11 +965,10 @@ const bcryptBytesOf = (given: u8[], hash: string): void => {
930
965
 
931
966
  /** The most lanes a batch runs. */
932
967
  const BCRYPT_BATCH_LANES: i32 = 6
933
- /** Where the six expanded keys of 18 start in the batch state, after the six Blowfish states. */
934
- const BCRYPT_BATCH_KEYS: i32 = 6252
935
- /** Where the six safety words start, after the keys. */
936
- const BCRYPT_BATCH_SAFETY: i32 = 6360
937
- /** Words in the batch state: six Blowfish states, six expanded keys of 18 and six safety words. */
968
+ /**
969
+ * Words in the batch state: six Blowfish states of 1042, six expanded keys of
970
+ * 18 from word 6252, and six safety words from word 6360.
971
+ */
938
972
  const BCRYPT_BATCH_WORDS: i32 = 6366
939
973
 
940
974
  /**
@@ -954,13 +988,15 @@ interface BcryptBatch {
954
988
 
955
989
  /** The six states set to π and each slot's safety word stored, for `exposeWith` on the fresh batch state. */
956
990
  const bcryptInitN = (st: u32[], batch: BcryptBatch): i32 => {
957
- if (toI32(st.length) < BCRYPT_BATCH_WORDS) {
991
+ if (toI32(st.length) < 6366) {
958
992
  return 0
959
993
  }
960
994
  const safety: u32[] = batch.safety
961
- for (let k: i32 = 0; k < BCRYPT_BATCH_LANES; k++) {
962
- bcryptInitState(st, k * BCRYPT_STATE_WORDS)
963
- st[BCRYPT_BATCH_SAFETY + k] = k < toI32(safety.length) ? safety[k] : 0
995
+ for (let k: i32 = 0; k < 6; k++) {
996
+ st[6360 + k] = k < toI32(safety.length) ? safety[k] : 0
997
+ }
998
+ for (let k: i32 = 0; k < 6; k++) {
999
+ bcryptInitState(st, k * 1042)
964
1000
  }
965
1001
  return 0
966
1002
  }
@@ -975,7 +1011,7 @@ const bcryptKeyWord = (password: u8[], arg: i32): u32 => {
975
1011
  const safety: u32 = (arg & 32) !== 0 ? 0x10000 : 0
976
1012
  bcryptKeySchedule(expanded, password, safety)
977
1013
  const k: i32 = arg & 31
978
- const word: u32 = k < 19 ? expanded[k] : 0
1014
+ const word: u32 = k < toI32(expanded.length) ? expanded[k] : 0
979
1015
  wipe(expanded)
980
1016
  return word
981
1017
  }
@@ -987,21 +1023,29 @@ const bcryptKeyWord = (password: u8[], arg: i32): u32 => {
987
1023
  * function is exposing and a `Secret` cannot be handed into it otherwise.
988
1024
  */
989
1025
  const bcryptLoadLane = (st: u32[], password: Secret<u8[]>, slot: i32): i32 => {
990
- if (toI32(st.length) < BCRYPT_BATCH_WORDS) {
1026
+ if (toI32(st.length) < 6366) {
991
1027
  return 0
992
1028
  }
993
- if (slot < 0 || slot >= BCRYPT_BATCH_LANES) {
1029
+ if (slot < 0 || slot >= 6) {
994
1030
  return 0
995
1031
  }
996
- const at: i32 = slot * BCRYPT_STATE_WORDS
997
- const kept: i32 = slot * 18 + BCRYPT_BATCH_KEYS
998
- const flag: i32 = st[BCRYPT_BATCH_SAFETY + slot] !== 0 ? 32 : 0
999
- for (let k: i32 = 0; k < 18; k++) {
1032
+ const at: i32 = slot * 1042
1033
+ const flag: i32 = st[6360 + slot] !== 0 ? 32 : 0
1034
+ let p: i32 = at
1035
+ let kept: i32 = slot * 18 + 6252
1036
+ for (let k: i32 = 0; k < 18 && p < toI32(st.length) && kept < toI32(st.length); k++) {
1000
1037
  const word: u32 = exposeWith(password, k | flag, bcryptKeyWord)
1001
- st[at + k] ^= word
1002
- st[kept + k] = word
1038
+ if (p < toI32(st.length) && kept < toI32(st.length)) {
1039
+ st[p] ^= word
1040
+ st[kept] = word
1041
+ }
1042
+ p += 1
1043
+ kept += 1
1044
+ }
1045
+ const last: u32 = exposeWith(password, 18 | flag, bcryptKeyWord)
1046
+ if (at < toI32(st.length)) {
1047
+ st[at] ^= last
1003
1048
  }
1004
- st[at] ^= exposeWith(password, 18 | flag, bcryptKeyWord)
1005
1049
  return 0
1006
1050
  }
1007
1051
 
@@ -1573,7 +1617,7 @@ const bcryptBodyN = (st: u32[], width: i32): void => {
1573
1617
 
1574
1618
  /** `bcryptEncrypt` under the state at `at`: one block, `lr[0]` and `lr[1]` in place. */
1575
1619
  const bcryptEncryptAt = (st: u32[], at: i32, lr: u32[]): void => {
1576
- if (at < 0 || at + BCRYPT_STATE_WORDS > toI32(st.length)) {
1620
+ if (at < 0 || at > toI32(st.length) - 1042) {
1577
1621
  return
1578
1622
  }
1579
1623
  if (lr.length < 2) {
@@ -1581,17 +1625,19 @@ const bcryptEncryptAt = (st: u32[], at: i32, lr: u32[]): void => {
1581
1625
  }
1582
1626
  let l: u32 = lr[0] ^ st[at]
1583
1627
  let r: u32 = lr[1]
1584
- for (let n: i32 = 1; n < 17; n += 2) {
1628
+ let p: i32 = at + 1
1629
+ for (let n: i32 = 1; n < 17 && p < toI32(st.length) - 1; n += 2) {
1585
1630
  const la: i32 = toI32(l >>> 24) & 0xff
1586
1631
  const lb: i32 = toI32(l >>> 16) & 0xff
1587
1632
  const lc: i32 = toI32(l >>> 8) & 0xff
1588
1633
  const ld: i32 = toI32(l) & 0xff
1589
- r ^= st[at + n] ^ (((st[at + 18 + la] + st[at + 274 + lb]) ^ st[at + 530 + lc]) + st[at + 786 + ld])
1634
+ r ^= st[p] ^ (((st[at + 18 + la] + st[at + 274 + lb]) ^ st[at + 530 + lc]) + st[at + 786 + ld])
1590
1635
  const ra: i32 = toI32(r >>> 24) & 0xff
1591
1636
  const rb: i32 = toI32(r >>> 16) & 0xff
1592
1637
  const rc: i32 = toI32(r >>> 8) & 0xff
1593
1638
  const rd: i32 = toI32(r) & 0xff
1594
- l ^= st[at + n + 1] ^ (((st[at + 18 + ra] + st[at + 274 + rb]) ^ st[at + 530 + rc]) + st[at + 786 + rd])
1639
+ l ^= st[p + 1] ^ (((st[at + 18 + ra] + st[at + 274 + rb]) ^ st[at + 530 + rc]) + st[at + 786 + rd])
1640
+ p += 2
1595
1641
  }
1596
1642
  lr[0] = r ^ st[at + 17]
1597
1643
  lr[1] = l
@@ -1599,14 +1645,20 @@ const bcryptEncryptAt = (st: u32[], at: i32, lr: u32[]): void => {
1599
1645
 
1600
1646
  /** Each running slot's P XORed with its kept expanded key, as `bcryptXorKey`. */
1601
1647
  const bcryptXorKeyN = (st: u32[], width: i32): void => {
1602
- if (toI32(st.length) < BCRYPT_BATCH_WORDS) {
1648
+ if (toI32(st.length) < 6366) {
1603
1649
  return
1604
1650
  }
1605
- for (let k: i32 = 0; k < width && k < BCRYPT_BATCH_LANES; k++) {
1606
- const at: i32 = k * BCRYPT_STATE_WORDS
1607
- const kept: i32 = k * 18 + BCRYPT_BATCH_KEYS
1608
- for (let i: i32 = 0; i < 18; i++) {
1609
- st[at + i] ^= st[kept + i]
1651
+ for (let k: i32 = 0; k < width && k < 6; k++) {
1652
+ let p: i32 = k * 1042
1653
+ let kept: i32 = k * 18 + 6252
1654
+ for (
1655
+ let i: i32 = 0;
1656
+ i < 18 && p >= 0 && p < toI32(st.length) && kept >= 0 && kept < toI32(st.length);
1657
+ i++
1658
+ ) {
1659
+ st[p] ^= st[kept]
1660
+ p += 1
1661
+ kept += 1
1610
1662
  }
1611
1663
  }
1612
1664
  }
@@ -1616,15 +1668,10 @@ const bcryptXorSaltN = (st: u32[], salts: u32[], width: i32): void => {
1616
1668
  if (salts.length < 24) {
1617
1669
  return
1618
1670
  }
1619
- for (let k: i32 = 0; k < width && k < BCRYPT_BATCH_LANES; k++) {
1620
- bcryptXorSalt(
1621
- st,
1622
- k * BCRYPT_STATE_WORDS,
1623
- salts[k * 4],
1624
- salts[k * 4 + 1],
1625
- salts[k * 4 + 2],
1626
- salts[k * 4 + 3]
1627
- )
1671
+ let s: i32 = 0
1672
+ for (let k: i32 = 0; k < width && k < 6 && s >= 0 && s < toI32(salts.length) - 3; k++) {
1673
+ bcryptXorSalt(st, k * 1042, salts[s], salts[s + 1], salts[s + 2], salts[s + 3])
1674
+ s += 4
1628
1675
  }
1629
1676
  }
1630
1677
 
@@ -1634,20 +1681,30 @@ const bcryptXorSaltN = (st: u32[], salts: u32[], width: i32): void => {
1634
1681
  * written over P and the S-boxes.
1635
1682
  */
1636
1683
  const bcryptExpandAt = (st: u32[], at: i32, salts: u32[], k: i32, lr: u32[]): void => {
1637
- if (lr.length < 2 || 4 * k + 3 >= toI32(salts.length)) {
1684
+ if (lr.length < 2 || k < 0 || k > 5 || 4 * k > toI32(salts.length) - 4) {
1638
1685
  return
1639
1686
  }
1687
+ if (at < 0 || at > toI32(st.length) - 1042) {
1688
+ return
1689
+ }
1690
+ const s: i32 = 4 * k
1691
+ const w0: u32 = salts[s]
1692
+ const w1: u32 = salts[s + 1]
1693
+ const w2: u32 = salts[s + 2]
1694
+ const w3: u32 = salts[s + 3]
1640
1695
  lr[0] = 0
1641
1696
  lr[1] = 0
1642
- for (let i: i32 = 0; i < BCRYPT_STATE_WORDS; i += 2) {
1643
- const half: i32 = (i & 2) !== 0 ? 2 : 0
1644
- lr[0] ^= salts[4 * k + half]
1645
- lr[1] ^= salts[4 * k + half + 1]
1697
+ let p: i32 = at
1698
+ for (let i: i32 = 0; i < 1042; i += 2) {
1699
+ const odd: boolean = (i & 2) !== 0
1700
+ lr[0] ^= odd ? w2 : w0
1701
+ lr[1] ^= odd ? w3 : w1
1646
1702
  bcryptEncryptAt(st, at, lr)
1647
- if (at + i + 1 < toI32(st.length)) {
1648
- st[at + i] = lr[0]
1649
- st[at + i + 1] = lr[1]
1703
+ if (p >= 0 && p < toI32(st.length) - 1) {
1704
+ st[p] = lr[0]
1705
+ st[p + 1] = lr[1]
1650
1706
  }
1707
+ p += 2
1651
1708
  }
1652
1709
  }
1653
1710
 
@@ -1671,12 +1728,12 @@ const bcryptMagicAt = (st: u32[], at: i32, lr: u32[], dst: u8[], to: i32, l: u32
1671
1728
  * caller made and wipes. The block and digest copies are wiped here.
1672
1729
  */
1673
1730
  const bcryptEksN = (st: u32[], salts: u32[], cost: i32, width: i32, digests: u8[]): void => {
1674
- if (toI32(st.length) < BCRYPT_BATCH_WORDS || salts.length < 24 || width < 2 || width > BCRYPT_BATCH_LANES) {
1731
+ if (toI32(st.length) < 6366 || salts.length < 24 || width < 2 || width > 6) {
1675
1732
  return
1676
1733
  }
1677
1734
  const lr: u32[] = new Array<u32>(2)
1678
- for (let k: i32 = 0; k < width; k++) {
1679
- bcryptExpandAt(st, k * BCRYPT_STATE_WORDS, salts, k, lr)
1735
+ for (let k: i32 = 0; k < width && k < 6; k++) {
1736
+ bcryptExpandAt(st, k * 1042, salts, k, lr)
1680
1737
  }
1681
1738
 
1682
1739
  let rounds: u32 = toU32(1) << toU32(cost)
@@ -1689,8 +1746,8 @@ const bcryptEksN = (st: u32[], salts: u32[], cost: i32, width: i32, digests: u8[
1689
1746
  }
1690
1747
 
1691
1748
  const digest: u8[] = new Array<u8>(24)
1692
- for (let k: i32 = 0; k < width; k++) {
1693
- const at: i32 = k * BCRYPT_STATE_WORDS
1749
+ for (let k: i32 = 0; k < width && k < 6; k++) {
1750
+ const at: i32 = k * 1042
1694
1751
  bcryptMagicAt(st, at, lr, digest, 0, 0x4f727068, 0x65616e42)
1695
1752
  bcryptMagicAt(st, at, lr, digest, 8, 0x65686f6c, 0x64657253)
1696
1753
  bcryptMagicAt(st, at, lr, digest, 16, 0x63727944, 0x6f756274)
@@ -1707,11 +1764,11 @@ const bcryptEksN = (st: u32[], salts: u32[], cost: i32, width: i32, digests: u8[
1707
1764
  * are wiped here.
1708
1765
  */
1709
1766
  const bcryptHashNExposed = (st: u32[], batch: BcryptBatch): u8[] => {
1710
- const out: u8[] = new Array<u8>(BCRYPT_BATCH_LANES * BCRYPT_HASH_LENGTH)
1711
- const digests: u8[] = new Array<u8>(31 * BCRYPT_BATCH_LANES)
1767
+ const out: u8[] = new Array<u8>(360)
1768
+ const digests: u8[] = new Array<u8>(186)
1712
1769
  bcryptEksN(st, batch.salts, batch.cost, batch.width, digests)
1713
- for (let k: i32 = 0; k < batch.width && k < toI32(batch.prefixes.length); k++) {
1714
- bcryptJoin(out, BCRYPT_HASH_LENGTH * k, batch.prefixes[k], digests, k)
1770
+ for (let k: i32 = 0; k < batch.width && k < 6 && k < toI32(batch.prefixes.length); k++) {
1771
+ bcryptJoin(out, 60 * k, batch.prefixes[k], digests, k)
1715
1772
  }
1716
1773
  wipe(digests)
1717
1774
  return out
@@ -1723,7 +1780,7 @@ const bcryptHashNExposed = (st: u32[], batch: BcryptBatch): u8[] => {
1723
1780
  * and wiped here, so the bits are all that leaves.
1724
1781
  */
1725
1782
  const bcryptVerifyNExposed = (st: u32[], batch: BcryptBatch): i32 => {
1726
- const digests: u8[] = new Array<u8>(31 * BCRYPT_BATCH_LANES)
1783
+ const digests: u8[] = new Array<u8>(186)
1727
1784
  bcryptEksN(st, batch.salts, batch.cost, batch.width, digests)
1728
1785
  let same: i32 = 0
1729
1786
  for (
@@ -1775,8 +1832,12 @@ const bcryptLoadN = (
1775
1832
  }
1776
1833
 
1777
1834
  /** Slot k's 60-byte hash out of the batch's `all`, as a string. */
1778
- const bcryptLaneString = (all: u8[], k: i32): string =>
1779
- String.fromBytes(all, BCRYPT_HASH_LENGTH * k, BCRYPT_HASH_LENGTH * (k + 1))
1835
+ const bcryptLaneString = (all: u8[], k: i32): string => {
1836
+ if (k < 0 || k >= 6) {
1837
+ return ""
1838
+ }
1839
+ return bcryptText(all, k * 60, 60)
1840
+ }
1780
1841
 
1781
1842
  /** Bit k set for each lane given: lane 0 always, lanes 1–5 when not null. */
1782
1843
  const bcryptMaskOf = (
@@ -1883,7 +1944,11 @@ export const bcryptHashN = (
1883
1944
  // Slot s ran lane lanes[s]; a lane not given stays "".
1884
1945
  const out: string[] = ["", "", "", "", "", ""]
1885
1946
  for (let s: i32 = 0; s < toI32(lanes.length); s++) {
1886
- out[lanes[s]] = bcryptLaneString(all, s)
1947
+ const lane: i32 = lanes[s]
1948
+ const text: string = bcryptLaneString(all, s)
1949
+ if (lane >= 0 && lane < toI32(out.length)) {
1950
+ out[lane] = text
1951
+ }
1887
1952
  }
1888
1953
  return Ok(out)
1889
1954
  }
@@ -1934,8 +1999,12 @@ export const bcryptHash4 = (
1934
1999
  const bcryptBatchSalt = (words: u32[], salts: u8[], k: i32, s: i32, cost: i32): u8[] => {
1935
2000
  const salt: u8[] = bcryptSaltOf(salts, k)
1936
2001
  const four: u32[] = bcryptSaltWords(salt)
1937
- for (let j: i32 = 0; j < 4 && j < toI32(four.length) && 4 * s + j < toI32(words.length); j++) {
1938
- words[4 * s + j] = four[j]
2002
+ if (s >= 0 && s < 6) {
2003
+ let w: i32 = 4 * s
2004
+ for (let j: i32 = 0; j < 4 && j < toI32(four.length) && w < toI32(words.length); j++) {
2005
+ words[w] = four[j]
2006
+ w += 1
2007
+ }
1939
2008
  }
1940
2009
  return bcryptSettingOf(salt, cost)
1941
2010
  }
@@ -1943,8 +2012,13 @@ const bcryptBatchSalt = (words: u32[], salts: u8[], k: i32, s: i32, cost: i32):
1943
2012
  /** The 16 salt bytes of lane `k` from the bytes of a batch. */
1944
2013
  const bcryptSaltOf = (salts: u8[], k: i32): u8[] => {
1945
2014
  const salt: u8[] = new Array<u8>(16)
1946
- for (let j: i32 = 0; j < 16 && 16 * k + j < toI32(salts.length); j++) {
1947
- salt[j] = salts[16 * k + j]
2015
+ if (k < 0 || k >= 6) {
2016
+ return salt
2017
+ }
2018
+ let i: i32 = 16 * k
2019
+ for (let j: i32 = 0; j < 16 && i < toI32(salts.length); j++) {
2020
+ salt[j] = salts[i]
2021
+ i += 1
1948
2022
  }
1949
2023
  return salt
1950
2024
  }
@@ -1952,13 +2026,15 @@ const bcryptSaltOf = (salts: u8[], k: i32): u8[] => {
1952
2026
  /** The 60 bytes of slot `k`'s hash into `out` at `at`: its 29-byte setting, then its 31 digest characters from the batch's. */
1953
2027
  const bcryptJoin = (out: u8[], at: i32, prefix: u8[], digests: u8[], k: i32): void => {
1954
2028
  bcryptPut(out, at, prefix)
1955
- const o: i32 = at + BCRYPT_SETTING_LENGTH
1956
- for (
1957
- let j: i32 = 0;
1958
- j < 31 && 31 * k + j < toI32(digests.length) && o + j >= 0 && o + j < toI32(out.length);
1959
- j++
1960
- ) {
1961
- out[o + j] = digests[31 * k + j]
2029
+ if (at < 0 || at > toI32(out.length) - 29 || k < 0 || k >= 6) {
2030
+ return
2031
+ }
2032
+ let o: i32 = at + 29
2033
+ let d: i32 = 31 * k
2034
+ for (let j: i32 = 0; j < 31 && d < toI32(digests.length) && o < toI32(out.length); j++) {
2035
+ out[o] = digests[d]
2036
+ o += 1
2037
+ d += 1
1962
2038
  }
1963
2039
  }
1964
2040
 
@@ -2137,7 +2213,7 @@ const bcryptVerifyGroup = (
2137
2213
  ok: boolean[]
2138
2214
  ): void => {
2139
2215
  const words: u32[] = new Array<u32>(24)
2140
- const safety: u32[] = new Array<u32>(BCRYPT_BATCH_LANES)
2216
+ const safety: u32[] = new Array<u32>(6)
2141
2217
  const prefixes: u8[][] = []
2142
2218
  // Copies, so no callee is handed the caller's `hashes` (the thread rule).
2143
2219
  const given: string[] = []
@@ -2146,14 +2222,16 @@ const bcryptVerifyGroup = (
2146
2222
  let cost: i32 = 0
2147
2223
  for (const k of bcryptSlotsOf(group)) {
2148
2224
  const s: i32 = toI32(slots.length)
2149
- if (s < toI32(safety.length) && k >= 0 && k < toI32(lanes.length) && k < toI32(hashes.length)) {
2225
+ if (s < 6 && s < toI32(safety.length) && k >= 0 && k < toI32(lanes.length) && k < toI32(hashes.length)) {
2150
2226
  const lane: BcryptParsed = lanes[k]
2151
2227
  const hash: string = hashes[k]
2152
2228
  const setting: BcryptSetting = lane.setting
2153
2229
  cost = setting.cost
2154
2230
  safety[s] = setting.safety
2155
- for (let j: i32 = 0; j < 4 && j < toI32(setting.salt.length); j++) {
2156
- words[4 * s + j] = setting.salt[j]
2231
+ let w: i32 = 4 * s
2232
+ for (let j: i32 = 0; j < 4 && j < toI32(setting.salt.length) && w < toI32(words.length); j++) {
2233
+ words[w] = setting.salt[j]
2234
+ w += 1
2157
2235
  }
2158
2236
  given.push(hash)
2159
2237
  prefixes.push(lane.prefix)
@@ -2237,7 +2315,7 @@ const bcryptCalibrateTime = (width: i32): i64 => {
2237
2315
  export const bcryptCalibrate = (): i32 => {
2238
2316
  let best: i32 = 1
2239
2317
  let bestTime: i64 = bcryptCalibrateTime(1)
2240
- for (let w: i32 = 2; w <= BCRYPT_BATCH_LANES; w++) {
2318
+ for (let w: i32 = 2; w <= 6; w++) {
2241
2319
  const took: i64 = bcryptCalibrateTime(w)
2242
2320
  // took / w < bestTime / best, without dividing.
2243
2321
  if (took * toI64(best) < bestTime * toI64(w)) {