all repos — min @ 0a9061ff6377f52494863b4d7fec07d197811f17

A small but practical concatenative programming language.

Fixed md4 implementation.
h3rald h3rald@h3rald.com
Thu, 20 Jul 2023 21:15:57 +0000
commit

0a9061ff6377f52494863b4d7fec07d197811f17

parent

d6ba75e2e65676dba2169cc3ebf9fb44f430b5b5

1 files changed, 10 insertions(+), 6 deletions(-)

jump to
M minpkg/lib/min_crypto.nimminpkg/lib/min_crypto.nim

@@ -15,7 +15,8 @@

when defined(ssl): import openssl - + + proc MD4(d: cstring, n: culong, md: cstring = nil): cstring {.cdecl, importc.} proc EVP_MD_CTX_new*(): EVP_MD_CTX {.cdecl, importc: "EVP_MD_CTX_new".} proc EVP_MD_CTX_free*(ctx: EVP_MD_CTX) {.cdecl, importc: "EVP_MD_CTX_free".} else:

@@ -47,12 +48,11 @@ elif defined(macosx):

{.passL: "-Bstatic -L"&getProjectPath()&"/minpkg/vendor/openssl/macosx -lssl -lcrypto -Bdynamic".} proc hash(s: string, kind: EVP_MD, size: int): string = - var hash_length: cuint = 0 - var hash = alloc[ptr uint8](size) + var hash = alloc[ptr cuint](size) let ctx = EVP_MD_CTX_new() discard EVP_DigestInit_ex(ctx, kind, nil) - discard EVP_DigestUpdate(ctx, unsafeAddr s[0], s.len.cuint) - discard EVP_DigestFinal_ex(ctx, hash, cast[ptr cuint](hash_length)) + discard EVP_DigestUpdate(ctx, s.cstring, s.len.cuint) + discard EVP_DigestFinal_ex(ctx, hash, nil) EVP_MD_CTX_free(ctx) var hashStr = newString(size) copyMem(addr(hashStr[0]), hash, size)

@@ -67,7 +67,11 @@

def.symbol("md4") do (i: In): let vals = i.expect("'sym") let s = vals[0].getString - i.push hash(s, EVP_md4(), 32).newVal + var result = "" + var str = MD4(s.cstring, s.len.culong) + for i in 0 ..< 16: + result.add str[i].BiggestInt.toHex(2).toLower + i.push result.newVal def.symbol("sha1") do (i: In): let vals = i.expect("'sym")