【问题标题】:Unable to get a sha256 hash of a string [duplicate]无法获取字符串的 sha256 哈希 [重复]
【发布时间】:2017-02-13 08:59:23
【问题描述】:

我正在尝试获取字符串的哈希:

hs = hashlib.sha256(get_some_string()).hexdigest()

...但我得到一个错误:

TypeError: Unicode 对象必须在散列之前进行编码

【问题讨论】:

标签: python python-3.x hash


【解决方案1】:

使用utf-8编码:

hs = hashlib.sha256(get_some_string().encode('utf-8')).hexdigest()

如需了解更多信息,请阅读documentation

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-08-24
    • 2021-11-19
    • 1970-01-01
    • 2018-08-28
    • 1970-01-01
    • 2020-06-05
    • 2019-05-15
    • 2019-08-26
    相关资源
    最近更新 更多