mirror of
https://github.com/codex-storage/nim-libp2p.git
synced 2025-01-11 11:34:24 +00:00
Add SHA-1 support to multihash.nim, enabled tests for it.
This commit is contained in:
parent
68f3f506f3
commit
215c6c4257
@ -15,13 +15,13 @@
|
||||
## 4. SHA3/KECCAK
|
||||
## 5. SHAKE-128/SHAKE-256
|
||||
## 6. BLAKE2s/BLAKE2s
|
||||
## 7. SHA1
|
||||
##
|
||||
## Hashes which are not yet supported
|
||||
## 1. SHA1
|
||||
## 2. SKEIN
|
||||
## 3. MURMUR
|
||||
## 1. SKEIN
|
||||
## 2. MURMUR
|
||||
import tables
|
||||
import nimcrypto/[sha2, keccak, blake2, hash, utils]
|
||||
import nimcrypto/[sha, sha2, keccak, blake2, hash, utils]
|
||||
import varint, vbuffer, base58, multicodec, multibase
|
||||
|
||||
const
|
||||
@ -49,6 +49,13 @@ proc identhash(data: openarray[byte], output: var openarray[byte]) =
|
||||
else: len(data)
|
||||
copyMem(addr output[0], unsafeAddr data[0], length)
|
||||
|
||||
proc sha1hash(data: openarray[byte], output: var openarray[byte]) =
|
||||
if len(output) > 0:
|
||||
var digest = sha1.digest(data)
|
||||
var length = if sha1.sizeDigest > len(output): len(output)
|
||||
else: sha1.sizeDigest
|
||||
copyMem(addr output[0], addr digest.data[0], length)
|
||||
|
||||
proc dblsha2_256hash(data: openarray[byte], output: var openarray[byte]) =
|
||||
if len(output) > 0:
|
||||
var digest1 = sha256.digest(data)
|
||||
@ -163,6 +170,8 @@ const
|
||||
HashesList = [
|
||||
MHash(mcodec: multiCodec("identity"), size: 0,
|
||||
coder: identhash),
|
||||
MHash(mcodec: multiCodec("sha1"), size: sha1.sizeDigest,
|
||||
coder: sha1hash),
|
||||
MHash(mcodec: multiCodec("dbl-sha2-256"), size: sha256.sizeDigest,
|
||||
coder: dblsha2_256hash
|
||||
),
|
||||
|
@ -1,14 +1,14 @@
|
||||
import unittest
|
||||
import nimcrypto/utils
|
||||
import ../libp2p/multihash
|
||||
|
||||
const
|
||||
RustTestVectors = [
|
||||
# TODO: SHA1
|
||||
# [
|
||||
# "sha1",
|
||||
# "beep boop",
|
||||
# "11147c8357577f51d4f0a8d393aa1aaafb28863d9421"
|
||||
# ],
|
||||
[
|
||||
"sha1",
|
||||
"beep boop",
|
||||
"11147C8357577F51D4F0A8D393AA1AAAFB28863D9421"
|
||||
],
|
||||
[
|
||||
"sha2-256",
|
||||
"helloworld",
|
||||
@ -22,7 +22,9 @@ const
|
||||
[
|
||||
"sha2-512",
|
||||
"hello world",
|
||||
"1340309ECC489C12D6EB4CC40F50C902F2B4D0ED77EE511A7C7A9BCD3CA86D4CD86F989DD35BC5FF499670DA34255B45B0CFD830E81F605DCF7DC5542E93AE9CD76F"
|
||||
"""1340309ECC489C12D6EB4CC40F50C902F2B4D0ED77EE511A7C7A9BCD3CA86D4C
|
||||
D86F989DD35BC5FF499670DA34255B45B0CFD830E81F605DCF7DC5542E93AE9C
|
||||
D76F"""
|
||||
],
|
||||
[
|
||||
"sha3-224",
|
||||
@ -37,12 +39,15 @@ const
|
||||
[
|
||||
"sha3-384",
|
||||
"hello world",
|
||||
"153083BFF28DDE1B1BF5810071C6643C08E5B05BDB836EFFD70B403EA8EA0A634DC4997EB1053AA3593F590F9C63630DD90B"
|
||||
"""153083BFF28DDE1B1BF5810071C6643C08E5B05BDB836EFFD70B403EA8EA0A63
|
||||
4DC4997EB1053AA3593F590F9C63630DD90B"""
|
||||
],
|
||||
[
|
||||
"sha3-512",
|
||||
"hello world",
|
||||
"1440840006653E9AC9E95117A15C915CAAB81662918E925DE9E004F774FF82D7079A40D4D27B1B372657C61D46D470304C88C788B3A4527AD074D1DCCBEE5DBAA99A"
|
||||
"""1440840006653E9AC9E95117A15C915CAAB81662918E925DE9E004F774FF82D7
|
||||
079A40D4D27B1B372657C61D46D470304C88C788B3A4527AD074D1DCCBEE5DBA
|
||||
A99A"""
|
||||
],
|
||||
[
|
||||
"keccak-224",
|
||||
@ -57,12 +62,15 @@ const
|
||||
[
|
||||
"keccak-384",
|
||||
"hello world",
|
||||
"1C3065FC99339A2A40E99D3C40D695B22F278853CA0F925CDE4254BCAE5E22ECE47E6441F91B6568425ADC9D95B0072EB49F"
|
||||
"""1C3065FC99339A2A40E99D3C40D695B22F278853CA0F925CDE4254BCAE5E22ECE4
|
||||
7E6441F91B6568425ADC9D95B0072EB49F"""
|
||||
],
|
||||
[
|
||||
"keccak-512",
|
||||
"hello world",
|
||||
"1D403EE2B40047B8060F68C67242175660F4174D0AF5C01D47168EC20ED619B0B7C42181F40AA1046F39E2EF9EFC6910782A998E0013D172458957957FAC9405B67D"
|
||||
"""1D403EE2B40047B8060F68C67242175660F4174D0AF5C01D47168EC20ED619B0
|
||||
B7C42181F40AA1046F39E2EF9EFC6910782A998E0013D172458957957FAC9405
|
||||
B67D"""
|
||||
]
|
||||
]
|
||||
|
||||
@ -73,8 +81,8 @@ suite "MultiHash test suite":
|
||||
var msg = item[1]
|
||||
var bmsg = cast[seq[byte]](msg)
|
||||
var mh1 = MultiHash.digest(item[0], bmsg)
|
||||
var mh2 = MultiHash.init(item[2])
|
||||
var mh2 = MultiHash.init(stripSpaces(item[2]))
|
||||
check:
|
||||
hex(mh1) == item[2]
|
||||
hex(mh1) == stripSpaces(item[2])
|
||||
hex(mh1) == hex(mh2)
|
||||
mh1 == mh2
|
||||
|
Loading…
x
Reference in New Issue
Block a user