Skip to content

Instantly share code, notes, and snippets.

@cocreature
Created December 2, 2019 18:14
Show Gist options
  • Save cocreature/c391ab685dadd31f48227ff650100399 to your computer and use it in GitHub Desktop.
Save cocreature/c391ab685dadd31f48227ff650100399 to your computer and use it in GitHub Desktop.
{-# LANGUAGE OverloadedStrings #-}
module Test where
import qualified Data.ByteArray
import qualified Data.ByteString
import qualified Data.ByteString.Char8
import qualified Data.ByteString.Base16
import qualified Data.Text
import qualified Data.Text.Encoding
import qualified Crypto.Hash
x :: Data.Text.Text
x = "01=.=83\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\176=\136\148\182Q#\230lIB\173\NULOq\247\222\162:\130\170\DEL\210\233l\231\137\158\184W\197\143"
main :: IO ()
main = do
-- This will silently truncate individual Chars to fit into Word8.
let myhashTruncated = (Crypto.Hash.hash $ Data.ByteString.Char8.pack $ Data.Text.unpack x) :: Crypto.Hash.Digest Crypto.Hash.SHA256
-- This does proper UTF8.
let myhashUtf8 = (Crypto.Hash.hash $ Data.Text.Encoding.encodeUtf8 x) :: Crypto.Hash.Digest Crypto.Hash.SHA256
-- This is your expected result.
Data.ByteString.Char8.putStrLn $ Data.ByteString.Base16.encode $ Data.ByteArray.convert myhashTruncated
-- This is the result DAML gives you.
Data.ByteString.Char8.putStrLn $ Data.ByteString.Base16.encode $ Data.ByteArray.convert myhashUtf8
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment