修复了大量存在的问题,增加了假鸭子类型等等机制
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user