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

问题描述 投票:0回答:1

是否有像JS一样在Python中将散列转换为单词数组的方法?

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

我尝试使用Google进行搜索,但似乎找不到在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 }

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

javascript python node.js hmac cryptojs
1个回答
0
投票

您可以在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}

希望有所帮助。

© www.soinside.com 2019 - 2024. All rights reserved.