Java Utililty Methods SHA1

List of utility methods to do SHA1

Description

The list of methods to do SHA1 are organized into topic(s).

Method

byte[]sha1hash(byte[] blob)
Perform a SHA-1 hash of a given byte array
byte[] toReturn = null;
MessageDigest md = MessageDigest.getInstance("SHA1");
md.update(blob);
toReturn = md.digest();
return toReturn;
byte[]sha1Hash(byte[] data)
sha Hash
MessageDigest md = MessageDigest.getInstance("SHA1");
return md.digest(data);
Stringsha1hash(Collection nquads)
A helper class to sha1 hash all the strings in a collection
try {
    final MessageDigest md = MessageDigest.getInstance("SHA-1");
    for (final String nquad : nquads) {
        md.update(nquad.getBytes("UTF-8"));
    return encodeHex(md.digest());
} catch (final NoSuchAlgorithmException e) {
    throw new RuntimeException(e);
...
byte[]sha1hash(File f)
shahash
return sha1hash(new BufferedInputStream(new FileInputStream(f)));
Stringsha1Hash(File file)
Generate a SHA.1 Hash for a given file.
try {
    DigestInputStream inputStream = new DigestInputStream(new FileInputStream(file),
            MessageDigest.getInstance("SHA-1"));
    try {
        byte[] buffer = new byte[4098];
        while (inputStream.read(buffer) != -1) {
        return bytesToHex(inputStream.getMessageDigest().digest());
...
Stringsha1Hash(final String tohash)
sha Hash
byte[] b = null;
try {
    b = tohash.getBytes("UTF-8");
    MessageDigest sha1 = MessageDigest.getInstance("SHA-1");
    b = sha1.digest(b);
} catch (UnsupportedEncodingException e) {
    throw new Error(e);
} catch (NoSuchAlgorithmException e) {
...
Stringsha1Hash(InputStream in)
sha Hash
return hash(in, "SHA1");
Stringsha1Hash(String content)
sha Hash
byte[] bytesOfMessage = null;
byte[] theDigest = null;
try {
    bytesOfMessage = content.getBytes("UTF-8");
    MessageDigest md = MessageDigest.getInstance("SHA-1");
    theDigest = md.digest(bytesOfMessage);
} catch (UnsupportedEncodingException e) {
    e.printStackTrace();
...
Stringsha1Hash(String input)
TODO: move to better algorithm?
if (_sha1Digest == null) {
    try {
        _sha1Digest = MessageDigest.getInstance("SHA-1");
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
        return null;
byte[] digest = _sha1Digest.digest(input.getBytes());
StringBuilder sb = new StringBuilder();
for (byte b : digest) {
    sb.append(String.format("%02x", b));
return sb.toString();
Stringsha1Hash(String input)
sha Hash
try {
    MessageDigest sha1Digest = MessageDigest.getInstance("SHA-1");
    sha1Digest.update(input.getBytes());
    return byteArrayToString(sha1Digest.digest());
} catch (Exception e) {
    logger.severe(e.getMessage());
return "";
...