Functional Weave
Code in TypeScript

crypto.sha1@1.0.0

impl/typescript.ts

3,012 bytes · the TypeScript implementation · view raw

/**
 * The SHA-1 digest of a byte string (FIPS 180-4 section 6.1).
 *
 * Only for HMAC-SHA1 in HOTP/TOTP: SHA-1 is broken for collisions. Pure,
 * synchronous code with no node:crypto or crypto.subtle, so it runs in the
 * browser too. Arithmetic is on signed 32-bit integers (`| 0`), which wrap
 * exactly as the standard's mod 2^32 additions require.
 */
export function sha1(bytes: readonly number[]): readonly number[] {
  if (!Array.isArray(bytes) && !(bytes instanceof Uint8Array)) {
    throw new TypeError("bytes must be a list of integers from 0 to 255");
  }
  const length = bytes.length;
  for (let i = 0; i < length; i++) {
    const b = bytes[i];
    if (typeof b !== "number" || !Number.isInteger(b) || b < 0 || b > 255) {
      throw new RangeError("bytes must be a list of integers from 0 to 255");
    }
  }

  // Padding (section 5.1.1): a 1 bit, zeros, then the length in bits as a
  // 64-bit big-endian integer, to a multiple of 64 bytes.
  const blocks = Math.floor((length + 8) / 64) + 1;
  const padded = new Uint8Array(blocks * 64);
  for (let i = 0; i < length; i++) padded[i] = bytes[i];
  padded[length] = 0x80;
  const bitsHigh = Math.floor(length / 0x20000000);
  const bitsLow = (length * 8) >>> 0;
  const end = padded.length;
  padded[end - 8] = bitsHigh >>> 24;
  padded[end - 7] = (bitsHigh >>> 16) & 255;
  padded[end - 6] = (bitsHigh >>> 8) & 255;
  padded[end - 5] = bitsHigh & 255;
  padded[end - 4] = bitsLow >>> 24;
  padded[end - 3] = (bitsLow >>> 16) & 255;
  padded[end - 2] = (bitsLow >>> 8) & 255;
  padded[end - 1] = bitsLow & 255;

  const h = new Int32Array([0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476, 0xc3d2e1f0]);
  const w = new Int32Array(80);
  for (let block = 0; block < blocks; block++) {
    const base = block * 64;
    for (let t = 0; t < 16; t++) {
      const j = base + t * 4;
      w[t] = (padded[j] << 24) | (padded[j + 1] << 16) | (padded[j + 2] << 8) | padded[j + 3];
    }
    for (let t = 16; t < 80; t++) {
      const x = w[t - 3] ^ w[t - 8] ^ w[t - 14] ^ w[t - 16];
      w[t] = (x << 1) | (x >>> 31);
    }
    let a = h[0], b = h[1], c = h[2], d = h[3], e = h[4];
    for (let t = 0; t < 80; t++) {
      let f: number;
      let k: number;
      if (t < 20) {
        f = (b & c) | (~b & d);
        k = 0x5a827999;
      } else if (t < 40) {
        f = b ^ c ^ d;
        k = 0x6ed9eba1;
      } else if (t < 60) {
        f = (b & c) | (b & d) | (c & d);
        k = 0x8f1bbcdc | 0;
      } else {
        f = b ^ c ^ d;
        k = 0xca62c1d6 | 0;
      }
      const temp = (((a << 5) | (a >>> 27)) + f + e + k + w[t]) | 0;
      e = d;
      d = c;
      c = (b << 30) | (b >>> 2);
      b = a;
      a = temp;
    }
    h[0] = (h[0] + a) | 0;
    h[1] = (h[1] + b) | 0;
    h[2] = (h[2] + c) | 0;
    h[3] = (h[3] + d) | 0;
    h[4] = (h[4] + e) | 0;
  }

  const out: number[] = [];
  for (let i = 0; i < 5; i++) {
    out.push((h[i] >>> 24) & 255, (h[i] >>> 16) & 255, (h[i] >>> 8) & 255, h[i] & 255);
  }
  return out;
}