nish 0.42.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 (125) hide show
  1. package/README.md +11 -5
  2. package/docs/AI.md +287 -129
  3. package/package.json +9 -9
  4. package/runtime/core/numbers.mjs +3 -1
  5. package/runtime/core/strings.mjs +90 -38
  6. package/runtime/core.d.ts +24 -0
  7. package/runtime/core.mjs +21 -0
  8. package/runtime/nish.d.ts +167 -24
  9. package/runtime/nish.mjs +7 -1
  10. package/runtime/runtime-cross.c +9 -0
  11. package/runtime/runtime-net.c +51 -13
  12. package/runtime/runtime-region.c +13 -0
  13. package/runtime/runtime-text.c +11 -0
  14. package/runtime/runtime-uring.c +83 -25
  15. package/runtime/runtime-wasm.c +3 -0
  16. package/runtime/runtime.c +329 -6
  17. package/runtime/sqlite/README.md +16 -3
  18. package/runtime/sqlite/nish-sqlite.c +940 -29
  19. package/scripts/build.sh +111 -29
  20. package/skills/writing-nish/SKILL.md +12 -4
  21. package/std/README.md +6 -6
  22. package/std/array.ts +10 -2
  23. package/std/bytes.ts +239 -26
  24. package/std/cli-args.ts +1072 -606
  25. package/std/cli-flags.ts +180 -98
  26. package/std/cli.ts +21 -13
  27. package/std/collections.ts +245 -127
  28. package/std/compress/checksum.ts +32 -37
  29. package/std/compress/deflate.ts +1102 -542
  30. package/std/compress/gzip.ts +290 -145
  31. package/std/compress/zlib.ts +57 -21
  32. package/std/crypto/aes.ts +153 -75
  33. package/std/crypto/base64url.ts +163 -39
  34. package/std/crypto/bcrypt-batch.ts +164 -90
  35. package/std/crypto/bcrypt.ts +185 -107
  36. package/std/crypto/chacha20poly1305.ts +84 -68
  37. package/std/crypto/ct.ts +9 -7
  38. package/std/crypto/ed25519.ts +64 -27
  39. package/std/crypto/hkdf.ts +105 -63
  40. package/std/crypto/hmac.ts +95 -89
  41. package/std/crypto/jwt.ts +248 -126
  42. package/std/crypto/p256.ts +758 -444
  43. package/std/crypto/p384.ts +20 -13
  44. package/std/crypto/pki-roots.ts +95 -38
  45. package/std/crypto/pki.ts +164 -60
  46. package/std/crypto/rsa.ts +266 -223
  47. package/std/crypto/scram.ts +141 -0
  48. package/std/crypto/sha1.ts +157 -57
  49. package/std/crypto/sha256.ts +502 -422
  50. package/std/crypto/sha512.ts +204 -135
  51. package/std/crypto/x509-random.ts +9 -3
  52. package/std/crypto/x509.ts +331 -171
  53. package/std/host/ring.ts +1 -1
  54. package/std/host/text.ts +1 -1
  55. package/std/json-equal.ts +35 -19
  56. package/std/json-format.ts +215 -108
  57. package/std/json-number.ts +126 -72
  58. package/std/json-pointer.ts +42 -13
  59. package/std/json-tokenizer.ts +498 -307
  60. package/std/json-typed.ts +115 -36
  61. package/std/json-value.ts +341 -152
  62. package/std/json.ts +300 -158
  63. package/std/net/buffer-pool.ts +17 -7
  64. package/std/net/dns-config.ts +427 -236
  65. package/std/net/dns-message.ts +114 -47
  66. package/std/net/dns-resolver.ts +736 -237
  67. package/std/net/dns.ts +205 -52
  68. package/std/net/fetch.ts +320 -184
  69. package/std/net/grpc.ts +948 -0
  70. package/std/net/handler.ts +1276 -218
  71. package/std/net/hpack.ts +395 -180
  72. package/std/net/http-compress.ts +502 -65
  73. package/std/net/http-fields.ts +85 -49
  74. package/std/net/http-server.ts +1764 -603
  75. package/std/net/http1-client-codec.ts +372 -161
  76. package/std/net/http1-codec.ts +665 -331
  77. package/std/net/http1-server.ts +881 -269
  78. package/std/net/http2-cleartext.ts +165 -95
  79. package/std/net/http2-frame.ts +232 -89
  80. package/std/net/http2-tls.ts +42 -12
  81. package/std/net/http2.ts +327 -101
  82. package/std/net/http3-frame.ts +68 -33
  83. package/std/net/http3-server.ts +672 -206
  84. package/std/net/http3.ts +669 -191
  85. package/std/net/oidc.ts +63 -24
  86. package/std/net/outbound.ts +207 -142
  87. package/std/net/postgres-async.ts +1223 -470
  88. package/std/net/postgres-carrier.ts +575 -372
  89. package/std/net/postgres.ts +1521 -632
  90. package/std/net/prefork.ts +254 -98
  91. package/std/net/protobuf.ts +1017 -0
  92. package/std/net/qpack.ts +143 -92
  93. package/std/net/quic-conn-ack.ts +104 -22
  94. package/std/net/quic-conn-cid.ts +66 -26
  95. package/std/net/quic-conn-params.ts +13 -7
  96. package/std/net/quic-datagram.ts +90 -27
  97. package/std/net/quic-frame.ts +324 -121
  98. package/std/net/quic-listener.ts +206 -91
  99. package/std/net/quic-packet.ts +289 -163
  100. package/std/net/quic-recovery.ts +279 -128
  101. package/std/net/quic-stream.ts +466 -167
  102. package/std/net/quic.ts +584 -258
  103. package/std/net/router.ts +450 -99
  104. package/std/net/serve.ts +661 -240
  105. package/std/net/static.ts +1458 -0
  106. package/std/net/tls/client.ts +140 -45
  107. package/std/net/tls/codec.ts +510 -260
  108. package/std/net/tls/record-client.ts +268 -76
  109. package/std/net/tls/record-server.ts +236 -79
  110. package/std/net/tls/record.ts +173 -47
  111. package/std/net/tls/schedule.ts +58 -39
  112. package/std/net/tls/ticket.ts +295 -103
  113. package/std/net/tls/verify.ts +1 -1
  114. package/std/net/tls-tcp.ts +218 -75
  115. package/std/net/tls.ts +260 -152
  116. package/std/net/upstream.ts +283 -86
  117. package/std/net/url.ts +277 -102
  118. package/std/net/websocket.ts +532 -209
  119. package/std/net/webtransport.ts +755 -436
  120. package/std/regex.ts +1659 -829
  121. package/std/sqlite.ts +325 -0
  122. package/std/sync/ring.ts +320 -156
  123. package/std/sync/shm.ts +53 -6
  124. package/std/text.ts +285 -135
  125. package/std/threads.ts +22 -2
@@ -0,0 +1,141 @@
1
+ /**
2
+ * `nish/crypto/scram` — the arithmetic of a SCRAM-SHA-256 client, as RFC 5802
3
+ * §3 defines it with SHA-256 for `H` and `HMAC` (RFC 7677 §3):
4
+ *
5
+ * SaltedPassword := Hi(Normalize(password), salt, i)
6
+ * ClientKey := HMAC(SaltedPassword, "Client Key")
7
+ * StoredKey := H(ClientKey)
8
+ * ClientSignature := HMAC(StoredKey, AuthMessage)
9
+ * ClientProof := ClientKey XOR ClientSignature
10
+ * ServerKey := HMAC(SaltedPassword, "Server Key")
11
+ * ServerSignature := HMAC(ServerKey, AuthMessage)
12
+ *
13
+ * import { scramSha256ClientProof, scramSha256SaltedPassword, scramSha256ServerSignature } from "nish/crypto/scram";
14
+ *
15
+ * const salted: Secret<u8[]> | null = scramSha256SaltedPassword(password, salt, 4096);
16
+ * if (salted !== null) {
17
+ * const proof: u8[] = scramSha256ClientProof(salted, authMessage);
18
+ * const expected: u8[] = scramSha256ServerSignature(salted, authMessage);
19
+ * wipe(salted);
20
+ * }
21
+ *
22
+ * The module holds none of SCRAM's messages: the caller builds AuthMessage,
23
+ * decodes the server's salt and encodes the proof, so that a driver keeps its
24
+ * own wire format and this file stays a pure function of its inputs, which a
25
+ * contract (`specs/scram-sha256.md`) can hold to the RFC's printed exchange.
26
+ * `Normalize` (SASLprep) is the caller's too: the password arrives as the
27
+ * bytes the caller decided to send.
28
+ *
29
+ * SaltedPassword, ClientKey and ServerKey each let whoever holds them log in
30
+ * as the user, so SaltedPassword is taken and given as a `Secret`, and
31
+ * ClientKey, StoredKey, ClientSignature and ServerKey are wiped before each
32
+ * function returns. ClientProof and ServerSignature cross the wire in the
33
+ * clear and are plain bytes. A caller compares the server's signature with
34
+ * `timingSafeEqual` (`nish/crypto/ct`), and caps the iteration count before
35
+ * it calls: `i=` is the server's to choose and Hi runs that many HMACs.
36
+ */
37
+
38
+ import { hmacSha256, pbkdf2HmacSha256 } from "nish/crypto/hmac"
39
+ import { Sha256 } from "nish/crypto/sha256"
40
+ import { Secret, exposeWith, wipe } from "nish:secret"
41
+
42
+ /** The bytes of an ASCII literal, for the two HMAC labels. */
43
+ const scramAscii = (text: string): u8[] => {
44
+ const n: i32 = toI32(text.length)
45
+ const out: u8[] = []
46
+ for (let i: i32 = 0; i < n; i++) {
47
+ out.push(toU8(text.charCodeAt(i)))
48
+ }
49
+ return out
50
+ }
51
+
52
+ /**
53
+ * SHA-256 of `data` through a hasher of its own, whose state, block and
54
+ * schedule are wiped before it returns: the one-shot `sha256` leaves the last
55
+ * block of its input in a hasher nothing wipes, and here the input is
56
+ * ClientKey.
57
+ */
58
+ const scramSha256Wiped = (data: u8[]): u8[] => {
59
+ const h = new Sha256()
60
+ h.update(data, toI32(0), toI32(data.length))
61
+ const digest: u8[] = h.digest()
62
+ wipe(h.state)
63
+ wipe(h.block)
64
+ wipe(h.schedule)
65
+ h.fill = 0
66
+ h.total = 0
67
+ h.finished = true
68
+ return digest
69
+ }
70
+
71
+ /** ClientKey XOR HMAC(H(ClientKey), AuthMessage), with every key on the way wiped. */
72
+ const scramClientProofExposed = (salted: u8[], authMessage: u8[]): u8[] => {
73
+ const clientKey: u8[] = hmacSha256(salted, scramAscii("Client Key"))
74
+ const storedKey: u8[] = scramSha256Wiped(clientKey)
75
+ const clientSignature: u8[] = hmacSha256(storedKey, authMessage)
76
+ // Both are HMAC-SHA-256 tags, 32 bytes each; the bounds below only let the
77
+ // prover see that, and never stop the loop early.
78
+ const proof: u8[] = new Array<u8>(32)
79
+ for (let i: i32 = 0; i < 32 && i < toI32(clientKey.length) && i < toI32(clientSignature.length); i++) {
80
+ proof[i] = clientKey[i] ^ clientSignature[i]
81
+ }
82
+ wipe(clientKey)
83
+ wipe(storedKey)
84
+ wipe(clientSignature)
85
+ return proof
86
+ }
87
+
88
+ /** HMAC(HMAC(SaltedPassword, "Server Key"), AuthMessage), with ServerKey wiped. */
89
+ const scramServerSignatureExposed = (salted: u8[], authMessage: u8[]): u8[] => {
90
+ const serverKey: u8[] = hmacSha256(salted, scramAscii("Server Key"))
91
+ const signature: u8[] = hmacSha256(serverKey, authMessage)
92
+ wipe(serverKey)
93
+ return signature
94
+ }
95
+
96
+ /**
97
+ * SaltedPassword, RFC 5802 §3's `Hi(password, salt, i)`: PBKDF2 with
98
+ * HMAC-SHA-256 and a 32-byte output (§2.2).
99
+ *
100
+ * The time is linear in `iterations` and nothing here bounds it, so a caller
101
+ * taking the count from a server caps it first.
102
+ *
103
+ * @param password The password, already normalized as the caller sends it;
104
+ * read only inside `exposeWith`, and still the caller's to wipe.
105
+ * @param salt The salt the server sent, decoded from base64.
106
+ * @param iterations The server's iteration count `i`; below 1 answers `null`.
107
+ * @returns SaltedPassword, which the caller wipes, or `null` when it cannot be
108
+ * computed.
109
+ */
110
+ export const scramSha256SaltedPassword = (
111
+ password: Secret<u8[]>,
112
+ salt: u8[],
113
+ iterations: i32
114
+ ): Secret<u8[]> | null => pbkdf2HmacSha256(password, salt, iterations, 32)
115
+
116
+ /**
117
+ * ClientProof, the `p=` of the client-final message (RFC 5802 §3). ClientKey,
118
+ * StoredKey and ClientSignature are wiped before it returns.
119
+ *
120
+ * @param saltedPassword SaltedPassword from `scramSha256SaltedPassword`, read
121
+ * only inside `exposeWith`; still the caller's to wipe.
122
+ * @param authMessage The client-first message without its GS2 header, the
123
+ * server-first message and the client-final message without its proof,
124
+ * joined by commas.
125
+ * @returns The 32-byte proof, which is sent in the clear.
126
+ */
127
+ export const scramSha256ClientProof = (saltedPassword: Secret<u8[]>, authMessage: u8[]): u8[] =>
128
+ exposeWith(saltedPassword, authMessage, scramClientProofExposed)
129
+
130
+ /**
131
+ * ServerSignature, what the server's `v=` must equal (RFC 5802 §3). ServerKey
132
+ * is wiped before it returns. Compare it to the server's with
133
+ * `timingSafeEqual`.
134
+ *
135
+ * @param saltedPassword SaltedPassword from `scramSha256SaltedPassword`, read
136
+ * only inside `exposeWith`; still the caller's to wipe.
137
+ * @param authMessage The same AuthMessage the proof was computed over.
138
+ * @returns The 32-byte signature the server should send.
139
+ */
140
+ export const scramSha256ServerSignature = (saltedPassword: Secret<u8[]>, authMessage: u8[]): u8[] =>
141
+ exposeWith(saltedPassword, authMessage, scramServerSignatureExposed)
@@ -15,7 +15,9 @@
15
15
  * const digest: u8[] = sha1(message); // 20 bytes
16
16
  *
17
17
  * One shot only, because a handshake key is a few dozen bytes: there is no
18
- * streaming hasher to keep in step with `Sha256`'s.
18
+ * streaming hasher to keep in step with `Sha256`'s. A caller that hashes again
19
+ * and again keeps a `Sha1` and calls `digestInto`, which writes into its
20
+ * buffer and allocates nothing.
19
21
  *
20
22
  * Every word is a `u32`, whose arithmetic wraps, so the additions modulo 2^32
21
23
  * are plain `+`. The only branches are on lengths, loop counters and the round
@@ -33,22 +35,21 @@ export const SHA1_BLOCK: i32 = 64
33
35
  /** ROTL^n(x) of FIPS 180-4 §3.2: a left rotation of a 32-bit word, `0 < n < 32`. */
34
36
  const sha1Rotl = (x: u32, n: u32): u32 => (x << n) | (x >>> (32 - n))
35
37
 
36
- /** The big-endian word at `buf[at .. at + 4)`, as FIPS 180-4 §3.1 orders bytes in a word. */
37
- const sha1LoadWord = (buf: u8[], at: i32): u32 =>
38
- (toU32(buf[at]) << 24) | (toU32(buf[at + 1]) << 16) | (toU32(buf[at + 2]) << 8) | toU32(buf[at + 3])
39
-
40
38
  /**
41
39
  * Folds the 64-byte block at `src[at .. at + 64)` into `h`: FIPS 180-4
42
- * §6.1.2, steps 1 to 4. `w` is the 80-word schedule, allocated once per call
43
- * to `sha1` rather than once per block.
40
+ * §6.1.2, steps 1 to 4. `w` is the 80-word schedule, held by the `Sha1`
41
+ * hasher rather than allocated once per block. Every caller passes a block
42
+ * inside `src` and the hasher's own `h` and `w`; the conditions below are the
43
+ * shapes that prove each index in range, not checks a caller can fail.
44
44
  */
45
45
  const sha1Compress = (h: u32[], w: u32[], src: u8[], at: i32): void => {
46
- // Written as the condition the body runs under, the shape that proves the
47
- // plain `w[t]` and `h[i]` indices in range (as `sha256Compress` does).
48
- if (toI32(h.length) >= 5 && toI32(w.length) >= 80 && at >= 0 && at <= toI32(src.length) - SHA1_BLOCK) {
46
+ if (toI32(h.length) >= 5 && toI32(w.length) >= 80) {
49
47
  // Step 1: the message schedule, big-endian words (§3.1), then (6.1.2).
50
- for (let t: i32 = 0; t < 16; t += 1) {
51
- w[t] = sha1LoadWord(src, at + 4 * t)
48
+ const n: i32 = toI32(src.length)
49
+ let p: i32 = at
50
+ for (let t: i32 = 0; t < 16 && p >= 0 && p < n - 3; t += 1) {
51
+ w[t] = (toU32(src[p]) << 24) | (toU32(src[p + 1]) << 16) | (toU32(src[p + 2]) << 8) | toU32(src[p + 3])
52
+ p += 4
52
53
  }
53
54
  for (let t: i32 = 16; t < 80; t += 1) {
54
55
  w[t] = sha1Rotl(w[t - 3] ^ w[t - 8] ^ w[t - 14] ^ w[t - 16], 1)
@@ -89,62 +90,161 @@ const sha1Compress = (h: u32[], w: u32[], src: u8[], at: i32): void => {
89
90
  h[2] += c
90
91
  h[3] += d
91
92
  h[4] += e
92
- } else {
93
- panic("sha1: a block outside its buffer")
93
+ }
94
+ }
95
+
96
+ /** Sets `h` to H^(0) of FIPS 180-4 §5.3.1. */
97
+ const sha1Begin = (h: u32[]): void => {
98
+ if (toI32(h.length) >= 5) {
99
+ h[0] = 0x67452301
100
+ h[1] = 0xefcdab89
101
+ h[2] = 0x98badcfe
102
+ h[3] = 0x10325476
103
+ h[4] = 0xc3d2e1f0
104
+ }
105
+ }
106
+
107
+ /**
108
+ * Finishes a digest whose last `tail` bytes (`0 <= tail < 64`) the caller has
109
+ * copied to `last[0 .. tail)`: pads them (FIPS 180-4 §5.1.1) with the message
110
+ * length of `bits` bits, folds the one or two padded blocks into `h`, and
111
+ * writes the digest to `out[at .. at + 20)`, which the caller has checked.
112
+ */
113
+ const sha1Finish = (h: u32[], w: u32[], last: u8[], tail: i32, bits: u64, out: u8[], at: i32): void => {
114
+ let lastLength: i32 = 64
115
+ if (tail >= 56) {
116
+ lastLength = 128
117
+ }
118
+ const room: i32 = toI32(last.length)
119
+ // The 0x80 that ends the message, then zeros, then the length big-endian.
120
+ let z: i32 = tail
121
+ while (z >= 0 && z < lastLength && z < room) {
122
+ last[z] = z === tail ? 0x80 : 0
123
+ z += 1
124
+ }
125
+ let k: i32 = lastLength - 8
126
+ let shift: u64 = 56
127
+ for (let i: i32 = 0; i < 8 && k >= 0 && k < room; i += 1) {
128
+ last[k] = toU8(bits >>> shift)
129
+ shift -= 8
130
+ k += 1
131
+ }
132
+ for (let off: i32 = 0; off < lastLength; off += 64) {
133
+ sha1Compress(h, w, last, off)
134
+ }
135
+ let p: i32 = at
136
+ for (let i: i32 = 0; i < 5 && i < toI32(h.length) && p >= 0 && p < toI32(out.length) - 3; i += 1) {
137
+ const word: u32 = h[i]
138
+ out[p] = toU8(word >>> 24)
139
+ out[p + 1] = toU8(word >>> 16)
140
+ out[p + 2] = toU8(word >>> 8)
141
+ out[p + 3] = toU8(word)
142
+ p += 4
143
+ }
144
+ }
145
+
146
+ /**
147
+ * A SHA-1 hasher that keeps its working state, so that a caller hashing again
148
+ * and again (a server answering WebSocket handshakes) allocates once, when it
149
+ * builds one, and never after: `digestInto` writes into the caller's buffer.
150
+ * It holds no secret, so nothing in it is wiped.
151
+ *
152
+ * const hasher = new Sha1();
153
+ * hasher.digestInto(message, 0, message.length, digest, 0); // digest[0 .. 20)
154
+ */
155
+ export class Sha1 {
156
+ /** The chaining value H of FIPS 180-4 §6.1.2, five words. */
157
+ h: u32[]
158
+ /** The 80-word message schedule, reused for every block. */
159
+ w: u32[]
160
+ /** The last one or two blocks: the message's tail and its padding (§5.1.1). */
161
+ last: u8[]
162
+
163
+ /** A hasher with its buffers allocated, ready for `digestInto`. */
164
+ constructor() {
165
+ this.h = new Array<u32>(5)
166
+ this.w = new Array<u32>(80)
167
+ this.last = new Array<u8>(128)
168
+ }
169
+
170
+ /**
171
+ * Writes the SHA-1 digest of `data[start .. start + length)` into
172
+ * `out[at .. at + 20)`, allocating nothing. A range outside `data` or `out`
173
+ * writes nothing and answers `false`.
174
+ *
175
+ * @param data The buffer holding the message; it is read, not changed.
176
+ * @param start Where in `data` the message starts.
177
+ * @param length How many bytes of `data` the message is.
178
+ * @param out The buffer the 20-byte digest is written into.
179
+ * @param at Where in `out` the digest starts.
180
+ * @returns `true` once the digest is written; `false`, with `out` untouched, when either range lies outside its buffer.
181
+ */
182
+ digestInto(data: u8[], start: i32, length: i32, out: u8[], at: i32): boolean {
183
+ // `toI32` of a length past 2^31 - 1 (under `--number-mode f64`) saturates,
184
+ // which only narrows the ranges accepted here: an `i32` range ends below it.
185
+ if (start < 0 || length < 0 || start > toI32(data.length) - length) {
186
+ return false
187
+ }
188
+ if (at < 0 || at > toI32(out.length) - SHA1_SIZE) {
189
+ return false
190
+ }
191
+ const h: u32[] = this.h
192
+ const w: u32[] = this.w
193
+ const last: u8[] = this.last
194
+ sha1Begin(h)
195
+ // Whole blocks straight from `data`, then the tail and its padding (§5.1.1)
196
+ // in one or two blocks of `last`.
197
+ const size: i32 = toI32(data.length)
198
+ let off: i32 = start
199
+ let left: i32 = length
200
+ while (left >= 64 && off >= 0 && off <= size - 64) {
201
+ sha1Compress(h, w, data, off)
202
+ off += 64
203
+ left -= 64
204
+ }
205
+ let k: i32 = 0
206
+ const room: i32 = toI32(last.length)
207
+ while (k < left && k < room && off >= 0 && off < size) {
208
+ last[k] = data[off]
209
+ k += 1
210
+ off += 1
211
+ }
212
+ sha1Finish(h, w, last, k, toU64(toI64(length)) << 3, out, at)
213
+ return true
94
214
  }
95
215
  }
96
216
 
97
217
  /**
98
218
  * The SHA-1 digest of all of `data`, as a fresh 20-byte array.
99
219
  *
100
- * An array longer than 2^31 - 1 bytes panics, for the reason `sha256` gives:
101
- * under `--number-mode f64`, `toI32` of a longer length saturates, and the
102
- * answer would be the digest of a prefix.
220
+ * It walks `data` with a `number` offset and copies each block into the
221
+ * hasher's own buffer, so an array longer than 2^31 - 1 bytes (possible under
222
+ * `--number-mode f64`) is hashed whole, never cut to a prefix.
103
223
  *
104
224
  * @param data The whole message; it is read, not changed.
105
225
  * @returns A fresh 20-byte digest.
106
226
  */
107
227
  export const sha1 = (data: u8[]): u8[] => {
108
- if (data.length > 2147483647) {
109
- panic("sha1: a message longer than 2^31 - 1 bytes")
110
- }
111
- const n: i32 = toI32(data.length)
112
- // H^(0) of FIPS 180-4 §5.3.1.
113
- const h: u32[] = new Array<u32>(5)
114
- h[0] = 0x67452301
115
- h[1] = 0xefcdab89
116
- h[2] = 0x98badcfe
117
- h[3] = 0x10325476
118
- h[4] = 0xc3d2e1f0
119
- const w: u32[] = new Array<u32>(80)
120
- // Whole blocks straight from `data`, then the tail and its padding (§5.1.1)
121
- // in one or two blocks of their own.
122
- const whole: i32 = n - (n % SHA1_BLOCK)
123
- for (let at: i32 = 0; at < whole; at += SHA1_BLOCK) {
124
- sha1Compress(h, w, data, at)
125
- }
126
- const tail: i32 = n - whole
127
- const tailBlocks: i32 = tail < SHA1_BLOCK - 8 ? 1 : 2
128
- const last: u8[] = new Array<u8>(tailBlocks * SHA1_BLOCK)
129
- const lastLength: i32 = toI32(last.length)
130
- for (let k: i32 = 0; k < tail && k < lastLength; k += 1) {
131
- last[k] = data[whole + k]
132
- }
133
- last[tail] = 0x80
134
- const bits: u64 = toU64(toI64(n)) << 3
135
- for (let i: i32 = 0; i < 8; i += 1) {
136
- last[lastLength - 1 - i] = toU8(bits >>> toU64(8 * i))
137
- }
138
- for (let at: i32 = 0; at < lastLength; at += SHA1_BLOCK) {
139
- sha1Compress(h, w, last, at)
140
- }
141
228
  const out: u8[] = new Array<u8>(SHA1_SIZE)
142
- for (let i: i32 = 0; i < 5; i += 1) {
143
- const word: u32 = h[i]
144
- out[4 * i] = toU8(word >>> 24)
145
- out[4 * i + 1] = toU8(word >>> 16)
146
- out[4 * i + 2] = toU8(word >>> 8)
147
- out[4 * i + 3] = toU8(word)
229
+ const hasher: Sha1 = new Sha1()
230
+ const h: u32[] = hasher.h
231
+ const w: u32[] = hasher.w
232
+ const last: u8[] = hasher.last
233
+ const room: i32 = toI32(last.length)
234
+ sha1Begin(h)
235
+ const n: number = data.length
236
+ let off: number = 0
237
+ while (true) {
238
+ let k: i32 = 0
239
+ while (k < SHA1_BLOCK && k < room && off >= 0 && off < n) {
240
+ last[k] = data[off]
241
+ k += 1
242
+ off += 1
243
+ }
244
+ if (k < SHA1_BLOCK) {
245
+ sha1Finish(h, w, last, k, toU64(toI64(n)) << 3, out, 0)
246
+ return out
247
+ }
248
+ sha1Compress(h, w, last, 0)
148
249
  }
149
- return out
150
250
  }