How To Reproduce An Sha256-based Hmac From R In Python 3
I am trying to reproduce salted sha256 output from R code in Python: library(openssl) res = sha256('test@gmail.com', key = '111') res # [1] '172f052058445afd9fe3afce05bfec573b5bb
Solution 1:
I can't quite reproduce your issue. The following keys match
In R:
library(openssl)
sha256("test@gmail.com")
#[1] "87924606b4131a8aceeeae8868531fbb9712aaa07a5d3a756b26ce0f5d6ca674"
In Python3:
import hashlib
print(hashlib.sha256(b"test@gmail.com").hexdigest())
#87924606b4131a8aceeeae8868531fbb9712aaa07a5d3a756b26ce0f5d6ca674
Update in response to your comment
The first thing to notice is that in R sha256
with a non-NULL
key
argument will calculate the hash-based message authentication code (HMAC). From ?sha256
:
All hash functions either calculate a hash-digest for ‘key == NULL’ or HMAC (hashed message authentication code) when ‘key’ is not ‘NULL’.
So if you want to use a key you will need to compare the resulting HMAC in R with the SHA2556-based HMAC in Python.
In R:
library(openssl)
sha256("test@gmail.com", key = "111")
#[1] "172f052058445afd9fe3afce05bfec573b5bb4c659bfd4cfc69a59d1597a0031"
In Python 3:
import hmac
import hashlib
print(hmac.new(b"111", b"test@gmail.com", hashlib.sha256).hexdigest())
#172f052058445afd9fe3afce05bfec573b5bb4c659bfd4cfc69a59d1597a0031
Post a Comment for "How To Reproduce An Sha256-based Hmac From R In Python 3"