Is there a way to convert a hash to a word array in Python as in JS?
In JS with CryptoJS I can use: CryptoJS.enc.Hex.parse(hash)
which will output the word array.
I've tried googling it but cannot seem to find how to do that in Python.
Javascript example:
var CryptoJS = require("crypto-js");
var hash = "c8f3ab9777da89748851932d3446b197450bb12fa9b9136ad708734291a6c60c";
console.log(hash);
I cannot figure out how to get similar output with hmac and hashlib libraries in Python but I expect the output something like this:
{ words:
[ -923554921,
2010810740,
-2007919827,
877048215,
1158394159,
-1447488662,
-687312062,
-1851341300 ],
sigBytes: 32 }
Update: I need to have an output in the exact same format (spacing, indents, new lines) to produce a subsequent hash from the output.
You can do this in Python but it's not builtin as part of any crypto library that I am aware of.
A simple implementation (requires 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)})
This will output: {'words': [-923554921, 2010810740, -2007919827, 877048215, 1158394159, -1447488662, -687312062, -1851341300], 'sigBytes': 32}
Hope that helps.