Java SHA-1 Hash Hex String

Just simple code of how to get SHA-1 hash hex code of String.

Pure Java

public static String sha1(String input) throws NoSuchAlgorithmException, UnsupportedEncodingException {
    MessageDigest md5 = MessageDigest.getInstance("SHA1");
    byte[] digest = md5.digest(input.getBytes("UTF-8"));
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < digest.length; ++i) {
        sb.append(Integer.toHexString((digest[i] & 0xFF) | 0x100).substring(1, 3));
    return sb.toString();


public static String sha1guava(String input) {
    return Hashing.sha1().newHasher()
            .putString(input, Charsets.UTF_8)

Apache Commons Codec

public static String sha1codec(String input) {
    return DigestUtils.sha1Hex(input);

Leave a Reply

Your email address will not be published. Required fields are marked *