From bdd241cbeadb6f05325f5c51f48b61b654a87b4d Mon Sep 17 00:00:00 2001 From: cheatfate Date: Wed, 14 Mar 2018 13:55:41 +0200 Subject: [PATCH] first attempt to create digest() --- hash.nim | 4 +++- keccak.nim | 20 ++++++++++++++------ 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/hash.nim b/hash.nim index 541827d..375bda5 100644 --- a/hash.nim +++ b/hash.nim @@ -7,6 +7,8 @@ type proc `$`*(digest: MDigest): string = result = "" var i = 0'u - while i < len(digest.data): + while i < uint(len(digest.data)): result &= hexChar(cast[uint8](digest.data[i])) inc(i) + +var x = keccak224.digest(nil, 0) \ No newline at end of file diff --git a/keccak.nim b/keccak.nim index 7acf468..b3b275b 100644 --- a/keccak.nim +++ b/keccak.nim @@ -35,7 +35,7 @@ type KeccakKind = enum Sha3, Keccak, Shake - KeccakContext[bits: static[uint], + KeccakContext[bits: static[int], kind: static[KeccakKind]] = object q: array[25, uint64] pt: int @@ -287,9 +287,17 @@ proc finish*(ctx: var KeccakContext, data: ptr uint8, ulen: uint): uint = d[i] = s[i] result = ctx.sizeDigest -proc finish*[bits: static[uint], kind: static[KeccakKind]](ctx: var KeccakContext[bits, kind]): MDigest[bits] = - discard +proc finish*[B: static[int], + K: static[KeccakKind]](ctx: var KeccakContext[B, K]): MDigest[B] = + discard finish(ctx, cast[ptr uint8](addr result.data[0]), + uint(len(result.data))) -# proc finish*(ctx: var KeccakContext): MdDigest[ctx.bits] = -# result.size = finish(ctx, cast[ptr uint8](addr result.data[0]), -# MaxMdDigestLength) +proc digest*(HashType: typedesc, data: ptr uint8, + ulen: uint): MDigest[HashType.bits] = + var ctx: HashType + ctx.init() + ctx.update(data, dataLen) + result = ctx.finish() + +when isMainModule: + echo $digest[keccak224](nil, 0)