GitHubivarvong
ply
main
1// Sha1 (src/sha1.ts) against node:crypto: every length 0..300 (every padding case: the 55/56/63/64-byte edges and the
2// two-block tail), lengths around each power of two to 1 MiB, inputs fed in random chunks (the buffered path at every
3// offset), bytes chosen to set the high bit of each schedule word and state word, and digest/hex/toOid agreement.
4import { test } from 'vitest';
5import assert from 'node:assert/strict';
6import { createHash } from 'node:crypto';
7import { Sha1, toHex, streamingSha1 } from '../src/sha1.js';
9const want = b => createHash('sha1').update(b).digest('hex');
10function bytes(n, seed) {
11 let s = seed >>> 0 || 1;
12 const out = new Uint8Array(n);
13 for (let i = 0; i < n; i++) { s ^= s << 13; s >>>= 0; s ^= s >>> 17; s ^= s << 5; s >>>= 0; out[i] = s; }
14 return out;
15}
17test('every length 0..300, random and all-0xff bytes, whole', () => {
18 for (let n = 0; n <= 300; n++) {
19 for (const b of [bytes(n, n + 1), new Uint8Array(n).fill(0xff), new Uint8Array(n)]) {
20 assert.equal(new Sha1().update(b).hex(), want(b), `length ${n}`);
21 assert.equal(toHex(new Sha1().update(b).digest()), want(b), `digest, length ${n}`);
22 }
23 }
24});
26test('lengths around powers of two to 1 MiB, fed in random chunks', () => {
27 let seed = 7;
28 for (let p = 6; p <= 20; p++) {
29 for (const n of [(1 << p) - 1, 1 << p, (1 << p) + 1]) {
30 const b = bytes(n, seed++);
31 const h = new Sha1();
32 let o = 0, r = seed;
33 while (o < n) { r = (r * 1103515245 + 12345) >>> 0; const k = Math.min(n - o, r % 200); h.update(b.subarray(o, o + k)); o += k; }
34 assert.equal(h.hex(), want(b), `length ${n}`);
35 }
36 }
37});
39test('the streaming hasher (pure-JS path under node) agrees', async () => {
40 const b = bytes(100_000, 3);
41 const s = streamingSha1();
42 for (let o = 0; o < b.length; o += 777) await s.update(b.subarray(o, o + 777));
43 assert.equal(await s.hex(), want(b));
44});
46// digest() and hex() are snapshots (a running pack checksum reads the hash so far and keeps hashing): called any number
47// of times they give the SHA-1 of the bytes fed so far, and update() continues from there. The expected value is
48// node:crypto's over the same prefix, never the hasher's own earlier answer. Before this held, digest() padded the live
49// state, so a second call hashed the message plus its padding: for 'abc', ec7a3886... where SHA-1 is a9993e36....
50test('digest/hex are non-destructive snapshots: twice in a row, at random points of a stream, updates continuing', () => {
51 // FIPS 180-1 appendix A's vector, so the first cell needs no oracle at all
52 const abc = new Sha1().update(new TextEncoder().encode('abc'));
53 assert.equal(abc.hex(), 'a9993e364706816aba3e25717850c26c9cd0d89d');
54 assert.equal(abc.hex(), 'a9993e364706816aba3e25717850c26c9cd0d89d', 'the second hex() of the same input');
55 assert.equal(toHex(abc.digest()), 'a9993e364706816aba3e25717850c26c9cd0d89d', 'digest() after two hex()');
56 // every buffered length 0..130 (both padding shapes, the block edges): digest twice, then keep hashing
57 for (let n = 0; n <= 130; n++) {
58 const b = bytes(n + 70, n + 11);
59 const h = new Sha1().update(b.subarray(0, n));
60 assert.equal(h.hex(), want(b.subarray(0, n)), `first, length ${n}`);
61 assert.equal(toHex(h.digest()), want(b.subarray(0, n)), `second, length ${n}`);
62 assert.equal(h.update(b.subarray(n)).hex(), want(b), `update after digest, length ${n}`);
63 }
64 // random streams: random chunk sizes, a snapshot (sometimes two) at random points, against node:crypto of the prefix
65 let r = 0x9e3779b9;
66 const next = () => { r ^= r << 13; r >>>= 0; r ^= r >>> 17; r ^= r << 5; r >>>= 0; return r; };
67 for (let round = 0; round < 200; round++) {
68 const n = next() % 5000;
69 const b = bytes(n, round + 1);
70 const h = new Sha1();
71 let o = 0, snaps = 0;
72 while (o < n) {
73 const k = Math.min(n - o, next() % 300);
74 h.update(b.subarray(o, o + k));
75 o += k;
76 if (next() % 3 === 0) {
77 snaps++;
78 assert.equal(h.hex(), want(b.subarray(0, o)), `round ${round}: snapshot at ${o} of ${n}`);
79 if (next() % 2 === 0) assert.equal(toHex(h.digest()), want(b.subarray(0, o)), `round ${round}: again at ${o}`);
80 }
81 }
82 assert.equal(h.hex(), want(b), `round ${round}: the whole input after ${snaps} snapshots`);
83 assert.equal(h.hex(), want(b), `round ${round}: and once more`);
84 }
85});
87test('the streaming hasher\'s hex() can be awaited twice (pure-JS path)', async () => {
88 const b = bytes(1000, 5);
89 const s = streamingSha1();
90 await s.update(b);
91 assert.equal(await s.hex(), want(b));
92 assert.equal(await s.hex(), want(b));
93});
95// The native path (Workers' crypto.DigestStream), which node does not have: a stand-in with DigestStream's shape (a
96// WritableStream whose `digest` promise settles on close) backed by node:crypto, so the expected value is still
97// node:crypto's. Closing a closed writer rejects, which is what a second hex() used to do.
98test('the streaming hasher\'s hex() can be awaited twice (native DigestStream path)', async () => {
99 class FakeDigestStream extends WritableStream {
100 constructor(algorithm) {
101 const h = createHash(algorithm.replace('-', '').toLowerCase());
102 let settle;
103 const digest = new Promise(res => { settle = res; });
104 super({ write(chunk) { h.update(chunk); }, close() { const d = h.digest(); settle(d.buffer.slice(d.byteOffset, d.byteOffset + d.length)); } });
105 this.digest = digest;
106 }
107 }
108 Object.defineProperty(globalThis.crypto, 'DigestStream', { value: FakeDigestStream, configurable: true });
109 try {
110 const b = bytes(5000, 9);
111 const s = streamingSha1();
112 assert.equal(s.kind, 'digeststream');
113 await s.update(b.subarray(0, 1234));
114 await s.update(b.subarray(1234));
115 const first = s.hex(), second = s.hex();
116 assert.equal(await first, want(b));
117 assert.equal(await second, want(b));
118 assert.equal(await s.hex(), want(b), 'and after both settled');
119 } finally {
120 delete globalThis.crypto.DigestStream;
121 }
122 assert.equal(streamingSha1().kind, 'js', 'the stand-in is gone');
123});
Diagnostics6 git calls · 262 ms
CallStatusSizeTime0262 ms
discover200191 B2 ms
git.upload-pack discover http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=2 git.response.bytes=191 git.server.agent=git/github-9922ec520325-Linux git.server.object_format=sha1 git.server.caps=agent ls-refs fetch server-option object-format git.server.fetch=shallow wait-for-done filter git.protocol.version=2 git.pkt.frames=9 git.pkt.data=7 git.pkt.flush=2 git.pkt.delim=0 git.pkt.bytes=191 git.pkt.payload.max=36
ls-refs20012 KiB · hit3 ms
git.upload-pack ls-refs http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=3 git.response.bytes=11990 git.response.cache=HIT git.response.duration_ms=3 git.protocol.version=2 git.pkt.frames=152 git.pkt.data=151 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=11990 git.pkt.payload.max=122 git.ls_refs.count=151 git.ls_refs.head=refs/heads/main
fetchtree:0 · deepen 102004.2 KiB · hit2 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=2 git.response.bytes=4347 git.response.cache=HIT git.response.duration_ms=2 git.fetch.filter=tree:0 git.fetch.wants=19912563f540228593df0c489082e5e2508e21e4 git.fetch.haves.count=0 git.fetch.deepen=10 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=7 git.pkt.data=5 git.pkt.flush=1 git.pkt.delim=1 git.pkt.bytes=4347 git.pkt.payload.max=4247 git.sideband.band1.bytes=4247 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=10 git.pack.bytes=4247 git.pack.ofs_delta=0 git.pack.ref_delta=0 git.pack.chain.max=0 git.pack.trailer=a6caf8e129bea82c9bae4cbc7c130c9205add400 git.pack.full=10Open pack map
fetchtree:0200919 B · hit3 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=3 git.response.bytes=919 git.response.cache=HIT git.response.duration_ms=3 git.fetch.filter=tree:0 git.fetch.wants=800d30ba8923860bfc460ca884b7f9ba22083588 git.fetch.haves.count=0 git.fetch.deepen=0 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=4 git.pkt.data=3 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=919 git.pkt.payload.max=892 git.sideband.band1.bytes=892 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=892 git.pack.trailer=5323b9266eea5ded637285d1c5320699f12be76eOpen pack map
fetchtree:02005.6 KiB · hit2 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=2 git.response.bytes=5724 git.response.cache=HIT git.response.duration_ms=2 git.fetch.filter=tree:0 git.fetch.wants=58fabb6768b3f737ee9cd8bac647bdc27405fb4d git.fetch.haves.count=0 git.fetch.deepen=0 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=4 git.pkt.data=3 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=5724 git.pkt.payload.max=5697 git.sideband.band1.bytes=5697 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=5697 git.pack.trailer=be7e0453974a5fe832f9065a33bb7e3434c10e9bOpen pack map
fetch2002.3 KiB252 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=252 git.response.bytes=2335 git.response.cache=MISS git.response.duration_ms=252 git.fetch.wants=614a496ac8e0a35f7e9adfc6f8bccbd44dc1aa76 git.fetch.haves.count=0 git.fetch.deepen=0 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=4 git.pkt.data=3 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=2335 git.pkt.payload.max=2308 git.sideband.band1.bytes=2308 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0
Each row is one HTTP round trip ply made to the git server for this page, on one timeline. discover asks what the server speaks, ls-refs lists branch tips, fetch brings objects by id. A hit was served by Workers Cache. Tap a row for its wire details.
- Remote
- https://github.com/ivarvong/ply
- Ref
- refs/heads/main
- Commit
- 19912563f540228593df0c489082e5e2508e21e4
- cf-ray
- a5e14dffce91a140
- Request
- bd6a2a46…
- Protocol
- v2 · sha1
- Agent
- git/github-9922ec520325-Linux
- Filter
- advertised
- Fetch
- shallow wait-for-done filter
- Capabilities
- agent ls-refs fetch server-option object-format