user2534685 user2534685 - 7 months ago 44
Python Question

Java and Python generate different Hmac-SHA256 output

I am trying to generate the same Hmac-SHA256 signature in Python as in my Java template. But the Python script generates not the same output. I can not find my mistake. Can anyone help me?

Java

public class Main {
public static void main(String[] args) {
String paramString = "teststring";
calculateSignature(hash(paramString));
}

private static byte[] hash(String paramString)
{
MessageDigest localMessageDigest = MessageDigest.getInstance("SHA-256");
localMessageDigest.update(paramString.getBytes("UTF-8"));
byte[] paramByte = localMessageDigest.digest();
System.out.println("Hash: " + DatatypeConverter.printBase64Binary(paramByte));
return paramByte;
}

public static void calculateSignature(byte[] paramArrayOfByte)
{
String Algor = "HmacSHA256";
Mac localMac = Mac.getInstance(Algor);
byte [] key = "secretkey".getBytes();
localMac.init(new SecretKeySpec(key, Algor));
paramArrayOfByte = localMac.doFinal(paramArrayOfByte);
System.out.println("Signature: " + DatatypeConverter.printBase64Binary(paramArrayOfByte));
}
}



Hash: PIcn4BmkK0RGZ6WHtgASUb7K2ruza/7YCHqSwYiC0RE=

Signature: X8V4RA7qaoVGz5K2l61gAXNPuLkAI7NZ9/9d7WnblbA=


Python

# -*- coding: utf-8 -*-
import hashlib
import base64
import hmac

secretkey = 'secretkey'
stringToSign = "teststring"

def hash(paramstring):
x = base64.b64encode(hashlib.sha256(paramstring).digest())
print('Hash: ' + x)
return x

def calculate_signature(hashvalue):
x = base64.b64encode(hmac.new(secretkey, hashvalue, hashlib.sha256).digest())
print('Signature: ' + x)

calculate_signature(hash(stringToSign))



Hash: PIcn4BmkK0RGZ6WHtgASUb7K2ruza/7YCHqSwYiC0RE=

Signature: NE8RtuFsOtafWrwDdlzILMgqCDm2huJ9A3IO6iy44Jc=

Answer

The problem is in python code you return base64 encoded but in java you just print it as base64 encoded but return without.

Try:

private static byte[] hash(String paramString)
{
    MessageDigest localMessageDigest = MessageDigest.getInstance("SHA-256");
    localMessageDigest.update(paramString.getBytes("UTF-8"));
    byte[] paramByte = localMessageDigest.digest();
    String base64Binary = DatatypeConverter.printBase64Binary(paramByte);
    System.out.println("Hash: " + base64Binary);
    return base64Binary.getBytes();
}

Or modify python code as:

def hash(paramstring):
    x = hashlib.sha256(paramstring).digest()
    return x