-9

I was wondering if you guys could help me convert some java code to python. Basically I'm trying to get the same hash output that the java code executes. Currently all I have gotten to (in python) is using the hashlib module converting some string to sha256.

I have tried googling and I have found nothing, friends couldn't help so this is my last resort. Here is the hash outputs I get from python & java. Obviously python being the incorrect hash, and java code being the correct hash.

Java Code Output Hash(correct):

947e238ef26e56d3de2fe2480268c23297d91efe6355b3e5fac0dcf63

Python Code Output Hash(incorrect):

88c5f638b023a5740676960747833fc4eacc20697c92e1b46b0384ebf

The java code hashes a string which gets split into variables, im stuck on the for loop which I'm not able to convert to python. PLEASE take note I am a noob and not that very experienced, thanks :P.

Java CODE:

    String pass = "6b508/^!zVl?947e238ef26e56d3de2fe2480268c23297d91efe6355b3e5fac0dcf63";
    String secretKey = "Wr79mk1PXFac!#%";
    String loginKey = pass.substring(0, 5);
    String randomKey = pass.substring(5, 13);
    String originalHash = pass.substring(13);

    try {
        MessageDigest md = MessageDigest.getInstance("SHA-256");
        md.update((loginKey + randomKey + secretKey).getBytes(StandardCharsets.UTF_8));

        //obtain hash
        byte[] hash = md.digest();
        StringBuilder hexString = new StringBuilder();
        for (byte aHash : hash) {
            String hex = Integer.toHexString(0xff & aHash);
            if (hex.length() == 1) hexString.append('0');
            hexString.append(hex);
        }
        String newHash = hexString.toString().substring(0, 56);
        if (newHash.equals(originalHash)) {
            //hash is right
            return true;
        }

Python CODE(My attempt to convert):

key = "6b508"
salt = "Wr79mk1PXFac!#%"
rand = "/^!zVl?"
hash = hashlib.sha256(key+rand+salt).hexdigest()
raw_input(hash[0:57])

Any help would be appreciated, thanks.

xev
  • 1
  • 1
  • 6
  • 1
    It probably has to due with the character widths, Java uses 16 bit characters while python uses 8 bit characters. Encode the string as a unicode or UTF-8 string and it *should* work. – vandench Jan 01 '18 at 02:38
  • 2
    Look carefully at the string you are hashing and how it is formed. The Java and Python versions look different to me. (If you hash different inputs, then ... naturally ... the outputs will be different.) – Stephen C Jan 01 '18 at 02:38
  • @vandench - I suspect no. In the Java case, he is converting to bytes (UTF-8) before hashing. The characters (in this example) look to be all ASCII. – Stephen C Jan 01 '18 at 02:41
  • If you would print out `loginKey`, `randomKey` and `originalHash` from the java code you would notice that your value for `rand` in the python code is wrong (it should be `/^!zVl?9`) and that java as well as the python code with the correct input values give as hash value of "47e238ef26e56d3de2fe2480268c23297d91efe6355b3e5fac0dcf63" – Thomas Kläger Jan 01 '18 at 12:54
  • @ThomasKläger you are correct, need to fix that – xev Jan 12 '18 at 23:33

1 Answers1

1

The error is in the Java code. pass.substring(5, 13) returns "/^!zVl?9" but the "9" is not wanted as part of randomKey.

Use String randomKey = pass.substring(5, 12) and String originalHash = pass.substring(12) instead.

Michael Butscher
  • 10,028
  • 4
  • 24
  • 25