MCPcopy Create free account
hub / github.com/angular/angular / generateHash

Function generateHash

packages/common/http/src/transfer_cache.ts:600–729  ·  view source on GitHub ↗
(value: string)

Source from the content-addressed store, hash-verified

598 * preventing cache key collision attacks.
599 */
600export function generateHash(value: string): string {
601 textEncoder ??= new TextEncoder();
602 const inputBytes = textEncoder.encode(value);
603
604 // Initial hash values (first 32 bits of the fractional parts of the square roots of the first 8 primes 2..19):
605 let hashState0 = 0x6a09e667;
606 let hashState1 = 0xbb67ae85;
607 let hashState2 = 0x3c6ef372;
608 let hashState3 = 0xa54ff53a;
609 let hashState4 = 0x510e527f;
610 let hashState5 = 0x9b05688c;
611 let hashState6 = 0x1f83d9ab;
612 let hashState7 = 0x5be0cd19;
613
614 // Pre-processing (Padding):
615 const messageLengthInBits = inputBytes.length * 8;
616
617 // The total length of the padded message must be a multiple of 64 bytes (512 bits)
618 const paddedLengthInBytes = (((inputBytes.length + 8) >> 6) + 1) << 6;
619 const paddedBytes = new Uint8Array(paddedLengthInBytes);
620 paddedBytes.set(inputBytes);
621 paddedBytes[inputBytes.length] = 0x80; // Append a single '1' bit (0x80 byte)
622
623 const paddedBytesView = new DataView(paddedBytes.buffer);
624 const lowBits = messageLengthInBits >>> 0;
625 const highBits = (messageLengthInBits / 0x100000000) >>> 0;
626 paddedBytesView.setUint32(paddedLengthInBytes - 8, highBits, false);
627 paddedBytesView.setUint32(paddedLengthInBytes - 4, lowBits, false);
628
629 // Process the message in successive 64-byte chunks:
630 const messageSchedule = new Uint32Array(64);
631 for (let chunkOffset = 0; chunkOffset < paddedLengthInBytes; chunkOffset += 64) {
632 // Initialize first 16 words of the message schedule:
633 for (let i = 0; i < 16; i++) {
634 messageSchedule[i] = paddedBytesView.getUint32(chunkOffset + i * 4, false);
635 }
636
637 // Extend to 64 words:
638 for (let i = 16; i < 64; i++) {
639 const prevWord15 = messageSchedule[i - 15];
640 const sigma0 =
641 (((prevWord15 >>> 7) | (prevWord15 << 25)) ^
642 ((prevWord15 >>> 18) | (prevWord15 << 14)) ^
643 (prevWord15 >>> 3)) >>>
644 0;
645
646 const prevWord2 = messageSchedule[i - 2];
647 const sigma1 =
648 (((prevWord2 >>> 17) | (prevWord2 << 15)) ^
649 ((prevWord2 >>> 19) | (prevWord2 << 13)) ^
650 (prevWord2 >>> 10)) >>>
651 0;
652
653 messageSchedule[i] =
654 (messageSchedule[i - 16] + sigma0 + messageSchedule[i - 7] + sigma1) >>> 0;
655 }
656
657 // Initialize working variables to current hash values:

Callers 2

makeCacheKeyFunction · 0.85

Calls 4

mapMethod · 0.80
setMethod · 0.65
joinMethod · 0.65
toStringMethod · 0.65

Tested by

no test coverage detected