SHA256 encryption in JAVA and PYTHON produce different results

I would appreciate your help to understand why my SHA256 encryption function in JAVA and in PYTHON does not produce the result (difference in 1 digit, one more in python):

hash to encrypt is: “thisisatest”
Java results is: a7c96262c21db9a06fd49e307d694fd95f624569f9b35bb3ffacd88044f9787
Python result is:
a7c96262c21db9a06fd49e307d694fd95f624569f9b35bb3ffacd880440f9787

Python code:

import hashlib

def encrypt_string(hash_string):
    sha_signature = 
        hashlib.sha256(hash_string.encode()).hexdigest()
    return sha_signature


hash_string = "thisisatest"
print(encrypt_string(hash_string), end="")

Java code:

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class OpenAPIPasswordEncryptor1 {


    private static MessageDigest sha256;

    // generated password is stored encrypted (using also user name for hashing)
    public synchronized static String encrypt(String hash) {
        try {

            StringBuilder builder = new StringBuilder();
            builder.append(hash);

            // first time , encrypt user name , password and static key
            String encryptedCredentials = encryptionIterator(builder.toString());
           return encryptedCredentials;
        } 

        catch (Exception e) {
            e.printStackTrace();
        }

        return "";
    }

    private static String encryptionIterator(String content) {
        try {
            sha256 = MessageDigest.getInstance("SHA-256");
            // append the static key to each iteration
            byte[] passBytes = (content).getBytes();
            sha256.reset();
            byte[] digested = sha256.digest(passBytes);
            StringBuffer sb = new StringBuffer();
            for (int i = 0; i < digested.length; i++) {
                sb.append(Integer.toHexString(0xff & digested[i]));
            }

            return sb.toString();
        } catch (NoSuchAlgorithmException ex) {
            ex.printStackTrace();
        }

        return "";
    }

    // generate password for developers
    public static void main(String[] args) {
        String hash = "thisisatest";
        String encrypt = encrypt(hash);
        System.out.println("Your Password Is '" + encrypt + "'");
    }
}

1
Leave a Reply

avatar
1 Comment threads
0 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
1 Comment authors
Jason Recent comment authors
  Subscribe  
newest oldest most voted
Notify of
Jason
Guest

The problem is that Integer.toHexString(int) drops the leading zero if the (unsigned) byte value is 15 or less. As a quick workaround, you could use this:

sb.append(String.format("%02x", 0xff & digested[i]));

Although more efficient implementations are certainly possible.

To produce the same bug in Python, use this:

def encrypt_string(hash_string):
    sha_signature = hashlib.sha256(hash_string.encode()).digest()
    return "".join(["{:x}".format(b) for b in sha_signature])