MCPcopy
hub / github.com/opentrace/opentrace / sha1

Function sha1

ui/public/lbug_wasm_worker.js:1106–1180  ·  view source on GitHub ↗
(bytes)

Source from the content-addressed store, hash-verified

1104 return (x << n) | (x >>> (32 - n));
1105 }
1106 function sha1(bytes) {
1107 const K = [1518500249, 1859775393, 2400959708, 3395469782];
1108 const H = [1732584193, 4023233417, 2562383102, 271733878, 3285377520];
1109 const newBytes = new Uint8Array(bytes.length + 1);
1110 newBytes.set(bytes);
1111 newBytes[bytes.length] = 128;
1112 bytes = newBytes;
1113 const l = bytes.length / 4 + 2;
1114 const N = Math.ceil(l / 16);
1115 const M = new Array(N);
1116 for (let i = 0; i < N; ++i) {
1117 const arr = new Uint32Array(16);
1118 for (let j = 0; j < 16; ++j) {
1119 arr[j] =
1120 (bytes[i * 64 + j * 4] << 24) |
1121 (bytes[i * 64 + j * 4 + 1] << 16) |
1122 (bytes[i * 64 + j * 4 + 2] << 8) |
1123 bytes[i * 64 + j * 4 + 3];
1124 }
1125 M[i] = arr;
1126 }
1127 M[N - 1][14] = ((bytes.length - 1) * 8) / Math.pow(2, 32);
1128 M[N - 1][14] = Math.floor(M[N - 1][14]);
1129 M[N - 1][15] = ((bytes.length - 1) * 8) & 4294967295;
1130 for (let i = 0; i < N; ++i) {
1131 const W = new Uint32Array(80);
1132 for (let t = 0; t < 16; ++t) {
1133 W[t] = M[i][t];
1134 }
1135 for (let t = 16; t < 80; ++t) {
1136 W[t] = ROTL(W[t - 3] ^ W[t - 8] ^ W[t - 14] ^ W[t - 16], 1);
1137 }
1138 let a = H[0];
1139 let b = H[1];
1140 let c = H[2];
1141 let d = H[3];
1142 let e = H[4];
1143 for (let t = 0; t < 80; ++t) {
1144 const s = Math.floor(t / 20);
1145 const T = (ROTL(a, 5) + f(s, b, c, d) + e + K[s] + W[t]) >>> 0;
1146 e = d;
1147 d = c;
1148 c = ROTL(b, 30) >>> 0;
1149 b = a;
1150 a = T;
1151 }
1152 H[0] = (H[0] + a) >>> 0;
1153 H[1] = (H[1] + b) >>> 0;
1154 H[2] = (H[2] + c) >>> 0;
1155 H[3] = (H[3] + d) >>> 0;
1156 H[4] = (H[4] + e) >>> 0;
1157 }
1158 return Uint8Array.of(
1159 H[0] >> 24,
1160 H[0] >> 16,
1161 H[0] >> 8,
1162 H[0],
1163 H[1] >> 24,

Callers

nothing calls this directly

Calls 2

ROTLFunction · 0.85
fFunction · 0.85

Tested by

no test coverage detected