lundi 15 avril 2019

Trying to pass a random string to SHA in Haskell

I'm trying to pass a random string (which happens to be a number) "4176730.5" to SHA in Haskell to get a larger random string like "2d711642b726b04401627ca9fbac32f5c8530fb1903cc4db02258717921a4881".

I have this code to generate a random number and cast it to a string

  num <- randomIO :: IO Float

  let x = C.pack (show (num*10000000))

  print x

but when I pass it to SHA with

  let a = sha256 x

I get the error

Couldn't match expected type ‘Data.ByteString.Lazy.Internal.ByteString’
            with actual type ‘C.ByteString’

I've tried casting my number to C.ByteString, but I think there are two types of Bytestring, according to the Haskell compiler.

The full code is:

import Data.Digest.Pure.SHA
import System.Random
import qualified Data.ByteString.Char8 as C

main :: IO ()

main = do
  num <- randomIO :: IO Float

  let x = C.pack (show (num*10000000))

  print x

  let a = sha256 x

      b = hmacSha256 "key" "some test message"
  mapM_ print [showDigest a, showDigest b]

Seeing as how there are apparently two types of Bytestring, and I'm casting to the wrong one, how do I cast my random string correctly?




Aucun commentaire:

Enregistrer un commentaire