Java SHA1 sha1Hex(String data)

Here you can find the source of sha1Hex(String data)

Description

sha Hex

License

Apache License

Declaration

public static String sha1Hex(String data) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

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

public class Main {
    private static final char[] DIGITS = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd',
            'e', 'f' };

    public static String sha1Hex(String data) {
        if (data == null) {
            throw new IllegalArgumentException("data must not be null");
        }/*from w  ww . ja va2s  .c om*/

        byte[] bytes = digest("SHA1", data);

        return toHexString(bytes);
    }

    private static byte[] digest(String algorithm, String data) {
        MessageDigest digest;
        try {
            digest = MessageDigest.getInstance(algorithm);
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        }

        return digest.digest(data.getBytes());
    }

    private static String toHexString(byte[] bytes) {
        int l = bytes.length;

        char[] out = new char[l << 1];

        for (int i = 0, j = 0; i < l; i++) {
            out[j++] = DIGITS[(0xF0 & bytes[i]) >>> 4];
            out[j++] = DIGITS[0x0F & bytes[i]];
        }

        return new String(out);
    }
}

Related

  1. sha1Hash(String tohash)
  2. sha1Hash(String toHash)
  3. sha1HashHex(String data)
  4. sha1HashInt(String text)
  5. sha1Hex(byte[] bytes)
  6. sha1hex(String source)
  7. sha1Java(String password)
  8. sha1Sum(byte[] ba)
  9. sha1sum(byte[] data, Integer startIdxInc, Integer stopIdxExc)