修复了大量存在的问题,增加了假鸭子类型等等机制
This commit is contained in:
@@ -16,7 +16,7 @@ def md5_I(x: t.CUInt32T, y: t.CUInt32T, z: t.CUInt32T) -> t.CUInt32T: return y ^
|
||||
def md5_rotl(x: t.CUInt32T, n: t.CInt) -> t.CUInt32T: return (x << n) | (x >> (32 - n))
|
||||
|
||||
# MD5 常量表、移位表
|
||||
md5_T: list[t.CUInt32T, 64] = [
|
||||
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,
|
||||
@@ -27,7 +27,7 @@ md5_T: list[t.CUInt32T, 64] = [
|
||||
0x6fa87e4f, 0xfe2ce6e0, 0xa3014314, 0x4e0811a1, 0xf7537e82, 0xbd3af235, 0x2ad7d2bb, 0xeb86d391
|
||||
]
|
||||
|
||||
md5_S: list[t.CInt, 64] = [
|
||||
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,
|
||||
@@ -37,9 +37,9 @@ md5_S: list[t.CInt, 64] = [
|
||||
# MD5 上下文结构体
|
||||
@t.Object
|
||||
class md5:
|
||||
state: list[t.CUInt32T, 4] # 4个32位状态寄存器
|
||||
state: t.CArray[t.CUInt32T, 4] # 4个32位状态寄存器
|
||||
count: t.CUInt64T # 已处理比特数
|
||||
buf: list[t.CUInt8T, MD5_BLOCK_LEN] # 分组缓冲区
|
||||
buf: t.CArray[t.CUInt8T, MD5_BLOCK_LEN] # 分组缓冲区
|
||||
# 初始化MD5上下文
|
||||
def __init__(self):
|
||||
memset(self.buf, 0, MD5_BLOCK_LEN)
|
||||
@@ -54,7 +54,7 @@ class md5:
|
||||
b: t.CUInt32T = self.state[1]
|
||||
c: t.CUInt32T = self.state[2]
|
||||
d: t.CUInt32T = self.state[3]
|
||||
w: list[t.CUInt32T, 16]
|
||||
w: t.CArray[t.CUInt32T, 16]
|
||||
# 字节序转32位字
|
||||
for i in range(16):
|
||||
w[i] = ((t.CUInt32T(block[i * 4 + 0]) << 0) |
|
||||
@@ -105,7 +105,7 @@ class md5:
|
||||
if length > 0:
|
||||
memcpy(self.buf + idx, data, length)
|
||||
# 结束计算,输出16字节摘要
|
||||
def final(self, out: list[t.CUInt8T, MD5_DIGEST_LEN]):
|
||||
def final(self, out: t.CArray[t.CUInt8T, MD5_DIGEST_LEN]):
|
||||
idx: t.CSizeT = self.count % MD5_BLOCK_LEN
|
||||
# 填充0x80结尾
|
||||
self.buf[idx] = 0x80
|
||||
|
||||
@@ -17,9 +17,9 @@ def sha1_f3(b: t.CUInt32T, c: t.CUInt32T, d: t.CUInt32T) -> t.CUInt32T: return (
|
||||
# SHA1 上下文
|
||||
@t.Object
|
||||
class sha1:
|
||||
state: list[t.CUInt32T, 5]
|
||||
state: t.CArray[t.CUInt32T, 5]
|
||||
count: t.CUInt64T
|
||||
buf: list[t.CUInt8T, SHA1_BLOCK_LEN]
|
||||
buf: t.CArray[t.CUInt8T, SHA1_BLOCK_LEN]
|
||||
def __init__(self):
|
||||
memset(self.buf, 0, SHA1_BLOCK_LEN)
|
||||
self.state[0] = 0x67452301
|
||||
@@ -30,7 +30,7 @@ class sha1:
|
||||
self.count = 0
|
||||
# 单分组压缩
|
||||
def transform(self, block: t.CUInt8T | t.CPtr):
|
||||
w: list[t.CUInt32T, 80]
|
||||
w: t.CArray[t.CUInt32T, 80]
|
||||
data: t.CUInt8T | t.CPtr = block
|
||||
# 前16个字
|
||||
for i in range(16):
|
||||
@@ -97,7 +97,7 @@ class sha1:
|
||||
length -= SHA1_BLOCK_LEN
|
||||
if length > 0:
|
||||
memcpy(self.buf + idx, data, length)
|
||||
def final(self, out: list[t.CUInt8T, SHA1_DIGEST_LEN]):
|
||||
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
|
||||
|
||||
@@ -8,7 +8,7 @@ SHA256_BLOCK_LEN: t.CDefine = 64
|
||||
SHA256_DIGEST_LEN: t.CDefine = 32
|
||||
|
||||
# SHA256 常量K
|
||||
sha256_K: list[t.CUInt32T, 64] = [
|
||||
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,
|
||||
@@ -30,9 +30,9 @@ def Maj(x: t.CUInt32T, y: t.CUInt32T, z: t.CUInt32T) -> t.CUInt32T: return (x &
|
||||
# SHA256 上下文
|
||||
@t.Object
|
||||
class sha256:
|
||||
state: list[t.CUInt32T, 8]
|
||||
state: t.CArray[t.CUInt32T, 8]
|
||||
count: t.CUInt64T
|
||||
buf: list[t.CUInt8T, SHA256_BLOCK_LEN]
|
||||
buf: t.CArray[t.CUInt8T, SHA256_BLOCK_LEN]
|
||||
def __init__(self):
|
||||
memset(self, 0, sha256.__sizeof__())
|
||||
self.state[0] = 0x6a09e667
|
||||
@@ -47,7 +47,7 @@ class sha256:
|
||||
# memset(self.buf, 0, SHA256_BLOCK_LEN)
|
||||
# 单分组压缩
|
||||
def transform(self, block: t.CUInt8T | t.CPtr):
|
||||
w: list[t.CUInt32T, 64]
|
||||
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) |
|
||||
@@ -102,7 +102,7 @@ class sha256:
|
||||
length -= SHA256_BLOCK_LEN
|
||||
if length > 0:
|
||||
memcpy(self.buf + idx, data, length)
|
||||
def final(self, out: list[t.CUInt8T, SHA256_DIGEST_LEN]):
|
||||
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
|
||||
|
||||
@@ -16,7 +16,7 @@ def sha512_BigSigma1(x: t.CUInt64T) -> t.CUInt64T: return sha512_ror(x, 14) ^ sh
|
||||
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: list[t.CUInt64T, 80] = [
|
||||
sha512_K: t.CArray[t.CUInt64T, 80] = [
|
||||
0x428a2f98d728ae22, 0x7137449123ef65cd, 0xb5c0fbcfec4d3b2f, 0xe9b5dba58189dbbc,
|
||||
0x3956c25bf348b538, 0x59f111f1b605d019, 0x923f82a4af194f9b, 0xab1c5ed5da6d8118,
|
||||
0xd807aa98a3030242, 0x12835b0145706fbe, 0x243185be4ee4b28c, 0x550c7dc3d5ffb4e2,
|
||||
@@ -41,9 +41,9 @@ sha512_K: list[t.CUInt64T, 80] = [
|
||||
|
||||
@t.Object
|
||||
class sha512:
|
||||
state: list[t.CUInt64T, 8]
|
||||
count: list[t.CUInt64T, 2]
|
||||
buf: list[t.CUInt8T, SHA512_BLOCK_LEN]
|
||||
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
|
||||
@@ -54,8 +54,8 @@ class sha512:
|
||||
self.state[5] = 0x9b05688c2b3e6c1f
|
||||
self.state[6] = 0x1f83d9abfb41bd6b
|
||||
self.state[7] = 0x5be0cd19137e2179
|
||||
def transform(self, block: list[t.CUInt8T, SHA512_BLOCK_LEN]):
|
||||
w: list[t.CUInt64T, 80]
|
||||
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
|
||||
@@ -121,7 +121,7 @@ class sha512:
|
||||
length -= SHA512_BLOCK_LEN
|
||||
if length > 0:
|
||||
memcpy(self.buf + idx, data, length)
|
||||
def final(self, out: list[t.CUInt8T, SHA512_DIGEST_LEN]):
|
||||
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)
|
||||
|
||||
Reference in New Issue
Block a user