| 121 | * DO NOT USE IT IN A SECURITY SENSITIVE CONTEXT. |
| 122 | */ |
| 123 | export 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. |