Initial import of TransPyV
This commit is contained in:
62
Test/Sha1Test/App/main.py
Normal file
62
Test/Sha1Test/App/main.py
Normal file
@@ -0,0 +1,62 @@
|
||||
import t, c
|
||||
from stdint import *
|
||||
import stdio
|
||||
import stdlib
|
||||
import memhub
|
||||
import hashlib
|
||||
|
||||
|
||||
POOL_SIZE: t.CDefine = 16777480
|
||||
|
||||
|
||||
@t.CExport
|
||||
def main() -> int:
|
||||
arena: bytes = stdlib.malloc(POOL_SIZE)
|
||||
if arena is None:
|
||||
return 1
|
||||
mb: memhub.MemBuddy | t.CPtr = memhub.MemBuddy(arena, POOL_SIZE)
|
||||
if mb is None:
|
||||
return 1
|
||||
hashlib._mbuddy = (memhub.MemManager | t.CPtr)(mb)
|
||||
|
||||
# 测试 "abc" 的 SHA1
|
||||
# 期望: a9993e364706816aba3e25717850c26c9cd0d89d
|
||||
s: str = "abc"
|
||||
stdio.printf("input: %s\n", s)
|
||||
|
||||
# 计算 len(s)
|
||||
n: t.CSizeT = len(s)
|
||||
stdio.printf("len=%d\n", n)
|
||||
|
||||
# 构造 sha1 对象
|
||||
ctx: hashlib.sha1 | t.CPtr = hashlib.sha1()
|
||||
if ctx is None:
|
||||
stdio.printf("ctx is None\n")
|
||||
return 1
|
||||
|
||||
ctx.update(s)
|
||||
|
||||
digest: bytes = mb.alloc(hashlib.SHA1_DIGEST_LEN)
|
||||
if digest is None:
|
||||
stdio.printf("digest alloc failed\n")
|
||||
return 1
|
||||
|
||||
ctx.final(digest)
|
||||
|
||||
# 打印 digest(十六进制)
|
||||
stdio.printf("SHA1: ")
|
||||
for i in range(hashlib.SHA1_DIGEST_LEN):
|
||||
b: t.CUInt8T = digest[i]
|
||||
hi: t.CInt = (b >> 4) & 0xF
|
||||
lo: t.CInt = b & 0xF
|
||||
if hi < 10:
|
||||
stdio.printf("%c", '0' + hi)
|
||||
else:
|
||||
stdio.printf("%c", 'a' + (hi - 10))
|
||||
if lo < 10:
|
||||
stdio.printf("%c", '0' + lo)
|
||||
else:
|
||||
stdio.printf("%c", 'a' + (lo - 10))
|
||||
stdio.printf("\n")
|
||||
stdio.printf("expect: a9993e364706816aba3e25717850c26c9cd0d89d\n")
|
||||
return 0
|
||||
28
Test/Sha1Test/project.vpj
Normal file
28
Test/Sha1Test/project.vpj
Normal file
@@ -0,0 +1,28 @@
|
||||
{
|
||||
"name": "Sha1Test",
|
||||
"version": "1.0.0",
|
||||
"source_dir": "./App",
|
||||
"temp_dir": "./temp",
|
||||
"output_dir": "./output",
|
||||
"compiler": {
|
||||
"cmd": "llc",
|
||||
"flags": ["-filetype=obj", "-relocation-model=pic"]
|
||||
},
|
||||
"linker": {
|
||||
"cmd": "clang++",
|
||||
"flags": ["-lmsvcrt", "-lucrt", "-lpthread", "-lmingwex", "-lkernel32", "-Wl,--allow-multiple-definition"],
|
||||
"output": "sha1test.exe"
|
||||
},
|
||||
"includes": [
|
||||
"../../../includes"
|
||||
],
|
||||
"target": {
|
||||
"triple": "x86_64-pc-windows-gnu",
|
||||
"datalayout": "e-m:w-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
|
||||
},
|
||||
"options": {
|
||||
"slice_level": 3,
|
||||
"target": "llvm",
|
||||
"strict_mode": true
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user