Skip to content

The all-in-one local AI studio for your desktop: chat, image and video generation and a coding agent in one free, open source app. Windows and Linux. No Docker, no terminal, no cloud required.

4.3 KB
Raw Blame
/**
 * SHA-256 that takes its input in pieces.
 *
 * WebCrypto only hashes a buffer it holds whole, and a chat attachment may
 * now be 256 MB (lib/chat-files.ts). Reading such a file into one buffer just
 * to name its hash would cost the renderer that much memory at once. This
 * hashes slice by slice instead, so a large file never sits in memory whole.
 *
 * Plain FIPS 180-4, no dependency. Checked against WebCrypto in the tests.
 */

const K = new Uint32Array([
  0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
  0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
  0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
  0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
  0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
  0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
  0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
  0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2,
])

export class Sha256 {
  private readonly h = new Uint32Array([
    0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19,
  ])
  private readonly w = new Uint32Array(64)
  private readonly block = new Uint8Array(64)
  private filled = 0
  private length = 0
  private done = false

  update(data: Uint8Array): this {
    if (this.done) throw new Error('Sha256: update after digest')
    this.length += data.length
    let at = 0
    if (this.filled > 0) {
      const take = Math.min(64 - this.filled, data.length)
      this.block.set(data.subarray(0, take), this.filled)
      this.filled += take
      at = take
      if (this.filled < 64) return this
      this.compress(this.block, 0)
      this.filled = 0
    }
    for (; at + 64 <= data.length; at += 64) this.compress(data, at)
    if (at < data.length) {
      this.block.set(data.subarray(at), 0)
      this.filled = data.length - at
    }
    return this
  }

  /** Lowercase hex, as WebCrypto plus a hex join would give it. */
  hex(): string {
    if (!this.done) {
      const bits = this.length * 8
      const tail = new Uint8Array(this.filled < 56 ? 64 : 128)
      tail.set(this.block.subarray(0, this.filled))
      tail[this.filled] = 0x80
      const view = new DataView(tail.buffer)
      view.setUint32(tail.length - 8, Math.floor(bits / 0x100000000))
      view.setUint32(tail.length - 4, bits >>> 0)
      for (let at = 0; at < tail.length; at += 64) this.compress(tail, at)
      this.done = true
    }
    return Array.from(this.h, (v) => v.toString(16).padStart(8, '0')).join('')
  }

  private compress(data: Uint8Array, at: number): void {
    const w = this.w
    for (let i = 0; i < 16; i++) {
      const j = at + i * 4
      w[i] = (data[j] << 24) | (data[j + 1] << 16) | (data[j + 2] << 8) | data[j + 3]
    }
    for (let i = 16; i < 64; i++) {
      const a = w[i - 15]
      const b = w[i - 2]
      const s0 = ((a >>> 7) | (a << 25)) ^ ((a >>> 18) | (a << 14)) ^ (a >>> 3)
      const s1 = ((b >>> 17) | (b << 15)) ^ ((b >>> 19) | (b << 13)) ^ (b >>> 10)
      w[i] = (w[i - 16] + s0 + w[i - 7] + s1) | 0
    }
    const h = this.h
    let a = h[0], b = h[1], c = h[2], d = h[3], e = h[4], f = h[5], g = h[6], k = h[7]
    for (let i = 0; i < 64; i++) {
      const s1 = ((e >>> 6) | (e << 26)) ^ ((e >>> 11) | (e << 21)) ^ ((e >>> 25) | (e << 7))
      const ch = (e & f) ^ (~e & g)
      const t1 = (k + s1 + ch + K[i] + w[i]) | 0
      const s0 = ((a >>> 2) | (a << 30)) ^ ((a >>> 13) | (a << 19)) ^ ((a >>> 22) | (a << 10))
      const maj = (a & b) ^ (a & c) ^ (b & c)
      const t2 = (s0 + maj) | 0
      k = g; g = f; f = e
      e = (d + t1) | 0
      d = c; c = b; b = a
      a = (t1 + t2) | 0
    }
    h[0] += a; h[1] += b; h[2] += c; h[3] += d
    h[4] += e; h[5] += f; h[6] += g; h[7] += k
  }
}

/** The hash of a file, read in slices of `chunkBytes`. */
export async function sha256OfBlob(blob: Blob, chunkBytes = 4 * 1024 * 1024): Promise<string> {
  const hash = new Sha256()
  for (let at = 0; at < blob.size; at += chunkBytes) {
    hash.update(new Uint8Array(await blob.slice(at, at + chunkBytes).arrayBuffer()))
  }
  return hash.hex()
}