forked from rocicorp/replicache-yjs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf: shorten hashes from 64 chars to 12 chars
- Loading branch information
Showing
4 changed files
with
49 additions
and
9 deletions.
There are no files selected for viewing
This file contains 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,24 @@ | ||
import * as base64 from 'base64-js'; | ||
|
||
export const AVG_CHUNK_SIZE_B = 1024; | ||
export const MIN_CHUNK_SIZE_B = 256; | ||
export const MAX_CHUNK_SIZE_B = 2048; | ||
|
||
export const hashFn = async (chunk: Uint8Array) => { | ||
const hashBuffer = await crypto.subtle.digest('SHA-256', chunk); | ||
// Truncate the sha-256 hash from 32 bytes to 9 bytes. This gives us | ||
// plenty of collision resistance for the range of expected document sizes. | ||
// If we assume a max document size of 100MB, the probability of having a | ||
// hash collision in a document of this size is roughly 1 in a trillion | ||
// (based on the approximation function p(n) = n^2 / (2H) | ||
// from | ||
// https://en.wikipedia.org/wiki/Birthday_attack#Simple_approximation), | ||
// where p(n) is probability of collision, n is number of hashes, and H | ||
// is number of possible hash outputs. For 100MB document we have | ||
// n = 100,000 (100,000 1KB chunks) and H = 2^(8*9), | ||
// p(100,000) = (100,000^2 / (2*2^72)) = 1.0587912e-12 | ||
// | ||
// In base64 9 bytes will encode to 12 chars with no padding (all chars | ||
// contain information). | ||
return base64.fromByteArray(new Uint8Array(hashBuffer.slice(0, 9))); | ||
}; |
This file contains 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 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 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