snapshot before regression test

This commit is contained in:
t
2026-07-18 19:25:40 +08:00
commit 796222a300
2295 changed files with 206453 additions and 0 deletions

View File

@@ -0,0 +1,4 @@
from .__md5 import md5, MD5_DIGEST_LEN
from .__sha1 import sha1, SHA1_DIGEST_LEN
from .__sha256 import sha256, SHA256_DIGEST_LEN
from .__sha512 import sha512, SHA512_DIGEST_LEN

131
includes/hashlib/__md5.py Normal file
View File

@@ -0,0 +1,131 @@
import string
import t, c
# ==============================================
# MD5 哈希算法
# ==============================================
MD5_BLOCK_LEN: t.CDefine = 64 # 分组块大小
MD5_DIGEST_LEN: t.CDefine = 16 # 摘要输出长度
# MD5 四轮非线性函数
def md5_F(x: t.CUInt32T, y: t.CUInt32T, z: t.CUInt32T) -> t.CUInt32T: return (x & y) | (~x & z)
def md5_G(x: t.CUInt32T, y: t.CUInt32T, z: t.CUInt32T) -> t.CUInt32T: return (x & z) | (y & ~z)
def md5_H(x: t.CUInt32T, y: t.CUInt32T, z: t.CUInt32T) -> t.CUInt32T: return x ^ y ^ z
def md5_I(x: t.CUInt32T, y: t.CUInt32T, z: t.CUInt32T) -> t.CUInt32T: return y ^ (x | ~z)
# 循环左移
def md5_rotl(x: t.CUInt32T, n: t.CInt) -> t.CUInt32T: return (x << n) | (x >> (32 - n))
# MD5 常量表、移位表
md5_T: t.CArray[t.CUInt32T, 64] = [
0xd76aa478, 0xe8c7b756, 0x242070db, 0xc1bdceee, 0xf57c0faf, 0x4787c62a, 0xa8304613, 0xfd469501,
0x698098d8, 0x8b44f7af, 0xffff5bb1, 0x895cd7be, 0x6b901122, 0xfd987193, 0xa679438e, 0x49b40821,
0xf61e2562, 0xc040b340, 0x265e5a51, 0xe9b6c7aa, 0xd62f105d, 0x02441453, 0xd8a1e681, 0xe7d3fbc8,
0x21e1cde6, 0xc33707d6, 0xf4d50d87, 0x455a14ed, 0xa9e3e905, 0xfcefa3f8, 0x676f02d9, 0x8d2a4c8a,
0xfffa3942, 0x8771f681, 0x6d9d6122, 0xfde5380c, 0xa4beea44, 0x4bdecfa9, 0xf6bb4b60, 0xbebfbc70,
0x289b7ec6, 0xeaa127fa, 0xd4ef3085, 0x04881d05, 0xd9d4d039, 0xe6db99e5, 0x1fa27cf8, 0xc4ac5665,
0xf4292244, 0x432aff97, 0xab9423a7, 0xfc93a039, 0x655b59c3, 0x8f0ccc92, 0xffeff47d, 0x85845dd1,
0x6fa87e4f, 0xfe2ce6e0, 0xa3014314, 0x4e0811a1, 0xf7537e82, 0xbd3af235, 0x2ad7d2bb, 0xeb86d391
]
md5_S: t.CArray[t.CInt, 64] = [
7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22,
5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20,
4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23,
6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21
]
# MD5 上下文结构体
@t.Object
class md5:
state: t.CArray[t.CUInt32T, 4] # 4个32位状态寄存器
count: t.CUInt64T # 已处理比特数
buf: t.CArray[t.CUInt8T, MD5_BLOCK_LEN] # 分组缓冲区
# 初始化MD5上下文
def __init__(self):
string.memset(self.buf, 0, MD5_BLOCK_LEN)
self.state[0] = 0x67452301
self.state[1] = 0xefcdab89
self.state[2] = 0x98badcfe
self.state[3] = 0x10325476
self.count = 0
# MD5 单分组压缩变换
def transform(self, block: t.CUInt8T | t.CPtr):
a: t.CUInt32T = self.state[0]
b: t.CUInt32T = self.state[1]
c: t.CUInt32T = self.state[2]
d: t.CUInt32T = self.state[3]
w: t.CArray[t.CUInt32T, 16]
# 字节序转32位字
for i in range(16):
w[i] = ((t.CUInt32T(block[i * 4 + 0]) << 0) |
(t.CUInt32T(block[i * 4 + 1]) << 8) |
(t.CUInt32T(block[i * 4 + 2]) << 16) |
(t.CUInt32T(block[i * 4 + 3]) << 24))
# 4轮64步迭代
for i in range(16):
tmp: t.CUInt32T = md5_F(b, c, d) + a + md5_T[i] + w[i]
a = d; d = c; c = b
b += md5_rotl(tmp, md5_S[i])
# 在 C 中的 for i<end 的循环退出后的值就是 range 的 end 参数
for i in range(16, 32):
tmp: t.CUInt32T = md5_G(b, c, d) + a + md5_T[i] + w[(5 * i + 1) % 16]
a = d; d = c; c = b
b += md5_rotl(tmp, md5_S[i])
for i in range(32, 48):
tmp: t.CUInt32T = md5_H(b, c, d) + a + md5_T[i] + w[(3 * i + 5) % 16]
a = d; d = c; c = b
b += md5_rotl(tmp, md5_S[i])
for i in range(48, 64):
tmp: t.CUInt32T = md5_I(b, c, d) + a + md5_T[i] + w[(7 * i + 0) % 16]
a = d; d = c; c = b
b += md5_rotl(tmp, md5_S[i])
# 累加到状态
self.state[0] += a
self.state[1] += b
self.state[2] += c
self.state[3] += d
# 分批输入数据
def update(self, s: str):
data: t.CUInt8T | t.CPtr = t.CUInt8T(s, t.CPtr)
length: t.CSizeT = len(s)
idx: t.CSizeT = self.count % MD5_BLOCK_LEN
self.count += length
if idx > 0:
left: t.CSizeT = MD5_BLOCK_LEN - idx
if length >= left:
string.memcpy(self.buf + idx, data, left)
self.transform(self.buf)
data += left
length -= left
idx = 0
while length >= MD5_BLOCK_LEN:
self.transform(data)
data += MD5_BLOCK_LEN
length -= MD5_BLOCK_LEN
if length > 0:
string.memcpy(self.buf + idx, data, length)
# 结束计算输出16字节摘要
def final(self, out: t.CArray[t.CUInt8T, MD5_DIGEST_LEN]):
idx: t.CSizeT = self.count % MD5_BLOCK_LEN
# 填充0x80结尾
self.buf[idx] = 0x80
idx += 1
# 空间不够则先压缩一次
if idx > MD5_BLOCK_LEN - 8:
string.memset(self.buf + idx, 0, MD5_BLOCK_LEN - idx)
self.transform(self.buf)
idx = 0
# 补0到末尾8字节
string.memset(self.buf + idx, 0, MD5_BLOCK_LEN - 8 - idx)
# 写入总比特数
bits: t.CUInt64T = self.count << 3
for i in range(8):
self.buf[MD5_BLOCK_LEN - 8 + i] = (bits >> (i * 8)) & 0xFF
self.transform(self.buf)
# 状态转小端字节输出
for i in range(4):
out[i * 4 + 0] = (self.state[i] >> 0) & 0xFF
out[i * 4 + 1] = (self.state[i] >> 8) & 0xFF
out[i * 4 + 2] = (self.state[i] >> 16) & 0xFF
out[i * 4 + 3] = (self.state[i] >> 24) & 0xFF

120
includes/hashlib/__sha1.py Normal file
View File

@@ -0,0 +1,120 @@
import string
import t, c
# ==============================================
# SHA1 哈希算法
# ==============================================
SHA1_BLOCK_LEN: t.CDefine = 64
SHA1_DIGEST_LEN: t.CDefine = 20
def sha1_rotl(x: t.CUInt32T, n: t.CInt) -> t.CUInt32T: return (x << n) | (x >> (32 - n))
# 三轮逻辑函数
def sha1_f1(b: t.CUInt32T, c: t.CUInt32T, d: t.CUInt32T) -> t.CUInt32T: return (b & c) | (~b & d)
def sha1_f2(b: t.CUInt32T, c: t.CUInt32T, d: t.CUInt32T) -> t.CUInt32T: return b ^ c ^ d
def sha1_f3(b: t.CUInt32T, c: t.CUInt32T, d: t.CUInt32T) -> t.CUInt32T: return (b & c) | (b & d) | (c & d)
# SHA1 上下文
@t.Object
class sha1:
state: t.CArray[t.CUInt32T, 5]
count: t.CUInt64T
buf: t.CArray[t.CUInt8T, SHA1_BLOCK_LEN]
def __init__(self):
memset(self.buf, 0, SHA1_BLOCK_LEN)
self.state[0] = 0x67452301
self.state[1] = 0xefcdab89
self.state[2] = 0x98badcfe
self.state[3] = 0x10325476
self.state[4] = 0xc3d2e1f0
self.count = 0
# 单分组压缩
def transform(self, block: t.CUInt8T | t.CPtr):
w: t.CArray[t.CUInt32T, 80]
data: t.CUInt8T | t.CPtr = block
# 前16个字
for i in range(16):
w[i] = (((t.CUInt32T(data[i * 4 + 0])) << 24) |
((t.CUInt32T(data[i * 4 + 1])) << 16) |
((t.CUInt32T(data[i * 4 + 2])) << 8) |
((t.CUInt32T(data[i * 4 + 3])) << 0))
# 扩展到80个字
for i in range(16, 80):
w[i] = sha1_rotl(w[i-3] ^ w[i-8] ^ w[i-14] ^ w[i-16],1)
a: t.CUInt32T = self.state[0]
b: t.CUInt32T = self.state[1]
c: t.CUInt32T = self.state[2]
d: t.CUInt32T = self.state[3]
e: t.CUInt32T = self.state[4]
k: t.CUInt32T
tmp: t.CUInt32T
# 4轮迭代
for i in range(0, 20):
k = 0x5a827999
tmp = sha1_rotl(a,5) + sha1_f1(b, c, d) + e + k + w[i]
e = d; d = c
c = sha1_rotl(b, 30)
b = a; a = tmp
for i in range(20, 40):
k = 0x6ed9eba1
tmp = sha1_rotl(a,5) + sha1_f2(b, c, d) + e + k + w[i]
e = d; d = c
c = sha1_rotl(b, 30)
b = a; a = tmp
for i in range(40, 60):
k = 0x8f1bbcdc
tmp = sha1_rotl(a,5) + sha1_f3(b, c, d) + e + k + w[i]
e = d; d = c
c = sha1_rotl(b, 30)
b = a; a = tmp
for i in range(60, 80):
k = 0xca62c1d6
tmp = sha1_rotl(a,5) + sha1_f2(b, c, d) + e + k + w[i]
e = d; d = c
c = sha1_rotl(b, 30)
b = a; a = tmp
self.state[0] += a
self.state[1] += b
self.state[2] += c
self.state[3] += d
self.state[4] += e
def update(self, s: str):
data: t.CUInt8T | t.CPtr = t.CUInt8T(s, t.CPtr)
length: t.CSizeT = len(s)
idx: t.CSizeT = self.count % SHA1_BLOCK_LEN
self.count += length
if idx > 0:
left: t.CSizeT = SHA1_BLOCK_LEN - idx
if length >= left:
memcpy(self.buf + idx, data, left)
self.transform(self.buf)
data += left
length -= left
idx = 0
while length >= SHA1_BLOCK_LEN:
self.transform(data)
data += SHA1_BLOCK_LEN
length -= SHA1_BLOCK_LEN
if length > 0:
memcpy(self.buf + idx, data, length)
def final(self, out: t.CArray[t.CUInt8T, SHA1_DIGEST_LEN]):
idx: t.CSizeT = self.count % SHA1_BLOCK_LEN
self.buf[idx] = 0x80
idx += 1
if idx > SHA1_BLOCK_LEN - 8:
memset(self.buf + idx, 0, SHA1_BLOCK_LEN - idx)
self.transform(self.buf)
idx = 0
memset(self.buf + idx,0,SHA1_BLOCK_LEN-8-idx)
bits: t.CUInt64T = self.count << 3
# SHA1 大端写入长度
for i in range(8):
self.buf[SHA1_BLOCK_LEN - 8 + i] = (bits >> (56 - i * 8)) & 0xFF
self.transform(self.buf)
# 大端输出摘要
for i in range(5):
out[i * 4 + 0] = (self.state[i] >> 24) & 0xFF
out[i * 4 + 1] = (self.state[i] >> 16) & 0xFF
out[i * 4 + 2] = (self.state[i] >> 8) & 0xFF
out[i * 4 + 3] = (self.state[i] >> 0) & 0xFF

View File

@@ -0,0 +1,123 @@
import t, c
# ==============================================
# SHA256 哈希算法
# ==============================================
SHA256_BLOCK_LEN: t.CDefine = 64
SHA256_DIGEST_LEN: t.CDefine = 32
# SHA256 常量K
sha256_K: t.CArray[t.CUInt32T, 64] = [
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
]
# SHA256 辅助函数
def s0(x: t.CUInt32T) -> t.CUInt32T: return ((x >> 7) | (x << 25)) ^ ((x >> 18) | (x << 14)) ^ ((x >> 3))
def s1(x: t.CUInt32T) -> t.CUInt32T: return ((x >> 17) | (x << 15)) ^ ((x >> 19) | (x << 13)) ^ ((x >> 10))
def S0(x: t.CUInt32T) -> t.CUInt32T: return ((x >> 2) | (x << 30)) ^ ((x >> 13) | (x << 19)) ^ ((x >> 22) | (x << 10))
def S1(x: t.CUInt32T) -> t.CUInt32T: return ((x >> 6) | (x << 26)) ^ ((x >> 11) | (x << 21)) ^ ((x >> 25) | (x << 7))
def Ch(x: t.CUInt32T, y: t.CUInt32T, z: t.CUInt32T) -> t.CUInt32T: return (x & y) ^ (~x & z)
def Maj(x: t.CUInt32T, y: t.CUInt32T, z: t.CUInt32T) -> t.CUInt32T: return (x & y) ^ (x & z) ^ (y & z)
# SHA256 上下文
@t.Object
class sha256:
state: t.CArray[t.CUInt32T, 8]
count: t.CUInt64T
buf: t.CArray[t.CUInt8T, SHA256_BLOCK_LEN]
def __init__(self):
memset(self, 0, sha256.__sizeof__())
self.state[0] = 0x6a09e667
self.state[1] = 0xbb67ae85
self.state[2] = 0x3c6ef372
self.state[3] = 0xa54ff53a
self.state[4] = 0x510e527f
self.state[5] = 0x9b05688c
self.state[6] = 0x1f83d9ab
self.state[7] = 0x5be0cd19
self.count = 0
# memset(self.buf, 0, SHA256_BLOCK_LEN)
# 单分组压缩
def transform(self, block: t.CUInt8T | t.CPtr):
w: t.CArray[t.CUInt32T, 64]
for i in range(16):
w[i] = ((t.CUInt32T(block[i * 4 + 0]) << 24) |
(t.CUInt32T(block[i * 4 + 1]) << 16) |
(t.CUInt32T(block[i * 4 + 2]) << 8 ) |
(t.CUInt32T(block[i * 4 + 3]) << 0 ))
# 扩展64字
for i in range(16, 64):
w[i] = w[i-16] + s0(w[i-15]) + w[i-7] + s1(w[i-2])
a: t.CUInt32T = self.state[0]
b: t.CUInt32T = self.state[1]
c: t.CUInt32T = self.state[2]
d: t.CUInt32T = self.state[3]
e: t.CUInt32T = self.state[4]
f: t.CUInt32T = self.state[5]
g: t.CUInt32T = self.state[6]
h: t.CUInt32T = self.state[7]
t1: t.CUInt32T
t2: t.CUInt32T
# 64步迭代
for i in range(64):
t1 = h + S1(e) + Ch(e,f,g) + sha256_K[i] + w[i]
t2 = S0(a) + Maj(a,b,c)
h = g; g = f; f = e
e = d + t1
d = c; c = b; b = a
a = t1 + t2
# 累加状态
self.state[0] += a
self.state[1] += b
self.state[2] += c
self.state[3] += d
self.state[4] += e
self.state[5] += f
self.state[6] += g
self.state[7] += h
def update(self, s: str):
data: t.CUInt8T | t.CPtr = t.CUInt8T(s, t.CPtr)
length: t.CSizeT = len(s)
idx: t.CSizeT = self.count % SHA256_BLOCK_LEN
self.count += length
if idx > 0:
left: t.CSizeT = SHA256_BLOCK_LEN - idx
if length >= left:
memcpy(self.buf + idx, data, left)
self.transform(self.buf)
data += left
length -= left
idx = 0
while length >= SHA256_BLOCK_LEN:
self.transform(data)
data += SHA256_BLOCK_LEN
length -= SHA256_BLOCK_LEN
if length > 0:
memcpy(self.buf + idx, data, length)
def final(self, out: t.CArray[t.CUInt8T, SHA256_DIGEST_LEN]):
idx: t.CSizeT = self.count % SHA256_BLOCK_LEN
self.buf[idx] = 0x80
idx += 1
if idx > SHA256_BLOCK_LEN - 8:
memset(self.buf + idx, 0, SHA256_BLOCK_LEN - idx)
self.transform(self.buf)
idx = 0
memset(self.buf + idx, 0, SHA256_BLOCK_LEN - 8 - idx)
bits: t.CUInt64T = self.count << 3
for i in range(8):
self.buf[SHA256_BLOCK_LEN-8+i] = (bits >> (56 - i*8)) & 0xFF
self.transform(self.buf)
# 大端输出32字节摘要
for i in range(8):
out[i * 4 + 0] = (self.state[i] >> 24) & 0xFF
out[i * 4 + 1] = (self.state[i] >> 16) & 0xFF
out[i * 4 + 2] = (self.state[i] >> 8) & 0xFF
out[i * 4 + 3] = (self.state[i] >> 0) & 0xFF

View File

@@ -0,0 +1,148 @@
import t, c
# ==============================================
# SHA512 哈希算法
# ==============================================
SHA512_BLOCK_LEN: t.CDefine = 128
SHA512_DIGEST_LEN: t.CDefine = 64
def sha512_ror(x: t.CUInt64T, n: int) -> t.CUInt64T: return (x >> n) | (x << (64 - n))
def sha512_shr(x: t.CUInt64T, n: int) -> t.CUInt64T: return x >> n
def sha512_Ch (x: t.CUInt64T, y: t.CUInt64T, z: t.CUInt64T) -> t.CUInt64T: return (x & y) ^ (~x & z)
def sha512_Maj(x: t.CUInt64T, y: t.CUInt64T, z: t.CUInt64T) -> t.CUInt64T: return (x & y) ^ (x & z) ^ (y & z)
def sha512_BigSigma0(x: t.CUInt64T) -> t.CUInt64T: return sha512_ror(x, 28) ^ sha512_ror(x, 34) ^ sha512_ror(x, 39)
def sha512_BigSigma1(x: t.CUInt64T) -> t.CUInt64T: return sha512_ror(x, 14) ^ sha512_ror(x, 18) ^ sha512_ror(x, 41)
def sha512_Sigma0(x: t.CUInt64T) -> t.CUInt64T: return sha512_ror(x, 1) ^ sha512_ror(x, 8) ^ sha512_shr(x, 7)
def sha512_Sigma1(x: t.CUInt64T) -> t.CUInt64T: return sha512_ror(x, 19) ^ sha512_ror(x, 61) ^ sha512_shr(x, 6)
sha512_K: t.CArray[t.CUInt64T, 80] = [
0x428a2f98d728ae22, 0x7137449123ef65cd, 0xb5c0fbcfec4d3b2f, 0xe9b5dba58189dbbc,
0x3956c25bf348b538, 0x59f111f1b605d019, 0x923f82a4af194f9b, 0xab1c5ed5da6d8118,
0xd807aa98a3030242, 0x12835b0145706fbe, 0x243185be4ee4b28c, 0x550c7dc3d5ffb4e2,
0x72be5d74f27b896f, 0x80deb1fe3b1696b1, 0x9bdc06a725c71235, 0xc19bf174cf692694,
0xe49b69c19ef14ad2, 0xefbe4786384f25e3, 0x0fc19dc68b8cd5b5, 0x240ca1cc77ac9c65,
0x2de92c6f592b0275, 0x4a7484aa6ea6e483, 0x5cb0a9dcbd41fbd4, 0x76f988da831153b5,
0x983e5152ee66dfab, 0xa831c66d2db43210, 0xb00327c898fb213f, 0xbf597fc7beef0ee4,
0xc6e00bf33da88fc2, 0xd5a79147930aa725, 0x06ca6351e003826f, 0x142929670a0e6e70,
0x27b70a8546d22ffc, 0x2e1b21385c26c926, 0x4d2c6dfc5ac42aed, 0x53380d139d95b3df,
0x650a73548baf63de, 0x766a0abb3c77b2a8, 0x81c2c92e47edaee6, 0x92722c851482353b,
0xa2bfe8a14cf10364, 0xa81a664bbc423001, 0xc24b8b70d0f89791, 0xc76c51a30654be30,
0xd192e819d6ef5218, 0xd69906245565a910, 0xf40e35855771202a, 0x106aa07032bbd1b8,
0x19a4c116b8d2d0c8, 0x1e376c085141ab53, 0x2748774cdf8eeb99, 0x34b0bcb5e19b48a8,
0x391c0cb3c5c95a63, 0x4ed8aa4ae3418acb, 0x5b9cca4f7763e373, 0x682e6ff3d6b2b8a3,
0x748f82ee5defb2fc, 0x78a5636f43172f60, 0x84c87814a1f0ab72, 0x8cc702081a6439ec,
0x90befffa23631e28, 0xa4506cebde82bde9, 0xbef9a3f7b2c67915, 0xc67178f2e372532b,
0xca273eceea26619c, 0xd186b8c721c0c207, 0xeada7dd6cde0eb1e, 0xf57d4f7fee6ed178,
0x06f067aa72176fba, 0x0a637dc5a2c898a6, 0x113f9804bef90dae, 0x1b710b35131c471b,
0x28db77f523047d84, 0x32caab7b40c72493, 0x3c9ebe0a15c9bebc, 0x431d67c49c100d4c,
0x4cc5d4becb3e42b6, 0x597f299cfc657e2a, 0x5fcb6fab3ad6faec, 0x6c44198c4a475817
]
@t.Object
class sha512:
state: t.CArray[t.CUInt64T, 8]
count: t.CArray[t.CUInt64T, 2]
buf: t.CArray[t.CUInt8T, SHA512_BLOCK_LEN]
def __init__(self):
memset(self, 0, sha512.__sizeof__())
self.state[0] = 0x6a09e667f3bcc908
self.state[1] = 0xbb67ae8584caa73b
self.state[2] = 0x3c6ef372fe94f82b
self.state[3] = 0xa54ff53a5f1d36f1
self.state[4] = 0x510e527fade682d1
self.state[5] = 0x9b05688c2b3e6c1f
self.state[6] = 0x1f83d9abfb41bd6b
self.state[7] = 0x5be0cd19137e2179
def transform(self, block: t.CArray[t.CUInt8T, SHA512_BLOCK_LEN]):
w: t.CArray[t.CUInt64T, 80]
a: t.CUInt64T
b: t.CUInt64T
c: t.CUInt64T
d: t.CUInt64T
e: t.CUInt64T
f: t.CUInt64T
g: t.CUInt64T
h: t.CUInt64T
t1: t.CUInt64T
t2: t.CUInt64T
for i in range(16):
w[i] = t.CUInt64T(block[i * 8 + 0]) << 56
w[i] |= t.CUInt64T(block[i * 8 + 1]) << 48
w[i] |= t.CUInt64T(block[i * 8 + 2]) << 40
w[i] |= t.CUInt64T(block[i * 8 + 3]) << 32
w[i] |= t.CUInt64T(block[i * 8 + 4]) << 24
w[i] |= t.CUInt64T(block[i * 8 + 5]) << 16
w[i] |= t.CUInt64T(block[i * 8 + 6]) << 8
w[i] |= t.CUInt64T(block[i * 8 + 7]) << 0
for i in range(16, 80):
w[i] = sha512_Sigma1(w[i-2]) + w[i-7] + sha512_Sigma0(w[i-15]) + w[i-16]
a = self.state[0]
b = self.state[1]
c = self.state[2]
d = self.state[3]
e = self.state[4]
f = self.state[5]
g = self.state[6]
h = self.state[7]
for i in range(80):
t1 = h + sha512_BigSigma1(e) + sha512_Ch(e, f, g) + sha512_K[i] + w[i]
t2 = sha512_BigSigma0(a) + sha512_Maj(a, b, c)
h = g; g = f; f = e
e = d + t1
d = c; c = b; b = a
a = t1 + t2
self.state[0] += a
self.state[1] += b
self.state[2] += c
self.state[3] += d
self.state[4] += e
self.state[5] += f
self.state[6] += g
self.state[7] += h
def update(self, s: str):
data: t.CUInt8T | t.CPtr = t.CUInt8T(s, t.CPtr)
length: t.CSizeT = len(s)
idx: t.CUInt32T = t.CUInt32T(self.count[0] % SHA512_BLOCK_LEN)
self.count[0] += length
if self.count[0] < length:
self.count[1] += 1
if idx > 0:
left: t.CSizeT = SHA512_BLOCK_LEN - idx
if length >= left:
memcpy(self.buf + idx, data, left)
self.transform(self.buf)
data += left
length -= left
idx = 0
while length >= SHA512_BLOCK_LEN:
self.transform(data)
data += SHA512_BLOCK_LEN
length -= SHA512_BLOCK_LEN
if length > 0:
memcpy(self.buf + idx, data, length)
def final(self, out: t.CArray[t.CUInt8T, SHA512_DIGEST_LEN]):
bits_lo: t.CUInt64T = self.count[0] << 3
bits_hi: t.CUInt64T = (self.count[1] << 3) | (self.count[0] >> 61)
idx: t.CUInt32T = t.CUInt32T(self.count[0] % SHA512_BLOCK_LEN)
self.buf[idx] = 0x80
idx += 1
if idx > SHA512_BLOCK_LEN - 16:
memset(self.buf + idx, 0, SHA512_BLOCK_LEN - idx)
self.transform(self.buf)
idx = 0
memset(self.buf + idx, 0, SHA512_BLOCK_LEN - 16 - idx)
for i in range(8):
self.buf[SHA512_BLOCK_LEN - 16 + i] = t.CUInt8T(bits_hi >> (56 - i * 8))
for i in range(8):
self.buf[SHA512_BLOCK_LEN - 8 + i] = t.CUInt8T(bits_lo >> (56 - i * 8))
self.transform(self.buf)
for i in range(8):
out[i * 8 + 0] = t.CUInt8T(self.state[i] >> 56)
out[i * 8 + 1] = t.CUInt8T(self.state[i] >> 48)
out[i * 8 + 2] = t.CUInt8T(self.state[i] >> 40)
out[i * 8 + 3] = t.CUInt8T(self.state[i] >> 32)
out[i * 8 + 4] = t.CUInt8T(self.state[i] >> 24)
out[i * 8 + 5] = t.CUInt8T(self.state[i] >> 16)
out[i * 8 + 6] = t.CUInt8T(self.state[i] >> 8)
out[i * 8 + 7] = t.CUInt8T(self.state[i] >> 0)