I'm trying to hash a string/token, and I'm not getting the same hash result in C# that I get in Python.
String to hash:
eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1bmlxdWVfbmFtZSI6IkdVU1RBVk8uQlJFU09MSU5AQklHREFUQUNPUlAuQ09NLkJSIiwibmJmIjoxNjc4Mjk5NTA2LCJleHAiOjE2NzgzMDMxMDYsImlhdCI6MTY3ODI5OTUwNiwiaXNzIjoiQmlnIERhdGEgQ29ycC4iLCJwcm9kdWN0cyI6W10sImRvbWFpbiI6IkFETUlOIn0.AZufGQwwpilAjyVe9Rzc8ByI-xFYX6wf8SJUbiPMROQ
Result in Python with hashlib.sha3_256 and hexdigest :
501e1a7e062c8b53be510fb361fd937d55f98c480de90bfb4be2e71fd2d0a46a
Result in C# with SHA256Managed:
326fa626da197be117a5130aa19e75899e7dbcdd5fe730177a43da04b0568482
Method in C# .netCore:
public static string ComputeSha256Hash(string rawData)
{
var crypt = new SHA256Managed();
string hash = String.Empty;
byte[] crypto = crypt.ComputeHash(Encoding.UTF8.GetBytes(rawData));
foreach (byte theByte in crypto)
{
hash += theByte.ToString("x2");
}
return hash;
}
Method in Python:
generate_token():
requested_products = g.request_data.products
issued_at = datetime.utcnow()
expiration_dt = issued_at + timedelta(hours=g.request_data.expires)
# building jwt token payload
payload = {
'unique_name': g.request_data.login,
'nbf': issued_at,
'exp': expiration_dt,
'iat': issued_at,
'iss': constants.ISSUER,
'products': list(requested_products),
'domain': g.user_info["Group"]
}
# generating token
token = jwt.encode(payload, COMMUNICATION_KEY, algorithm='HS256')
# encrypting token to guarantee secure storage
hash_token = hashlib.sha3_256(token)
# storing encrypted token in database & returning it's id
token_id = jwtdb.jwtconf_save(
hash_token.hexdigest(),
issued_at,
g.user_info["Group"],
g.request_data.login,
expiration_dt,
list(requested_products))
I've tried changing the encoding to Encoding.ASCII without success.