MCPcopy
hub / github.com/angular/angular / sha1

Function sha1

packages/compiler/src/i18n/digest.ts:123–172  ·  view source on GitHub ↗
(str: string)

Source from the content-addressed store, hash-verified

121 * DO NOT USE IT IN A SECURITY SENSITIVE CONTEXT.
122 */
123export function sha1(str: string): string {
124 textEncoder ??= new TextEncoder();
125 const utf8 = [...textEncoder.encode(str)];
126 const words32 = bytesToWords32(utf8, Endian.Big);
127 const len = utf8.length * 8;
128
129 const w = new Uint32Array(80);
130 let a = 0x67452301,
131 b = 0xefcdab89,
132 c = 0x98badcfe,
133 d = 0x10325476,
134 e = 0xc3d2e1f0;
135
136 words32[len >> 5] |= 0x80 << (24 - (len % 32));
137 words32[(((len + 64) >> 9) << 4) + 15] = len;
138
139 for (let i = 0; i < words32.length; i += 16) {
140 const h0 = a,
141 h1 = b,
142 h2 = c,
143 h3 = d,
144 h4 = e;
145
146 for (let j = 0; j < 80; j++) {
147 if (j < 16) {
148 w[j] = words32[i + j];
149 } else {
150 w[j] = rol32(w[j - 3] ^ w[j - 8] ^ w[j - 14] ^ w[j - 16], 1);
151 }
152
153 const fkVal = fk(j, b, c, d);
154 const f = fkVal[0];
155 const k = fkVal[1];
156 const temp = [rol32(a, 5), f, e, k, w[j]].reduce(add32);
157 e = d;
158 d = c;
159 c = rol32(b, 30);
160 b = a;
161 a = temp;
162 }
163 a = add32(a, h0);
164 b = add32(b, h1);
165 c = add32(c, h2);
166 d = add32(d, h3);
167 e = add32(e, h4);
168 }
169
170 // Convert the output parts to a 160-bit hexadecimal string
171 return toHexU32(a) + toHexU32(b) + toHexU32(c) + toHexU32(d) + toHexU32(e);
172}
173
174/**
175 * Convert and format a number as a string representing a 32-bit unsigned hexadecimal number.

Callers 2

digest_spec.tsFile · 0.90
computeDigestFunction · 0.70

Calls 6

bytesToWords32Function · 0.85
toHexU32Function · 0.85
reduceMethod · 0.80
rol32Function · 0.70
fkFunction · 0.70
add32Function · 0.70

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…