/** * 纯 JS SHA-1(十六进制输出)。 * 零依赖实现:包运行在 Sketch 的 JavaScriptCore 里,没有 node crypto / Buffer。 */ const utf8Bytes = (str: string): number[] => { const bytes: number[] = []; for (let i = 0; i < str.length; i++) { let code = str.charCodeAt(i); if (code < 0x80) { bytes.push(code); } else if (code < 0x800) { bytes.push(0xc0 | (code >> 6), 0x80 | (code & 0x3f)); } else if (code >= 0xd800 && code <= 0xdbff && i + 1 < str.length && str.charCodeAt(i + 1) >= 0xdc00 && str.charCodeAt(i + 1) <= 0xdfff) { code = 0x10000 + ((code - 0xd800) << 10) + (str.charCodeAt(i + 1) - 0xdc00); bytes.push( 0xf0 | (code >> 18), 0x80 | ((code >> 12) & 0x3f), 0x80 | ((code >> 6) & 0x3f), 0x80 | (code & 0x3f), ); i++; } else { bytes.push(0xe0 | (code >> 12), 0x80 | ((code >> 6) & 0x3f), 0x80 | (code & 0x3f)); } } return bytes; }; const rol = (n: number, s: number): number => ((n << s) | (n >>> (32 - s))) >>> 0; export const sha1 = (message: string): string => { const bytes = utf8Bytes(message); const bitLen = bytes.length * 8; bytes.push(0x80); while (bytes.length % 64 !== 56) bytes.push(0); // 64 位长度:JS 位运算截断在 32 位,高 32 位用除法取 const hi = Math.floor(bitLen / 0x100000000); const lo = bitLen >>> 0; bytes.push((hi >>> 24) & 0xff, (hi >>> 16) & 0xff, (hi >>> 8) & 0xff, hi & 0xff); bytes.push((lo >>> 24) & 0xff, (lo >>> 16) & 0xff, (lo >>> 8) & 0xff, lo & 0xff); let h0 = 0x67452301; let h1 = 0xefcdab89; let h2 = 0x98badcfe; let h3 = 0x10325476; let h4 = 0xc3d2e1f0; const w: number[] = new Array(80); for (let i = 0; i < bytes.length; i += 64) { for (let j = 0; j < 16; j++) { w[j] = ((bytes[i + j * 4] << 24) | (bytes[i + j * 4 + 1] << 16) | (bytes[i + j * 4 + 2] << 8) | bytes[i + j * 4 + 3]) >>> 0; } for (let j = 16; j < 80; j++) { w[j] = rol(w[j - 3] ^ w[j - 8] ^ w[j - 14] ^ w[j - 16], 1); } let a = h0; let b = h1; let c = h2; let d = h3; let e = h4; for (let j = 0; j < 80; j++) { let f: number; let k: number; if (j < 20) { f = (b & c) | (~b & d); k = 0x5a827999; } else if (j < 40) { f = b ^ c ^ d; k = 0x6ed9eba1; } else if (j < 60) { f = (b & c) | (b & d) | (c & d); k = 0x8f1bbcdc; } else { f = b ^ c ^ d; k = 0xca62c1d6; } const t = (rol(a, 5) + ((f >>> 0) + e + k + w[j]) % 0x100000000) >>> 0; e = d; d = c; c = rol(b, 30); b = a; a = t; } h0 = (h0 + a) >>> 0; h1 = (h1 + b) >>> 0; h2 = (h2 + c) >>> 0; h3 = (h3 + d) >>> 0; h4 = (h4 + e) >>> 0; } const hex = (n: number): string => ('00000000' + n.toString(16)).slice(-8); return hex(h0) + hex(h1) + hex(h2) + hex(h3) + hex(h4); }; export default sha1;