How can I calculate the SHA-256 hash of a string in Android?

Eamorr :

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));
}
erickson :

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:

static String bin2hex(byte[] data) {
    StringBuilder hex = new StringBuilder(data.length * 2);
    for (byte b : data)
        hex.append(String.format("%02x", b & 0xFF));
    return hex.toString();
}

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I hash a string with SHA256 in JS?

How can I check a sha256 hash?

How can I compute a SHA-2 (ideally SHA 256 or SHA 512) hash in iOS?

can I use sha1,sha256 lib generated string as a hash key

How to calculate sha256 hash in Groovy 2.4

How can I replicate a javascript sha256 hash in Java and get the same Hex output?

Crystal: How can I find the SHA256 hash of a binary value?

How to hash some string with sha256 in Java?

How to create a SHA-256 hash of a string in Mason code?

How do I specify the key for computing a SHA256 hash?

How Do I Convert a Picture into a Sha256 Hash Python

If we know the SHA-256 and SHA-384 hash of a string, can we find the actual string?

Calculate sha256 hash starting with some string based on some fixed data and all permutations of other data

How to hash output of sha256 with sha256 in rust

Hash String via SHA-256 in Java

wrong calculatin of sha256 hash in android

How can I get the sha1 hash of a string in node.js?

How to determine the electron sha256 hash

How long is the SHA256 hash?

How to use sha256 hash in Python

How to Hex Encode a SHA-256 Hash

How to hash password with sha256 in flutter?

How can I calculate git tree hash?

ssh remote and calculate sha256 hash :: No such file or directory

Java: Calculate SHA-256 hash of large file efficiently

Calculate SHA-256 hash and B64 of a file in JavaScript

Calculate SHA256 hash WITHOUT System.Security.Cryptography

How to hash a string with SHA-3/256 with Delphi XE8?

How can I find keccak 256 hash in Python