I am trying to translate this piece of python code to java however I have so far had no luck.
hmac_key = base64.b64decode(secret)
digest = hmac.new(hmac_key, message.encode('utf-8'), digestmod=hashlib.sha256).digest()
signature = base64.b64encode(digest).decode('utf-8')
This is my current java implementation.
byte[] hmac_key = base64().decode(secret);
String hash = Hashing.hmacSha256(hmac_key).hashString(message, StandardCharsets.UTF_8).toString();
String updatedMsg = base64().encode(hash.getBytes(StandardCharsets.UTF_8));
Why do these two pieces of code return different outputs?
CodePudding user response:
You are calling toString()
method on generated hash instead use asBytes()
byte[] hmacKey = base64().decode(secret);
byte[] hash = Hashing.hmacSha256(hmacKey).hashString(message, StandardCharsets.UTF_8).asBytes();
String updatedMsg = base64().encode(hash);