Watch
1
0
Fork
You've already forked packages
0
packages/stripe/lib/sha256.weft
Weft packages 306b143104 packages/ at markup-kit dd1e6eb5270e
A snapshot of packages/ in the Weft repository; the official packages are published from these commits.

Markup-Kit-Commit: dd1e6eb5270e62e0c8b6c202b3d01f0da6e00ddc
2026-11-19 20:43:26 +00:00

195 lines
4.1 KiB
Text

// SHA-256 and HMAC-SHA-256 (FIPS 180-4, RFC 2104), for verifying webhook signatures. Written here until `std/crypto` is
// on main: when it is, this file goes and `webhooks.weft` calls `hmac` and `timingSafeEqual` from there.
let K: i64[] = [
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,
]
/// The low 32 bits. Words are held in an `i64`, whose sums can't overflow, and masked: SHA-256's additions wrap, and an
/// `int`'s panic.
let WORD: i64 = 0xffffffff
fn shr(x: i64, n: int): i64 {
return x >>> n
}
fn rotr(x: i64, n: int): i64 {
return (shr(x, n) | (x << (32 - n))) & WORD
}
fn compress(mut h: i64[], m: int[], at: int) {
let mut w: i64[] = []
for t of 0..16 {
let i = at + t * 4
w.push(i64((m[i] << 24) | (m[i + 1] << 16) | (m[i + 2] << 8) | m[i + 3]) & WORD)
}
for t of 16..64 {
let a = w[t - 15]
let b = w[t - 2]
let s0 = rotr(a, 7) ^ rotr(a, 18) ^ shr(a, 3)
let s1 = rotr(b, 17) ^ rotr(b, 19) ^ shr(b, 10)
w.push((w[t - 16] + s0 + w[t - 7] + s1) & WORD)
}
let mut a = h[0]
let mut b = h[1]
let mut c = h[2]
let mut d = h[3]
let mut e = h[4]
let mut f = h[5]
let mut g = h[6]
let mut k = h[7]
for t of 0..64 {
let s1 = rotr(e, 6) ^ rotr(e, 11) ^ rotr(e, 25)
let ch = (e & f) ^ (~e & g & WORD)
let t1 = (k + s1 + ch + K[t] + w[t]) & WORD
let s0 = rotr(a, 2) ^ rotr(a, 13) ^ rotr(a, 22)
let maj = (a & b) ^ (a & c) ^ (b & c)
let t2 = (s0 + maj) & WORD
k = g
g = f
f = e
e = (d + t1) & WORD
d = c
c = b
b = a
a = (t1 + t2) & WORD
}
let sums = [a, b, c, d, e, f, g, k]
for i of 0..8 {
h[i] = (h[i] + sums[i]) & WORD
}
}
/// The byte of `word` that `shift` bits down.
fn byteOf(word: i64, shift: int): int {
return try int(shr(word, shift) & 255) else 0
}
/// The SHA-256 digest of `data`.
export fn sha256(data: Bytes): Bytes {
let mut m = data.toList()
let length = i64(m.length)
m.push(0x80)
while m.length % 64 != 56 {
m.push(0)
}
// The length in bits, as 64 bits big-endian.
let bits = length * 8
let high = shr(bits, 32) & WORD
let low = bits & WORD
for shift of [24, 16, 8, 0] {
m.push(byteOf(high, shift))
}
for shift of [24, 16, 8, 0] {
m.push(byteOf(low, shift))
}
let mut h: i64[] = [
0x6a09e667,
0xbb67ae85,
0x3c6ef372,
0xa54ff53a,
0x510e527f,
0x9b05688c,
0x1f83d9ab,
0x5be0cd19,
]
let mut at = 0
while at < m.length {
compress(&h, m, at)
at += 64
}
let mut out: int[] = []
for word of h {
for shift of [24, 16, 8, 0] {
out.push(byteOf(word, shift))
}
}
return Bytes.of(out)
}
/// HMAC-SHA-256 of `data` with `key`.
export fn hmacSha256(key: Bytes, data: Bytes): Bytes {
let mut block = (key.length > 64 ? sha256(key) : key).toList()
while block.length < 64 {
block.push(0)
}
let inner = Bytes.of(block.map(b => b ^ 0x36)).concat(data)
let outer = Bytes.of(block.map(b => b ^ 0x5c))
return sha256(outer.concat(sha256(inner)))
}
/// Whether two byte strings are equal, looking at every byte whatever it finds.
export fn constantTimeEqual(a: Bytes, b: Bytes): boolean {
if a.length != b.length {
return false
}
let x = a.toList()
let y = b.toList()
let mut diff = 0
for i of 0..x.length {
diff = diff | (x[i] ^ y[i])
}
return diff == 0
}