Difference between android sha224 and python sha224

For a prototype application, I create a simple user login. The user password will then be hashed using sha224 and passed into the background code. The problem that I am currently facing is the following. The password that was stored in the database (also hashed using sha224) seems a little different from the hash that I am sending. To create hashes, I use the following code.

Password set == test

Python

from hashlib import sha224
sha224("test").hexdigest()

Android

MessageDigest sha224 = MessageDigest.getInstance("SHA-224");
sha224.update(key.getBytes());

byte[] digest = sha224.digest();
StringBuffer buffer = new StringBuffer();

for(int i = 0; i < digest.length; i++) {
 buffer.append(String.valueOf(Integer.toHexString(0xFF & digest[i])));
}

return buffer.toString();

What will be created now looks like this, and I will post two hashes right under each other. (The first is python and the second is android)

90a3ed9e32b2aaf4c61c410eb925426119e1a9dc53d4286ade99a809 90a3ed9e32b2aaf4c61c41eb925426119e1a9dc53d4286ade99a89

They are pretty much the same, but the python hash has two more 0. Do you guys know why?

+3
3

Android ; 0s .

buffer.append(String.format("%02x", 0xFF & digest[i]));
+4
final MessageDigest mDigest = MessageDigest.getInstance("SHA-224");
byte[] messageDigest = mDigest.digest(toEncrypt.getBytes());
final BigInteger number = new BigInteger(1, messageDigest);
final String sha = number.toString(16);
final int diff = 32 - sha.length();
final StringBuilder finalSHA = new StringBuilder(32);
for (int i=0;i<diff;i++) {
 finalSHA.append("0");
}
finalSHA.append(sha);
return finalSHA.toString();
0

. , , 23- , . . , , . BigInteger:

MessageDigest sha224 = MessageDigest.getInstance("SHA-224");
sha224.update(key.getBytes());

byte[] digest = sha224.digest();
StringBuffer buffer = new StringBuffer();

for(int i = 0; i < digest.length; i++) {
  String hex_string = Integer.toHexString(0xFF & digest[i]);
  if(hex_string.length()==1) hex_string = "0"+hex_string;
  buffer.append(hex_string);
}

return buffer.toString();
0

Source: https://habr.com/ru/post/1774958/


All Articles