如何使用Haskell cryptonite?

时间:2017-02-25 13:32:29

标签: haskell cryptography

我需要来自cryptonite的Scrypt,AES256和RSA,我很难弄清楚如何使用它们。这些是否有一些很好的例子?

tutorial不完整且无法计算。

谢谢!

PS:我既不是加密也不是Haskell专家。

1 个答案:

答案 0 :(得分:6)

尝试发布一个更好的问题,我设法编译代码。我发布了我的解决方案,希望它可以帮助其他人

import Crypto.Random(getSystemDRG, randomBytesGenerate)
import Crypto.KDF.Scrypt (generate, Parameters(..))
import Crypto.Cipher.AES (AES256)
import Crypto.Cipher.Types (BlockCipher(..), Cipher(..),nullIV)
import Crypto.Error (throwCryptoError)

saltSize = 32
paramN = 14 :: Word64
paramR = 8
paramP = 1
paramKeyLen = 32

-- AES256 encryption
encrypt :: ByteString -> ByteString -> ByteString
encrypt key plainData = ctrCombine ctx nullIV plainData
  where ctx :: AES256
        ctx = throwCryptoError $ cipherInit key

decrypt :: ByteString -> ByteString -> ByteString
decrypt = encrypt

--Scrypt KDF
deriveKey :: Text -> ByteString -> ByteString
deriveKey password salt = generate params (encodeUtf8 password) salt
  where params = Parameters {n = paramN, r = paramR, p = paramP, outputLength = paramKeyLen}

-- for generating the salt
random :: Int -> IO ByteString
random size = do
  drg <- getSystemDRG
  let (bytes, _) = randomBytesGenerate size drg
  return bytes