prototyping the NodeID creation hashing function

- misses conversion from ByteString to Integer
- uses unperformant Lazy ByteString builders + conversion to strict ones
This commit is contained in:
Trolli Schmittlauch 2020-03-19 00:39:58 +01:00
parent f1a52ab0b8
commit 8ad247978a
2 changed files with 29 additions and 1 deletions

View file

@ -46,7 +46,7 @@ category: Network
extra-source-files: CHANGELOG.md
common deps
build-depends: base ^>=4.12.0.0, containers ^>=0.6.0.1, utf8-string ^>=1.0.1.1, network ^>=2.8.0.1, time ^>=1.8.0.2, cmdargs ^>= 0.10, cryptonite ^>= 0.25
build-depends: base ^>=4.12.0.0, containers ^>=0.6.0.1, utf8-string ^>=1.0.1.1, network ^>=2.8.0.1, time ^>=1.8.0.2, cmdargs ^>= 0.10, cryptonite ^>= 0.25, memory, binary-strict
ghc-options: -Wall

28
Hash2Pub/hashtest.hs Normal file
View file

@ -0,0 +1,28 @@
{-# LANGUAGE DataKinds #-}
module Foo where
import Crypto.Hash
import qualified Data.ByteString as BS
import qualified Data.ByteString.Builder as BB
import qualified Data.ByteString.Lazy as BL
import qualified Data.ByteString.UTF8 as BSU
import qualified Data.ByteArray as BA
-- important: only works with strict ByteStrings
hashToShake128 string = hash bstr :: Digest (SHAKE128 128)
where
bstr = BSU.fromString string
-- TODO: type signature
-- TODO: convert byte string to Integer (as separate function, useful for parsing later on)
hashNodeID ipaddr@(a, b, _, _) domain vserver =
hashIpaddrUpper `BS.append` hashID domain' `BS.append` hashIpaddLower
where
vsBS = BS.pack [vserver] -- attention: only works for vserver IDs up to 255
-- TODO: this is inefficient and possibly better done with binary-strict
ipaddrNet = (BL.toStrict . BB.toLazyByteString . mconcat $ map BB.word32BE [a, b]) `BS.append` vsBS
domain' = BSU.fromString domain `BS.append` vsBS
hashID bstr = BS.pack . BA.unpack $ (hash bstr :: Digest (SHAKE128 128))
(hashIpaddrUpper, hashIpaddLower) = BS.splitAt 64 $ hashID ipaddrNet
testIDHash = hashNodeID (0x2001 + 0x16b8, 0x755a + 0xb1100, 0x7d6a + 0x12ab, 0xf0c5 + 0x386e) "example.com" 0