// 自研 SHA-256(FIPS 180-4)。
//
// 不依赖任何第三方实现:DKIM 的 body hash 与签名哈希都建立在这上面,
// 因此这里用 NIST 公开测试向量做固定断言。
///|
let sha256_k : Array[UInt] = [
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,
]
///|
fn rotr(x : UInt, n : Int) -> UInt {
(x >> n) | (x << (32 - n))
}
///|
fn sha256_compress(
state : Array[UInt],
block : BytesView,
offset : Int,
) -> Unit {
let w : Array[UInt] = Array::make(64, 0)
for i in 0..<16 {
let base = offset + i * 4
let b0 = block[base].to_uint()
let b1 = block[base + 1].to_uint()
let b2 = block[base + 2].to_uint()
let b3 = block[base + 3].to_uint()
w[i] = (b0 << 24) | (b1 << 16) | (b2 << 8) | b3
}
for i in 16..<64 {
let s0 = rotr(w[i - 15], 7) ^ rotr(w[i - 15], 18) ^ (w[i - 15] >> 3)
let s1 = rotr(w[i - 2], 17) ^ rotr(w[i - 2], 19) ^ (w[i - 2] >> 10)
w[i] = w[i - 16] + s0 + w[i - 7] + s1
}
let mut a = state[0]
let mut b = state[1]
let mut c = state[2]
let mut d = state[3]
let mut e = state[4]
let mut f = state[5]
let mut g = state[6]
let mut h = state[7]
for i in 0..<64 {
let s1 = rotr(e, 6) ^ rotr(e, 11) ^ rotr(e, 25)
let ch = (e & f) ^ ((e ^ 0xFFFFFFFF) & g)
let temp1 = h + s1 + ch + sha256_k[i] + w[i]
let s0 = rotr(a, 2) ^ rotr(a, 13) ^ rotr(a, 22)
let maj = (a & b) ^ (a & c) ^ (b & c)
let temp2 = s0 + maj
h = g
g = f
f = e
e = d + temp1
d = c
c = b
b = a
a = temp1 + temp2
}
state[0] = state[0] + a
state[1] = state[1] + b
state[2] = state[2] + c
state[3] = state[3] + d
state[4] = state[4] + e
state[5] = state[5] + f
state[6] = state[6] + g
state[7] = state[7] + h
}
///|
/// 计算 SHA-256 摘要,返回 32 字节。
pub fn sha256(data : BytesView) -> Bytes {
let state : Array[UInt] = [
0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab,
0x5be0cd19,
]
let len = data.length()
// 填充:0x80 + 若干 0x00 + 64 位大端比特长度
let padded_len = ((len + 8) / 64 + 1) * 64
let padded : Array[Byte] = Array::make(padded_len, b'\x00')
for i in 0..> shift) & 0xFFUL)
.to_int()
.to_byte()
}
let bytes = Bytes::from_array(padded)
let mut offset = 0
while offset < padded_len {
sha256_compress(state, bytes, offset)
offset = offset + 64
}
let out : Array[Byte] = Array::make(32, b'\x00')
for i in 0..<8 {
out[i * 4] = ((state[i] >> 24) & 0xFF).to_byte()
out[i * 4 + 1] = ((state[i] >> 16) & 0xFF).to_byte()
out[i * 4 + 2] = ((state[i] >> 8) & 0xFF).to_byte()
out[i * 4 + 3] = (state[i] & 0xFF).to_byte()
}
Bytes::from_array(out)
}
///|
/// 比较两个字节串是否相等。
pub fn bytes_equal(a : BytesView, b : BytesView) -> Bool {
if a.length() != b.length() {
return false
}
let mut i = 0
while i < a.length() {
if a[i] != b[i] {
return false
}
i = i + 1
}
true
}