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

Function generateHash

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

Source from the content-addressed store, hash-verified

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