mirror of
https://github.com/status-im/nim-keccak-tiny.git
synced 2025-02-22 11:18:28 +00:00
use the new status-im/nim-ranges library
This commit is contained in:
parent
05a49a3922
commit
92fb544b1a
@ -1,14 +1,12 @@
|
|||||||
{.compile: "keccak-tiny/keccak-tiny.c".}
|
{.compile: "keccak-tiny/keccak-tiny.c".}
|
||||||
|
|
||||||
import
|
import
|
||||||
strutils, parseutils
|
strutils, parseutils, ranges/memranges
|
||||||
|
|
||||||
type
|
type
|
||||||
Hash*[bits: static[int]] = object
|
Hash*[bits: static[int]] = object
|
||||||
data*: array[bits div 8, uint8]
|
data*: array[bits div 8, uint8]
|
||||||
|
|
||||||
InputRange* = (pointer, csize)
|
|
||||||
|
|
||||||
proc `$`*(h: Hash): string =
|
proc `$`*(h: Hash): string =
|
||||||
result = newStringOfCap(high(h.data) * 2)
|
result = newStringOfCap(high(h.data) * 2)
|
||||||
for byte in h.data:
|
for byte in h.data:
|
||||||
@ -29,9 +27,6 @@ proc hashFromHex*(bits: static[int], input: string): Hash[bits] =
|
|||||||
|
|
||||||
template hashFromHex*(s: static[string]): untyped = hashFromHex(s.len * 4, s)
|
template hashFromHex*(s: static[string]): untyped = hashFromHex(s.len * 4, s)
|
||||||
|
|
||||||
proc toInputRange(x: string): InputRange = (x.cstring.pointer, x.len)
|
|
||||||
proc toInputRange[T](x: openarray[T]): InputRange = (cast[pointer](x), x.len * T.sizeof)
|
|
||||||
|
|
||||||
proc extSha224(output: pointer, outSize: csize, input: pointer, inputSize: csize) {.importc: "sha3_224".}
|
proc extSha224(output: pointer, outSize: csize, input: pointer, inputSize: csize) {.importc: "sha3_224".}
|
||||||
proc extSha256(output: pointer, outSize: csize, input: pointer, inputSize: csize) {.importc: "sha3_256".}
|
proc extSha256(output: pointer, outSize: csize, input: pointer, inputSize: csize) {.importc: "sha3_256".}
|
||||||
proc extSha384(output: pointer, outSize: csize, input: pointer, inputSize: csize) {.importc: "sha3_384".}
|
proc extSha384(output: pointer, outSize: csize, input: pointer, inputSize: csize) {.importc: "sha3_384".}
|
||||||
@ -43,63 +38,63 @@ proc extKeccak512(output: pointer, outSize: csize, input: pointer, inputSize: cs
|
|||||||
proc extShake128(output: pointer, outSize: csize, input: pointer, inputSize: csize) {.importc: "shake128".}
|
proc extShake128(output: pointer, outSize: csize, input: pointer, inputSize: csize) {.importc: "shake128".}
|
||||||
proc extShake256(output: pointer, outSize: csize, input: pointer, inputSize: csize) {.importc: "shake256".}
|
proc extShake256(output: pointer, outSize: csize, input: pointer, inputSize: csize) {.importc: "shake256".}
|
||||||
|
|
||||||
proc sha3_224(input: InputRange): Hash[224] =
|
proc sha3_224(input: MemRange): Hash[224] =
|
||||||
extSha224(addr(result.data), 224 div 8, input[0], input[1])
|
extSha224(addr(result.data), 224 div 8, input.baseAddr, input.len)
|
||||||
|
|
||||||
template sha3_224*(input: typed): Hash[224] =
|
template sha3_224*(input: typed): Hash[224] =
|
||||||
sha3_224(toInputRange(input))
|
sha3_224(toMemRange(input))
|
||||||
|
|
||||||
proc sha3_256(input: InputRange): Hash[256] =
|
proc sha3_256(input: MemRange): Hash[256] =
|
||||||
extSha256(addr(result.data), 256 div 8, input[0], input[1])
|
extSha256(addr(result.data), 256 div 8, input.baseAddr, input.len)
|
||||||
|
|
||||||
template sha3_256*(input: typed): Hash[256] =
|
template sha3_256*(input: typed): Hash[256] =
|
||||||
sha3_256(toInputRange(input))
|
sha3_256(toMemRange(input))
|
||||||
|
|
||||||
proc sha3_384(input: InputRange): Hash[384] =
|
proc sha3_384(input: MemRange): Hash[384] =
|
||||||
extSha384(addr(result.data), 384 div 8, input[0], input[1])
|
extSha384(addr(result.data), 384 div 8, input.baseAddr, input.len)
|
||||||
|
|
||||||
template sha3_384*(input: typed): Hash[384] =
|
template sha3_384*(input: typed): Hash[384] =
|
||||||
sha3_384(toInputRange(input))
|
sha3_384(toMemRange(input))
|
||||||
|
|
||||||
proc sha3_512(input: InputRange): Hash[512] =
|
proc sha3_512(input: MemRange): Hash[512] =
|
||||||
extSha512(addr(result.data), 512 div 8, input[0], input[1])
|
extSha512(addr(result.data), 512 div 8, input.baseAddr, input.len)
|
||||||
|
|
||||||
template sha3_512*(input: typed): Hash[512] =
|
template sha3_512*(input: typed): Hash[512] =
|
||||||
sha3_512(toInputRange(input))
|
sha3_512(toMemRange(input))
|
||||||
|
|
||||||
proc keccak_224(input: InputRange): Hash[224] =
|
proc keccak_224(input: MemRange): Hash[224] =
|
||||||
extKeccak224(addr(result.data), 224 div 8, input[0], input[1])
|
extKeccak224(addr(result.data), 224 div 8, input.baseAddr, input.len)
|
||||||
|
|
||||||
template keccak_224*(input: typed): Hash[224] =
|
template keccak_224*(input: typed): Hash[224] =
|
||||||
keccak_224(toInputRange(input))
|
keccak_224(toMemRange(input))
|
||||||
|
|
||||||
proc keccak_256(input: InputRange): Hash[256] =
|
proc keccak_256(input: MemRange): Hash[256] =
|
||||||
extKeccak256(addr(result.data), 256 div 8, input[0], input[1])
|
extKeccak256(addr(result.data), 256 div 8, input.baseAddr, input.len)
|
||||||
|
|
||||||
template keccak_256*(input: typed): Hash[256] =
|
template keccak_256*(input: typed): Hash[256] =
|
||||||
keccak_256(toInputRange(input))
|
keccak_256(toMemRange(input))
|
||||||
|
|
||||||
proc keccak_384(input: InputRange): Hash[384] =
|
proc keccak_384(input: MemRange): Hash[384] =
|
||||||
extKeccak384(addr(result.data), 384 div 8, input[0], input[1])
|
extKeccak384(addr(result.data), 384 div 8, input.baseAddr, input.len)
|
||||||
|
|
||||||
template keccak_384*(input: typed): Hash[384] =
|
template keccak_384*(input: typed): Hash[384] =
|
||||||
keccak_384(toInputRange(input))
|
keccak_384(toMemRange(input))
|
||||||
|
|
||||||
proc keccak_512(input: InputRange): Hash[512] =
|
proc keccak_512(input: MemRange): Hash[512] =
|
||||||
extKeccak512(addr(result.data), 512 div 8, input[0], input[1])
|
extKeccak512(addr(result.data), 512 div 8, input.baseAddr, input.len)
|
||||||
|
|
||||||
template keccak_512*(input: typed): Hash[512] =
|
template keccak_512*(input: typed): Hash[512] =
|
||||||
keccak_512(toInputRange(input))
|
keccak_512(toMemRange(input))
|
||||||
|
|
||||||
proc shake_128(input: InputRange): Hash[128] =
|
proc shake_128(input: MemRange): Hash[128] =
|
||||||
extShake128(addr(result.data), 128 div 8, input[0], input[1])
|
extShake128(addr(result.data), 128 div 8, input.baseAddr, input.len)
|
||||||
|
|
||||||
template shake_128*(input: typed): Hash[128] =
|
template shake_128*(input: typed): Hash[128] =
|
||||||
shake_128(toInputRange(input))
|
shake_128(toMemRange(input))
|
||||||
|
|
||||||
proc shake_256(input: InputRange): Hash[256] =
|
proc shake_256(input: MemRange): Hash[256] =
|
||||||
extShake256(addr(result.data), 256 div 8, input[0], input[1])
|
extShake256(addr(result.data), 256 div 8, input.baseAddr, input.len)
|
||||||
|
|
||||||
template shake_256*(input: typed): Hash[256] =
|
template shake_256*(input: typed): Hash[256] =
|
||||||
shake_256(toInputRange(input))
|
shake_256(toMemRange(input))
|
||||||
|
|
||||||
|
@ -1,13 +1,13 @@
|
|||||||
mode = ScriptMode.Verbose
|
mode = ScriptMode.Verbose
|
||||||
|
|
||||||
packageName = "keccak_tiny"
|
packageName = "keccak_tiny"
|
||||||
version = "0.1.0"
|
version = "0.2.0"
|
||||||
author = "Zahary Karadjov"
|
author = "Zahary Karadjov"
|
||||||
description = "A wrapper for the keccak-tiny C library"
|
description = "A wrapper for the keccak-tiny C library"
|
||||||
license = "Apache2"
|
license = "Apache2"
|
||||||
skipDirs = @["tests"]
|
skipDirs = @["tests"]
|
||||||
|
|
||||||
requires "nim >= 0.17.0"
|
requires "nim >= 0.17.0", "ranges >= 0.0.1"
|
||||||
|
|
||||||
proc configForTests() =
|
proc configForTests() =
|
||||||
--hints: off
|
--hints: off
|
||||||
|
Loading…
x
Reference in New Issue
Block a user