From 88897ea74143a9b032165bc9b7f8da4b1189d5c2 Mon Sep 17 00:00:00 2001 From: Trolli Schmittlauch Date: Sat, 21 Mar 2020 01:13:21 +0100 Subject: [PATCH] generation of key IDs --- Hash2Pub/src/Hash2Pub/FediChord.hs | 13 +++++++++++++ Hash2Pub/test/FediChordSpec.hs | 6 ++++++ 2 files changed, 19 insertions(+) diff --git a/Hash2Pub/src/Hash2Pub/FediChord.hs b/Hash2Pub/src/Hash2Pub/FediChord.hs index 0460e69..8dba8d8 100644 --- a/Hash2Pub/src/Hash2Pub/FediChord.hs +++ b/Hash2Pub/src/Hash2Pub/FediChord.hs @@ -17,6 +17,8 @@ module Hash2Pub.FediChord ( , CacheEntry , genNodeID , genNodeIDBS + , genKeyID + , genKeyIDBS , byteStringToUInteger ) where @@ -132,12 +134,23 @@ genNodeIDBS (a, b, _, _) nodeDomain vserver = (hashIpaddrUpper, hashIpaddLower) = BS.splitAt 64 $ hashID ipaddrNet +-- | generates a 256 bit long @NodeID@ using SHAKE128 genNodeID :: HostAddress6 -- ^a node's IPv6 address -> String -- ^a node's 1st and 2nd level domain name -> Word8 -- ^the used vserver ID -> NodeID -- ^the generated @NodeID@ genNodeID ip nodeDomain vs = NodeID . byteStringToUInteger $ genNodeIDBS ip nodeDomain vs +-- | generates a 256 bit long key identifier, represented as ByteString, for looking up its data on the DHT +genKeyIDBS :: String -- ^the key string + -> BS.ByteString -- ^the key ID represented as a @ByteString@ +genKeyIDBS key = BS.pack . BA.unpack $ (hash (BSU.fromString key) :: Digest SHA3_256) + +-- | generates a 256 bit long key identifier for looking up its data on the DHT +genKeyID :: String -- ^the key string + -> NodeID -- ^the key ID +genKeyID = NodeID . byteStringToUInteger . genKeyIDBS + -- | parses the bit pattern of a ByteString as an unsigned Integer in Big Endian order -- by iterating it byte-wise from the back and shifting the byte values according to their offset diff --git a/Hash2Pub/test/FediChordSpec.hs b/Hash2Pub/test/FediChordSpec.hs index 4985091..2cc0345 100644 --- a/Hash2Pub/test/FediChordSpec.hs +++ b/Hash2Pub/test/FediChordSpec.hs @@ -44,6 +44,12 @@ spec = do byteStringToUInteger (BS.pack $ replicate 32 0xff) `shouldBe` 2^(256::Integer)-1 byteStringToUInteger (BS.pack $ replicate 32 0x00) `shouldBe` 0 byteStringToUInteger (BS.pack [0x00, 0x03, 0xf6, 0x78, 0x10, 0x00]) `shouldBe` 17019965440 + describe "key ID" $ + it "can be generated" $ do + genKeyIDBS "#sometag" `shouldBe` "\178\239\146\131\166\SYN\ESC\209\205\&3\143\212\145@#\205T\219\152\191\229\ACK|\153