gpt4 book ai didi

python - Python 的 hexlify 和 unhexlify 在 Haskell 中的等价物是什么?

转载 作者:行者123 更新时间:2023-11-28 21:11:10 25 4
gpt4 key购买 nike

我的代码看起来有点像

module Lib where

import Data.ByteString (ByteString)
import Data.ByteString.Lazy (fromStrict)
import Data.Digest.Pure.SHA (sha1, showDigest)

hash :: ByteString -> String
hash bstring = showDigest $ sha1 $ fromStrict bstring

它为我提供了 SHA1 散列的十六进制字符串表示形式。我将如何在这个十六进制表示和二进制表示之间进行转换?在 python 中,我会使用 binascii.hexlifybinascii.unhexlify。例如:

'95d09f2b10159347eece71399a7e2e907ea3df4f' <=> '\x95\xd0\x9f+\x10\x15\x93G\xee\xceq9\x9a~.\x90~\xa3\xdfO'

最佳答案

我通常将此称为 base-16 编码,它在 Python 中也可用 base64.b16decode()base64.b16encode()。 “hexlify”和“unhexlify”这两个词似乎有点特殊。

使用这些搜索词,我能够找到 base16-bytestring在黑客攻击中。它使用并返回 ByteString,而不是 String,因此如果您有 String,则需要打包。

这里是你如何使用它。您可以看到它的接口(interface)略有不同,Haskell 以不同方式转义字符串,但它给出的结果与您在示例中给出的结果相同。

> decode "95d09f2b10159347eece71399a7e2e907ea3df4f"
("\149\208\159+\DLE\NAK\147G\238\206q9\154~.\144~\163\223O","")
> encode "\x95\xd0\x9f+\x10\x15\x93G\xee\xceq9\x9a~.\x90~\xa3\xdfO"
"95d09f2b10159347eece71399a7e2e907ea3df4f"

关于python - Python 的 hexlify 和 unhexlify 在 Haskell 中的等价物是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35688087/

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