-
Notifications
You must be signed in to change notification settings - Fork 229
Replace sha1 npm package with inline implementation
#1465
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Matus Tomlein (matus-tomlein)
merged 5 commits into
snowplow:release/4.7.0
from
jantimon:issue/1464-replace-sha1-buffer-polyfill
Mar 30, 2026
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
456db0f
Replace sha1 npm package with inline implementation to remove Buffer …
jantimon c3dbb46
Revert unrelated autoinstaller lockfile changes and add rush change file
jantimon f8bac40
Apply suggestion from @Copilot
jantimon 3b9fadc
don't use padStart for IE11 compatibility
jantimon 34eb151
Merge remote-tracking branch 'upstream/release/4.7.0' into issue/1464…
jantimon File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
10 changes: 10 additions & 0 deletions
10
...owplow/browser-tracker-core/issue-1464-replace-sha1-buffer-polyfill_2026-03-29-16-18.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| { | ||
| "changes": [ | ||
| { | ||
| "comment": "Replace SHA1 buffer polyfill with lightweight implementation", | ||
| "type": "none", | ||
| "packageName": "@snowplow/browser-tracker-core" | ||
| } | ||
| ], | ||
| "packageName": "@snowplow/browser-tracker-core" | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| // DO NOT MODIFY THIS FILE MANUALLY BUT DO COMMIT IT. It is generated and used by Rush. | ||
| { | ||
| "pnpmShrinkwrapHash": "d3b2991182c4ca5879833e9c94d65eb10cb6c7f1", | ||
| "pnpmShrinkwrapHash": "7bfb995166f0ecc4b03f50797de4b75379120fb5", | ||
| "preferredVersionsHash": "bf21a9e8fbc5a3846fb05b4fa0859e0917b2202f" | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| /** | ||
| * Pure-JS SHA-1 implementation (FIPS 180-4). | ||
| * Replaces the `sha1` npm package to avoid pulling in a Node.js `Buffer` | ||
| * polyfill (~28 KB) when bundled for the browser. | ||
| */ | ||
| /** Compute SHA-1 hex digest of a UTF-8 string (FIPS 180-4). */ | ||
| export function sha1(message: string): string { | ||
| // Encode UTF-8 | ||
| const bytes: number[] = []; | ||
| for (let i = 0; i < message.length; i++) { | ||
| let c = message.charCodeAt(i); | ||
| if (c < 0x80) { | ||
| bytes.push(c); | ||
| } else if (c < 0x800) { | ||
| bytes.push(0xc0 | (c >> 6), 0x80 | (c & 0x3f)); | ||
| } else if (c >= 0xd800 && c <= 0xdbff && i + 1 < message.length) { | ||
| const next = message.charCodeAt(++i); | ||
| c = 0x10000 + ((c & 0x3ff) << 10) + (next & 0x3ff); | ||
| bytes.push(0xf0 | (c >> 18), 0x80 | ((c >> 12) & 0x3f), 0x80 | ((c >> 6) & 0x3f), 0x80 | (c & 0x3f)); | ||
| } else { | ||
| bytes.push(0xe0 | (c >> 12), 0x80 | ((c >> 6) & 0x3f), 0x80 | (c & 0x3f)); | ||
| } | ||
| } | ||
|
|
||
| // Pre-processing: pad to 512-bit blocks | ||
| const bitLen = bytes.length * 8; | ||
| bytes.push(0x80); | ||
| while (bytes.length % 64 !== 56) bytes.push(0); | ||
| // Append length as 64-bit big-endian (high 32 bits always 0 for strings < 512 MB) | ||
|
jantimon marked this conversation as resolved.
|
||
| bytes.push(0, 0, 0, 0); | ||
| bytes.push((bitLen >>> 24) & 0xff, (bitLen >>> 16) & 0xff, (bitLen >>> 8) & 0xff, bitLen & 0xff); | ||
|
|
||
| // Initialize hash values | ||
| let h0 = 0x67452301; | ||
| let h1 = 0xefcdab89; | ||
| let h2 = 0x98badcfe; | ||
| let h3 = 0x10325476; | ||
| let h4 = 0xc3d2e1f0; | ||
|
|
||
| // Process each 512-bit block | ||
| const w = new Array<number>(80); | ||
| for (let offset = 0; offset < bytes.length; offset += 64) { | ||
| for (let i = 0; i < 16; i++) { | ||
| w[i] = | ||
| (bytes[offset + i * 4] << 24) | | ||
| (bytes[offset + i * 4 + 1] << 16) | | ||
| (bytes[offset + i * 4 + 2] << 8) | | ||
| bytes[offset + i * 4 + 3]; | ||
| } | ||
| for (let i = 16; i < 80; i++) { | ||
| const n = w[i - 3] ^ w[i - 8] ^ w[i - 14] ^ w[i - 16]; | ||
| w[i] = (n << 1) | (n >>> 31); | ||
| } | ||
|
|
||
| let a = h0, | ||
| b = h1, | ||
| c = h2, | ||
| d = h3, | ||
| e = h4; | ||
|
|
||
| for (let i = 0; i < 80; i++) { | ||
| let f: number, k: number; | ||
| if (i < 20) { | ||
| f = (b & c) | (~b & d); | ||
| k = 0x5a827999; | ||
| } else if (i < 40) { | ||
| f = b ^ c ^ d; | ||
| k = 0x6ed9eba1; | ||
| } else if (i < 60) { | ||
| f = (b & c) | (b & d) | (c & d); | ||
| k = 0x8f1bbcdc; | ||
| } else { | ||
| f = b ^ c ^ d; | ||
| k = 0xca62c1d6; | ||
| } | ||
| const temp = (((a << 5) | (a >>> 27)) + f + e + k + w[i]) | 0; | ||
| e = d; | ||
| d = c; | ||
| c = (b << 30) | (b >>> 2); | ||
| b = a; | ||
| a = temp; | ||
| } | ||
|
|
||
| h0 = (h0 + a) | 0; | ||
| h1 = (h1 + b) | 0; | ||
| h2 = (h2 + c) | 0; | ||
| h3 = (h3 + d) | 0; | ||
| h4 = (h4 + e) | 0; | ||
| } | ||
|
|
||
| // Produce hex digest | ||
| let hex = ''; | ||
| for (const h of [h0, h1, h2, h3, h4]) { | ||
| const part = (h >>> 0).toString(16); | ||
| hex += (part.length < 8 ? '00000000'.slice(part.length) : '') + part; | ||
| } | ||
| return hex; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| import { sha1 } from '../../src/helpers/sha1'; | ||
|
|
||
| describe('sha1', () => { | ||
| // RFC 3174 test vectors | ||
| test('empty string', () => { | ||
| expect(sha1('')).toBe('da39a3ee5e6b4b0d3255bfef95601890afd80709'); | ||
| }); | ||
|
|
||
| test('"abc"', () => { | ||
| expect(sha1('abc')).toBe('a9993e364706816aba3e25717850c26c9cd0d89d'); | ||
| }); | ||
|
|
||
| test('"The quick brown fox jumps over the lazy dog"', () => { | ||
| expect(sha1('The quick brown fox jumps over the lazy dog')).toBe('2fd4e1c67a2d28fced849ee1bb76e7391b93eb12'); | ||
| }); | ||
|
jantimon marked this conversation as resolved.
|
||
|
|
||
| test('multi-block message', () => { | ||
| expect(sha1('abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq')).toBe( | ||
| '84983e441c3bd26ebaae4aa1f95129e5e54670f1' | ||
| ); | ||
| }); | ||
|
|
||
| // Domain hash use case — must match output of sha1 npm package v1.1.1 | ||
| test('domain hash: "example.com/"', () => { | ||
| expect(sha1('example.com/')).toBe('880970443b82bdca0439e34c62e6c667277c2b39'); | ||
| expect(sha1('example.com/').slice(0, 4)).toBe('8809'); | ||
| }); | ||
|
|
||
| test('domain hash: "localhost/"', () => { | ||
| expect(sha1('localhost/')).toBe('1fffd42e9a20211889ebfae87a84665b392c19a4'); | ||
| expect(sha1('localhost/').slice(0, 4)).toBe('1fff'); | ||
| }); | ||
|
|
||
| test('returns 40-char lowercase hex string', () => { | ||
| expect(sha1('anything')).toMatch(/^[0-9a-f]{40}$/); | ||
| }); | ||
| }); | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.