Java String to SHA1

Posted by AeroDroid on Stack Overflow See other posts from Stack Overflow or by AeroDroid
Published on 2011-02-04T07:22:16Z Indexed on 2011/02/04 7:25 UTC
Read the original article Hit count: 207

Filed under:
|
|

I'm trying to make a simple String to SHA1 converter in Java and this is what I've got...

public static String toSHA1(byte[] convertme) {
    MessageDigest md = null;
    try {
        md = MessageDigest.getInstance("SHA-1");
    }
    catch(NoSuchAlgorithmException e) {
        e.printStackTrace();
    } 
    return new String(md.digest(convertme));
}

When I pass it toSHA1("password".getBytes()), I get "[?a?????%l?3~??." I know it's probably a simple encoding fix like UTF-8, but could someone tell me what I should do to get what I want which is "5baa61e4c9b93f3f0682250b6cf8331b7ee68fd8"? Or am I doing this completely wrong?

Thanks a lot!

© Stack Overflow or respective owner

Related posts about java

Related posts about string