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

Function generateHash

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

Source from the content-addressed store, hash-verified

572 * preventing cache key collision attacks.
573 */
574export function generateHash(value: string): string {
575 textEncoder ??= new TextEncoder();
576 const inputBytes = textEncoder.encode(value);
577
578 // Initial hash values (first 32 bits of the fractional parts of the square roots of the first 8 primes 2..19):
579 let hashState0 = 0x6a09e667;
580 let hashState1 = 0xbb67ae85;
581 let hashState2 = 0x3c6ef372;
582 let hashState3 = 0xa54ff53a;
583 let hashState4 = 0x510e527f;
584 let hashState5 = 0x9b05688c;
585 let hashState6 = 0x1f83d9ab;
586 let hashState7 = 0x5be0cd19;
587
588 // Pre-processing (Padding):
589 const messageLengthInBits = inputBytes.length * 8;
590
591 // The total length of the padded message must be a multiple of 64 bytes (512 bits)
592 const paddedLengthInBytes = (((inputBytes.length + 8) >> 6) + 1) << 6;
593 const paddedBytes = new Uint8Array(paddedLengthInBytes);
594 paddedBytes.set(inputBytes);
595 paddedBytes[inputBytes.length] = 0x80; // Append a single '1' bit (0x80 byte)
596
597 const paddedBytesView = new DataView(paddedBytes.buffer);
598 const lowBits = messageLengthInBits >>> 0;
599 const highBits = (messageLengthInBits / 0x100000000) >>> 0;
600 paddedBytesView.setUint32(paddedLengthInBytes - 8, highBits, false);
601 paddedBytesView.setUint32(paddedLengthInBytes - 4, lowBits, false);
602
603 // Process the message in successive 64-byte chunks:
604 const messageSchedule = new Uint32Array(64);
605 for (let chunkOffset = 0; chunkOffset < paddedLengthInBytes; chunkOffset += 64) {
606 // Initialize first 16 words of the message schedule:
607 for (let i = 0; i < 16; i++) {
608 messageSchedule[i] = paddedBytesView.getUint32(chunkOffset + i * 4, false);
609 }
610
611 // Extend to 64 words:
612 for (let i = 16; i < 64; i++) {
613 const prevWord15 = messageSchedule[i - 15];
614 const sigma0 =
615 (((prevWord15 >>> 7) | (prevWord15 << 25)) ^
616 ((prevWord15 >>> 18) | (prevWord15 << 14)) ^
617 (prevWord15 >>> 3)) >>>
618 0;
619
620 const prevWord2 = messageSchedule[i - 2];
621 const sigma1 =
622 (((prevWord2 >>> 17) | (prevWord2 << 15)) ^
623 ((prevWord2 >>> 19) | (prevWord2 << 13)) ^
624 (prevWord2 >>> 10)) >>>
625 0;
626
627 messageSchedule[i] =
628 (messageSchedule[i - 16] + sigma0 + messageSchedule[i - 7] + sigma1) >>> 0;
629 }
630
631 // 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