R
R
Roman Morozov2018-11-05 21:59:38
PHP
Roman Morozov, 2018-11-05 21:59:38

Why don't SHA-256 methods match java, python, php?

Hello, please tell me how to properly implement password hashing in python. When everything is correct in Java and Php.
The bottom line is that java and php have the same hashes, but in python with the same value: "111111" - it gives a different hash
Code in java:

public static String encodePassword(String password) {
    try {
      MessageDigest messageDiegest = MessageDigest.getInstance("SHA-256");
      messageDiegest.update(password.getBytes("UTF-8"));
      return Base64.encodeToString(messageDiegest.digest(), false);
    }
    catch (NoSuchAlgorithmException e) {
      System.out.println("Exception while encoding password");
      throw new Error(e);
    }
    catch (UnsupportedEncodingException e) {
      System.out.println("Exception while encoding password");
      throw new Error(e);
    }
  }

PHP code:
$password = trim($pass1);
        $password = base64_encode(sha1($password, true));

Python code:
digest = hmac.new(bytes(ps, 'UTF-8'), msg=None, digestmod=hashlib.sha256).digest()
    signature = base64.b64encode(digest).decode()
    print(signature)

On java and php - gives me the password hash "111111" - vLFfghR5tNV3K9DKhmwArV + SbjWAcgZZzIDTnJ0JgCo =
On python, it gives the hash of the same password "111111" - 3EMl1xMKvwzg0nLFPbOdYZtAKH / cWN964EjE18KiRkw =

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
DevMan, 2018-11-05
@WeRn-rm

php - https://ideone.com/W3mOWJ
python - https://ideone.com/tBGIyd

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question