all repos — min @ 891cd32dfa91fd3b5be69e8c3d5aab984acdb716

A small but practical concatenative programming language.

Merge branch 'next' of github.com:h3rald/min into next
h3rald h3rald@h3rald.com
Wed, 16 Dec 2020 03:40:32 +0100
commit

891cd32dfa91fd3b5be69e8c3d5aab984acdb716

parent

4fd384f43567d7f952e21d9d061eedc411681708

M core/utils.nimcore/utils.nim

@@ -164,13 +164,7 @@ result = json.getStr.newVal

of JObject: var res = newDict(i.scope) for key, value in json.pairs: - var first = $key[0] - var rest = "" - if key.len > 1: - rest = key[1..key.len-1] - #first = sgregex.replace(first, peg"[^a-zA-Z0-9_]", "_") - #rest = sgregex.replace(rest, peg"[^a-zA-Z0-9/!?+*._-]", "_") - discard i.dset(res, first&rest, i.fromJson(value)) + discard i.dset(res, key, i.fromJson(value)) return res of JArray: var res = newSeq[MinValue](0)
M lib/min_crypto.nimlib/min_crypto.nim

@@ -1,7 +1,7 @@

import - md5, base64, strutils, + openssl, times import ../core/parser,

@@ -9,44 +9,70 @@ ../core/value,

../core/interpreter, ../core/utils import - ../packages/sha1/sha1, - ../packages/nimSHA2/nimSHA2, ../vendor/aes/aes {.compile: "../vendor/aes/libaes.c".} +when defined(windows): + {.passL: "-static -Lvendor/openssl/windows -lssl -lcrypto -lws2_32".} +elif defined(linux): + {.passL: "-static -Lvendor/openssl/linux -lssl -lcrypto".} +elif defined(macosx): + {.passL: "-Bstatic -Lvendor/openssl/macosx -lssl -lcrypto -Bdynamic".} + +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".} + +proc hash(s: string, kind: EVP_MD, size: int): string = + var hash_length: cuint = 0 + var hash = alloc[ptr cuchar](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)) + EVP_MD_CTX_free(ctx) + var hashStr = newString(size) + copyMem(addr(hashStr[0]), hash, size) + dealloc(hash) + return hashStr.toHex.toLowerAscii[0..size-1] + proc crypto_module*(i: In)= let def = i.define() def.symbol("md5") do (i: In): let vals = i.expect("'sym") - let s = vals[0] - i.push s.getString.getMD5.newVal + let s = vals[0].getString + i.push hash(s, EVP_md5(), 32).newVal + + def.symbol("md4") do (i: In): + let vals = i.expect("'sym") + let s = vals[0].getString + i.push hash(s, EVP_md4(), 32).newVal def.symbol("sha1") do (i: In): let vals = i.expect("'sym") - let s = vals[0] - i.push compute(s.getString).toHex.newVal + var s = vals[0].getString + i.push hash(s, EVP_sha1(), 40).newVal def.symbol("sha224") do (i: In): let vals = i.expect("'sym") - let s = vals[0] - i.push computeSHA224(s.getString).hex.toLowerAscii.newVal + let s = vals[0].getString + i.push hash(s, EVP_sha224(), 56).newVal def.symbol("sha256") do (i: In): let vals = i.expect("'sym") - let s = vals[0] - i.push computeSHA256(s.getString).hex.toLowerAscii.newVal + let s = vals[0].getString + i.push hash(s, EVP_sha256(), 64).newVal def.symbol("sha384") do (i: In): let vals = i.expect("'sym") - let s = vals[0] - i.push computeSHA384(s.getString).hex.toLowerAscii.newVal + let s = vals[0].getString + i.push hash(s, EVP_sha384(), 96).newVal def.symbol("sha512") do (i: In): let vals = i.expect("'sym") - let s = vals[0] - i.push computeSHA512(s.getString).hex.toLowerAscii.newVal + let s = vals[0].getString + i.push hash(s, EVP_sha512(), 128).newVal def.symbol("encode") do (i: In): let vals = i.expect("'sym")

@@ -57,14 +83,14 @@ def.symbol("decode") do (i: In):

let vals = i.expect("'sym") let s = vals[0] i.push s.getString.decode.newVal - + def.symbol("aes") do (i: In): let vals = i.expect("'sym", "'sym") let k = vals[0] let s = vals[1] var text = s.getString - var key = k.getString.compute.toHex - var iv = (key & $getTime().toUnix).compute.toHex + var key = hash(k.getString, EVP_sha1(), 40) + var iv = hash((key & $getTime().toUnix), EVP_sha1(), 40) var ctx = cast[ptr AES_ctx](alloc0(sizeof(AES_ctx))) AES_init_ctx_iv(ctx, cast[ptr uint8](key[0].addr), cast[ptr uint8](iv[0].addr)); var input = cast[ptr uint8](text[0].addr)
M next-release.mdnext-release.md

@@ -2,3 +2,5 @@ * Added support for sigils on double-quoted strings.

* Added support for arbitrary strings as dictionary keys. * Added **define-sigil**, **delete-sigil**, **seal-sigil**, **unseal-sigil**, **defined-sigil?**. * Fixed behavior of **semver-inc-mahor** and **semver-inc-minor** to set lower digits to zero. +* Now using OpenSSL for all hashing symbols in the crypto module. +* Added **md4** symbol.
M nifty.jsonnifty.json

@@ -48,16 +48,6 @@ "name": "nim-miniz",

"src": "https://github.com/h3rald/nim-miniz.git", "git": true }, - "nimSHA2": { - "name": "nimSHA2", - "src": "https://github.com/jangko/nimSHA2.git", - "git": true - }, - "sha1": { - "name": "sha1", - "src": "https://github.com/onionhammer/sha1.git", - "git": true - }, "nimline": { "name": "nimline", "src": "https://github.com/h3rald/nimline.git",
M run.minrun.min

@@ -2,7 +2,6 @@ #!/usr/bin/env min

;Capture a reference of default symbols before more are added symbols =min-symbols - 2 :n-args 1 :taskspec-arg
M site/contents/reference-crypto.mdsite/contents/reference-crypto.md

@@ -13,6 +13,9 @@

{#op||encode||{{sl}}||{{s}}|| Base64-encodes {{sl}}. #} +{#op||md4||{{sl}}||{{s}}|| +Returns the MD4 hash of {{sl}}. #} + {#op||md5||{{sl}}||{{s}}|| Returns the MD5 hash of {{sl}}. #}
M tests/crypto.mintests/crypto.min

@@ -3,6 +3,8 @@ 'test import

"crypto" describe + ("test" md4 "db346d691d7acc4dc2625db19f9e3f52" ==) assert + ("test" md5 "098f6bcd4621d373cade4e832627b4f6" ==) assert ("test" sha1 "a94a8fe5ccb19ba61c4c0873d391e987982fbbd3" ==) assert