将 hashlib 英化

This commit is contained in:
TermiNexus committed 2026-08-07 18:52:12 +08:00
1 parent c5570dc8b1
commit 02479420e8
4 files changed
+43 -42

No files matched your search

+24 -23
View File
@@ -3,20 +3,20 @@ import t, c
# ==============================================
# MD5 哈希算法
# MD5 hash algorithm
# ==============================================
MD5_BLOCK_LEN: t.CDefine = 64 # 分组块大小
MD5_DIGEST_LEN: t.CDefine = 16 # 摘要输出长度
MD5_BLOCK_LEN: t.CDefine = 64 # Group block size
MD5_DIGEST_LEN: t.CDefine = 16 # Digest output length
# MD5 四轮非线性函数
# MD5 four-round nonlinear functions
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)
# 循环左移
# Circular left shift
def md5_rotl(x: t.CUInt32T, n: t.CInt) -> t.CUInt32T: return (x << n) | (x >> (32 - n))
# MD5 常量表、移位表
# MD5 constant table
md5_T: t.CArray[t.CUInt32T, 64] = [
0xd76aa478, 0xe8c7b756, 0x242070db, 0xc1bdceee, 0xf57c0faf, 0x4787c62a, 0xa8304613, 0xfd469501,
0x698098d8, 0x8b44f7af, 0xffff5bb1, 0x895cd7be, 0x6b901122, 0xfd987193, 0xa679438e, 0x49b40821,
@@ -35,13 +35,14 @@ md5_S: t.CArray[t.CInt, 64] = [
6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21
]
# MD5 上下文结构体
# MD5 context structure
# ============================================================
@t.Object
class md5:
state: t.CArray[t.CUInt32T, 4] # 4个32位状态寄存器
count: t.CUInt64T # 已处理比特数
buf: t.CArray[t.CUInt8T, MD5_BLOCK_LEN] # 分组缓冲区
# 初始化MD5上下文
state: t.CArray[t.CUInt32T, 4] # 4 32-bit state registers
count: t.CUInt64T # processed bits
buf: t.CArray[t.CUInt8T, MD5_BLOCK_LEN] # Group buffer
# Initialize MD5 context
def __init__(self):
string.memset(self.buf, 0, MD5_BLOCK_LEN)
self.state[0] = 0x67452301
@@ -49,25 +50,25 @@ class md5:
self.state[2] = 0x98badcfe
self.state[3] = 0x10325476
self.count = 0
# MD5 单分组压缩变换
# MD5 single-group compression transform
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位字
# Byte order 32-bit words
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步迭代
# 4-round 51 iterations
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 参数
# 4-round 51 iterations
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
@@ -80,12 +81,12 @@ class md5:
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])
# 累加到状态
# Accumulate to state registers
self.state[0] += a
self.state[1] += b
self.state[2] += c
self.state[3] += d
# 分批输入数据
# Batch input data
def update(self, s: str):
data: t.CUInt8T | t.CPtr = t.CUInt8T(s, t.CPtr)
length: t.CSizeT = len(s)
@@ -105,25 +106,25 @@ class md5:
length -= MD5_BLOCK_LEN
if length > 0:
string.memcpy(self.buf + idx, data, length)
# 结束计算,输出16字节摘要
# End calculation, output 16-byte digest
def final(self, out: t.CArray[t.CUInt8T, MD5_DIGEST_LEN]):
idx: t.CSizeT = self.count % MD5_BLOCK_LEN
# 填充0x80结尾
# Pad 0x80 end
self.buf[idx] = 0x80
idx += 1
# 空间不够则先压缩一次
# If space is not enough, compress once
if idx > MD5_BLOCK_LEN - 8:
string.memset(self.buf + idx, 0, MD5_BLOCK_LEN - idx)
self.transform(self.buf)
idx = 0
# 补0到末尾8字节
# Pad 0 to end 8 bytes
string.memset(self.buf + idx, 0, MD5_BLOCK_LEN - 8 - idx)
# 写入总比特数
# Write total bit count
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)
# 状态转小端字节输出
# State registers to little-endian byte output output
for i in range(4):
out[i * 4 + 0] = (self.state[i] >> 0) & 0xFF
out[i * 4 + 1] = (self.state[i] >> 8) & 0xFF
+9 -9
View File
@@ -4,18 +4,18 @@ import t, c
# ==============================================
# SHA1 哈希算法
# SHA1 Hash Algorithm
# ==============================================
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))
# 三轮逻辑函数
# 3-round logical functions
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 上下文
# SHA1 Context
@t.Object
class sha1:
state: t.CArray[t.CUInt32T, 5]
@@ -29,17 +29,17 @@ class sha1:
self.state[3] = 0x10325476
self.state[4] = 0xc3d2e1f0
self.count = 0
# 单分组压缩
# Single-group compression transform
def transform(self, block: t.CUInt8T | t.CPtr):
w: t.CArray[t.CUInt32T, 80]
data: t.CUInt8T | t.CPtr = block
# 前16个字
# First 16 words
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个字
# Extend to 80 words
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]
@@ -49,7 +49,7 @@ class sha1:
e: t.CUInt32T = self.state[4]
k: t.CUInt32T
tmp: t.CUInt32T
# 4轮迭代
# 4-round iterations
for i in range(0, 20):
k = 0x5a827999
tmp = sha1_rotl(a,5) + sha1_f1(b, c, d) + e + k + w[i]
@@ -108,11 +108,11 @@ class sha1:
idx = 0
memset(self.buf + idx,0,SHA1_BLOCK_LEN-8-idx)
bits: t.CUInt64T = self.count << 3
# SHA1 大端写入长度
# SHA1 big-endian write length
for i in range(8):
self.buf[SHA1_BLOCK_LEN - 8 + i] = (bits >> (56 - i * 8)) & 0xFF
self.transform(self.buf)
# 大端输出摘要
# State registers to big-endian byte output output output
for i in range(5):
out[i * 4 + 0] = (self.state[i] >> 24) & 0xFF
out[i * 4 + 1] = (self.state[i] >> 16) & 0xFF
+9 -9
View File
@@ -2,12 +2,12 @@ import t, c
# ==============================================
# SHA256 哈希算法
# SHA256 Hash Algorithm
# ==============================================
SHA256_BLOCK_LEN: t.CDefine = 64
SHA256_DIGEST_LEN: t.CDefine = 32
# SHA256 常量K
# SHA256 Constants K
sha256_K: t.CArray[t.CUInt32T, 64] = [
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
@@ -19,7 +19,7 @@ sha256_K: t.CArray[t.CUInt32T, 64] = [
0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
]
# SHA256 辅助函数
# SHA256 Auxiliary Functions
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))
@@ -27,7 +27,7 @@ def S1(x: t.CUInt32T) -> t.CUInt32T: return ((x >> 6) | (x << 26)) ^ ((x >> 11
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 上下文
# SHA256 Context
@t.Object
class sha256:
state: t.CArray[t.CUInt32T, 8]
@@ -45,7 +45,7 @@ class sha256:
self.state[7] = 0x5be0cd19
self.count = 0
# memset(self.buf, 0, SHA256_BLOCK_LEN)
# 单分组压缩
# SHA256 single-group compression transform
def transform(self, block: t.CUInt8T | t.CPtr):
w: t.CArray[t.CUInt32T, 64]
for i in range(16):
@@ -53,7 +53,7 @@ class sha256:
(t.CUInt32T(block[i * 4 + 1]) << 16) |
(t.CUInt32T(block[i * 4 + 2]) << 8 ) |
(t.CUInt32T(block[i * 4 + 3]) << 0 ))
# 扩展64字
# Extend to 64 words
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]
@@ -66,7 +66,7 @@ class sha256:
h: t.CUInt32T = self.state[7]
t1: t.CUInt32T
t2: t.CUInt32T
# 64步迭代
# 64-round iterations
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)
@@ -74,7 +74,7 @@ class sha256:
e = d + t1
d = c; c = b; b = a
a = t1 + t2
# 累加状态
# Accumulate to state registers
self.state[0] += a
self.state[1] += b
self.state[2] += c
@@ -115,7 +115,7 @@ class sha256:
for i in range(8):
self.buf[SHA256_BLOCK_LEN-8+i] = (bits >> (56 - i*8)) & 0xFF
self.transform(self.buf)
# 大端输出32字节摘要
# Byte order 32-bit words
for i in range(8):
out[i * 4 + 0] = (self.state[i] >> 24) & 0xFF
out[i * 4 + 1] = (self.state[i] >> 16) & 0xFF
+1 -1
View File
@@ -2,7 +2,7 @@ import t, c
# ==============================================
# SHA512 哈希算法
# SHA512 Hash Algorithm
# ==============================================
SHA512_BLOCK_LEN: t.CDefine = 128
SHA512_DIGEST_LEN: t.CDefine = 64