javascript - 是否可以在Python中输出类似于CryptoJS.enc.Hex.parse(hash)的单词数组

标签 javascript python node.js hmac cryptojs

有没有办法像在 JS 中一样将 Python 中的哈希值转换为单词数组?

在带有 CryptoJS 的 JS 中,我可以使用:CryptoJS.enc.Hex.parse(hash) 它将输出单词数组。

我尝试用谷歌搜索它,但似乎找不到如何在 Python 中做到这一点。

Javascript 示例:

var CryptoJS = require("crypto-js");

var hash = "c8f3ab9777da89748851932d3446b197450bb12fa9b9136ad708734291a6c60c";

console.log(hash);

我无法弄清楚如何使用 Python 中的 hmac 和 hashlib 库获得类似的输出,但我期望输出如下所示:

{ words:
   [ -923554921,
     2010810740,
     -2007919827,
     877048215,
     1158394159,
     -1447488662,
     -687312062,
     -1851341300 ],
  sigBytes: 32 }

更新: 我需要具有完全相同格式(间距、缩进、换行)的输出,以便从输出中生成后续哈希。

最佳答案

您可以在 Python 中执行此操作,但据我所知,它不是内置的任何加密库的一部分。

一个简单的实现(需要 Python 3):


hash = "c8f3ab9777da89748851932d3446b197450bb12fa9b9136ad708734291a6c60c"

# Convert hex-encoded data into a byte array
hash_bytes = bytes.fromhex(hash)

# Split bytes into 4-byte chunks (32-bit integers) and convert
# The integers in your example a big-endian, signed integers
hash_ints = [
    int.from_bytes(hash_bytes[i:i+4], "big", signed=True) 
    for i in range(0, len(hash_bytes), 4)
]

# Print result
print({"words": hash_ints, "sigBytes": len(hash_bytes)})

这将输出:{'words': [-923554921, 2010810740, -2007919827, 877048215, 1158394159, -1447488662, -687312062, -1851341300], 'sigBytes': 32}

希望有帮助。

关于javascript - 是否可以在Python中输出类似于CryptoJS.enc.Hex.parse(hash)的单词数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58478184/

相关文章:

node.js - 获取 TypeError : undefined is not an object (evaluating '_mathjs.default.fraction' ), React-Native

javascript - 如何在模型中编写多个远程方法?

javascript - JavaScript 中的单行 if 与 &&

javascript - 使用 Grunt Connect 和 Livereload 配置多个服务器

python - 使用序列python设置数组元素

来自 t 统计的 Python p 值

javascript - jQuery Find and Replace 正在挂起浏览器!数据量太大?

python - psycopg2 选择 NULL 值

c# - NodeJS 的非无状态 Socket 解决方案

node.js - 无法升级 firebase-tools,因为依赖 iltorb npm 包安装失败