gpt4 book ai didi

haskell - 如何使用 Haskell Cryptonite?

转载 作者:行者123 更新时间:2023-12-01 08:50:59 25 4
gpt4 key购买 nike

我需要来自 cryptonite 的 Scrypt、AES256 和 RSA我很难弄清楚如何使用它们。这些有什么好的例子吗?

这个 tutorial不完整,也不算数。

谢谢!

PS:我既不是密码学专家,也不是 Haskell 专家。

最佳答案

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

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

关于haskell - 如何使用 Haskell Cryptonite?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42456724/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com