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

Stringsha1(final File file)
sha
final MessageDigest messageDigest = MessageDigest.getInstance("SHA1");
try (InputStream is = new BufferedInputStream(new FileInputStream(file))) {
    final byte[] buffer = new byte[1024];
    for (int read = 0; (read = is.read(buffer)) != -1;) {
        messageDigest.update(buffer, 0, read);
return sha12String(messageDigest);
...
Stringsha1(final String data)
Returns the SHA-1 of the specified string
if (null == data) {
    throw new IllegalArgumentException("null string");
try {
    final MessageDigest md = MessageDigest.getInstance("SHA-1");
    return toHexString(md.digest(data.getBytes()));
} catch (final NoSuchAlgorithmException e) {
    return null;
...
Stringsha1(final String str)
Creates the Sha1 representation of a given string
byte[] sha1hash = new byte[40];
try {
    final MessageDigest md = MessageDigest.getInstance("SHA-1");
    md.update(str.getBytes(StandardCharsets.UTF_8));
    sha1hash = md.digest();
} catch (final NoSuchAlgorithmException e) {
    e.printStackTrace();
return toHex(sha1hash);
Stringsha1(final String string)
Get the sha1 hash of a string.
try {
    final MessageDigest m = MessageDigest.getInstance("SHA-1");
    m.update(string.getBytes(), 0, string.length());
    return new BigInteger(1, m.digest()).toString(16);
} catch (NoSuchAlgorithmException e) {
    return "";
Stringsha1(final String text)
sha
try {
    final byte[] bytesOfMessage = text.getBytes("UTF-8");
    final MessageDigest md = MessageDigest.getInstance("SHA1");
    final byte[] digest = md.digest(bytesOfMessage);
    return new String(digest);
} catch (Exception e) {
    throw new RuntimeException();
byte[]SHA1(final String text)
SHA
final MessageDigest md = MessageDigest.getInstance("SHA-1");
md.update(text.getBytes("iso-8859-1"), 0, text.length());
final byte[] sha1hash = md.digest();
return sha1hash;
byte[]sha1(final String todigest)
sha
try {
    byte[] b = todigest.getBytes("UTF-8");
    MessageDigest sha1 = MessageDigest.getInstance("SHA-1");
    return sha1.digest(b);
} catch (UnsupportedEncodingException e) {
    throw new Error(e);
} catch (NoSuchAlgorithmException e) {
    throw new Error(e);
...
byte[]SHA1(InputStream in)
SHA
MessageDigest crypt = sha1.get();
crypt.reset();
byte[] buf = new byte[4 * 1024];
int len;
while ((len = in.read(buf)) > 0) {
    Thread.yield();
    crypt.update(buf, 0, len);
in.close();
return crypt.digest();
StringSHA1(MessageDigest messageDigest, String... texts)
SHA
try {
    if (messageDigest == null) {
        throw new Exception("messageDigest is null or text is null");
    if (texts == null || texts.length == 0)
        return null;
    if (texts[0] == null)
        return null;
...
Stringsha1(Object object)
Originally from http://www.velocityreviews.com/forums/t131917-sha1-hash-generator-in-hex.html
if (object == null) {
    throw new Exception("Object is null.");
String input = String.valueOf(object);
MessageDigest md;
try {
    md = MessageDigest.getInstance("SHA1");
} catch (NoSuchAlgorithmException ex) {
...