4

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=

0

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

6 Comments

It is right. The Hmac module in java needs a byte encoded input. Also I must adjust the python code to the java code. What is wrong in the python code?
Why you need to adjust python code? With these changes both python and java gives same result.
I must write the python code to communicate with a existing system. The java code works with the system and is my template.
Signature: X8V4RA7qaoVGz5K2l61gAXNPuLkAI7NZ9/9d7WnblbA=
So you can change python code as in answer. I updated it.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.