81 lines
2.1 KiB
Python
81 lines
2.1 KiB
Python
import stdio
|
|
import stdlib
|
|
import string
|
|
import stdint
|
|
import t, c
|
|
import base64
|
|
import hashlib
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def default_value_test_function(a: t.CInt, b: t.CInt = 1):
|
|
print(a, b)
|
|
|
|
|
|
|
|
|
|
# ==============================================
|
|
# 工具函数:打印十六进制哈希串
|
|
# ==============================================
|
|
def print_hex(buf: t.CUInt8T | t.CPtr, length: t.CInt):
|
|
for i in range(length):
|
|
stdio.printf("%02x", buf[i])
|
|
stdio.printf("\n")
|
|
|
|
# ==============================================
|
|
# 主函数测试入口
|
|
# ==============================================
|
|
def main1() -> t.CInt:
|
|
plain: str = "hello123"
|
|
print("原文", plain)
|
|
|
|
# ---------- Base64 测试 ----------
|
|
b64_out: t.CArray[t.CChar, 256]
|
|
b64_dec: t.CArray[t.CUInt8T, 256]
|
|
base64.b64encode(plain, b64_out)
|
|
print("Base64 编码:", b64_out)
|
|
dlen: t.CSizeT = base64.b64decode(b64_out, b64_dec)
|
|
b64_dec[dlen] = 0
|
|
print("Base64 解码:", b64_dec)
|
|
|
|
# ---------- MD5 测试 ----------
|
|
md5_buf: t.CArray[t.CUInt8T, hashlib.MD5_DIGEST_LEN]
|
|
mctx = hashlib.md5()
|
|
mctx.update(plain)
|
|
mctx.final(md5_buf)
|
|
print("MD5: ")
|
|
print_hex(md5_buf, hashlib.MD5_DIGEST_LEN)
|
|
|
|
# ---------- SHA1 测试 ----------
|
|
sha1_buf: t.CArray[t.CUInt8T, hashlib.SHA1_DIGEST_LEN]
|
|
s1ctx = hashlib.sha1()
|
|
s1ctx.update(plain)
|
|
s1ctx.final(sha1_buf)
|
|
print("SHA1: ")
|
|
print_hex(sha1_buf, hashlib.SHA1_DIGEST_LEN)
|
|
|
|
# ---------- SHA256 测试 ----------
|
|
sha256_buf: t.CArray[t.CUInt8T, hashlib.SHA256_DIGEST_LEN]
|
|
s2ctx = hashlib.sha256()
|
|
s2ctx.update(plain)
|
|
s2ctx.final(sha256_buf)
|
|
print("SHA256: ")
|
|
print_hex(sha256_buf, hashlib.SHA256_DIGEST_LEN)
|
|
|
|
# ---------- SHA512 测试 ----------
|
|
sha512_buf: t.CArray[t.CUInt8T, hashlib.SHA512_DIGEST_LEN]
|
|
s5ctx = hashlib.sha512()
|
|
s5ctx.update(plain)
|
|
s5ctx.final(sha512_buf)
|
|
print("SHA512: ")
|
|
print_hex(sha512_buf, hashlib.SHA512_DIGEST_LEN)
|
|
|
|
default_value_test_function(b=2, a=1)
|
|
|
|
return 0
|