试图在Haskell中将随机字符串传递给SHA

问题描述 投票:1回答:3

我正在尝试将随机字符串(恰好是一个数字)“4176730.5”传递给Haskell中的SHA,以获得更大的随机字符串,如“2d711642b726b04401627ca9fbac32f5c8530fb1903cc4db02258717921a4881”。

我有这个代码生成一个随机数并将其转换为字符串

  num <- randomIO :: IO Float

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

  print x

但是当我把它传递给SHA时

  let a = sha256 x

我收到了错误

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

我已经尝试将我的数字转换为C.ByteString,但根据Haskell编译器,我认为有两种类型的Bytestring。

完整的代码是:

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]

看到有两种类型的Bytestring,我正在向错误的方向转换,如何正确地投射我的随机字符串?

如果我将导入限定的Data.ByteString.Char8替换为C with,请继续下面的@ Cubic的回答

import qualified Data.ByteString.Lazy as C

我只是得到这些错误

Couldn't match type ‘Char’ with ‘GHC.Word.Word8’
Expected type: [GHC.Word.Word8]
  Actual type: String

Couldn't match expected type ‘C.ByteString’
            with actual type ‘[Char]’
haskell random sha bytestring
3个回答
© www.soinside.com 2019 - 2024. All rights reserved.