I'm trying to get the SHA256 of a string in Android.
Here is the PHP code that I want to match:
echo bin2hex(mhash(MHASH_SHA256,"asdf"));
//outputs "f0e4c2f76c58916ec258f246851bea091d14d4247a2fc3e18694461b1816e13b"
Now, in Java, I'm trying to do the following:
String password="asdf"
MessageDigest digest=null;
try {
digest = MessageDigest.getInstance("SHA-256");
} catch (NoSuchAlgorithmException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
digest.reset();
try {
Log.i("Eamorr",digest.digest(password.getBytes("UTF-8")).toString());
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
But this prints out: "a42yzk3axdv3k4yh98g8"
What did I do wrong here?
Solution thanks to erickson:
Log.i("Eamorr",bin2hex(getHash("asdf")));
public byte[] getHash(String password) {
MessageDigest digest=null;
try {
digest = MessageDigest.getInstance("SHA-256");
} catch (NoSuchAlgorithmException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
digest.reset();
return digest.digest(password.getBytes());
}
static String bin2hex(byte[] data) {
return String.format("%0" + (data.length*2) + "X", new BigInteger(1, data));
}
You are along the right lines, but converting the bytes is a little more complicated. This works on my device:
Source: bytesToHexString function is from the IOSched project.
The PHP function
bin2hex
means that it takes a string of bytes and encodes it as a hexadecimal number.In the Java code, you are trying to take a bunch of random bytes and decode them as a string using your platform's default character encoding. That isn't going to work, and if it did, it wouldn't produce the same results.
Here's a quick-and-dirty binary-to-hex conversion for Java:
This is quick to write, not necessarily quick to execute. If you are doing a lot of these, you should rewrite the function with a faster implementation.
i know this has been answered but i found a Hashing Library at android arsenal and its very easy,simple and just one line of code. can hash MD5, SHA-1, SHA-256, SHA-384, or SHA-512.
first add this to your gradle and sync
implementation 'com.github.1AboveAll:Hasher:1.2'
start hasing
Hasher.Companion.hash("Hello",HashType.SHA_1);