From 8ad247978a681728c6ae0050d65daf5dc7a0a79d Mon Sep 17 00:00:00 2001 From: Trolli Schmittlauch Date: Thu, 19 Mar 2020 00:39:58 +0100 Subject: [PATCH] prototyping the NodeID creation hashing function - misses conversion from ByteString to Integer - uses unperformant Lazy ByteString builders + conversion to strict ones --- Hash2Pub/Hash2Pub.cabal | 2 +- Hash2Pub/hashtest.hs | 28 ++++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 Hash2Pub/hashtest.hs diff --git a/Hash2Pub/Hash2Pub.cabal b/Hash2Pub/Hash2Pub.cabal index eb18b2e..428475d 100644 --- a/Hash2Pub/Hash2Pub.cabal +++ b/Hash2Pub/Hash2Pub.cabal @@ -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 diff --git a/Hash2Pub/hashtest.hs b/Hash2Pub/hashtest.hs new file mode 100644 index 0000000..b076b3e --- /dev/null +++ b/Hash2Pub/hashtest.hs @@ -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