make SHA Hash - Java Security

Java examples for Security:SHA

Description

make SHA Hash

Demo Code

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class Main{

    public static String makeSHA1Hash(String str)
            throws NoSuchAlgorithmException {
        MessageDigest md = MessageDigest.getInstance("SHA1");
        md.reset();//from  ww  w . j  a v a 2 s .  com
        byte[] buffer = str.getBytes();
        md.update(buffer);
        byte[] digest = md.digest();

        String hexStr = "";
        for (int i = 0; i < digest.length; i++) {
            hexStr += Integer.toString((digest[i] & 0xff) + 0x100, 16)
                    .substring(1);
        }
        return hexStr;
    }
}

Related Tutorials