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

StringcomputeSHA1(byte[] ba)
computes the SHA1 hashcode for the passed byte array.
String hash = null;
try {
    MessageDigest sha1 = MessageDigest.getInstance("SHA1");
    InputStream is = new ByteArrayInputStream(ba);
    BufferedInputStream bis = new BufferedInputStream(is);
    DigestInputStream dis = new DigestInputStream(bis, sha1);
    while (dis.read() != -1) {
    ;
    byte[] h = sha1.digest();
    Formatter formatter = new Formatter();
    for (byte b : h) {
        formatter.format("%02x", b);
    hash = formatter.toString();
} catch (Exception e) {
    e.printStackTrace();
return hash;
byte[]computeSha1(byte[] data)
compute Sha
MessageDigest md = MessageDigest.getInstance("SHA1");
md.update(data);
return md.digest();
byte[]computeSHA1(ByteBuffer convertme, int offset, int len)
compute SHA
int oldp = convertme.position();
int oldl = convertme.limit();
try {
    MessageDigest md = MessageDigest.getInstance("SHA-1");
    convertme.position(offset);
    convertme.limit(len);
    md.update(convertme);
    return md.digest();
...
StringcomputeSha1(ReadableByteChannel channel)
compute Sha
final MessageDigest sha1 = newSHA1();
final ByteBuffer buf = ByteBuffer.allocate(8192);
while (channel.read(buf) != -1) {
    buf.flip();
    sha1.update(buf);
    buf.clear();
return toHexString(ByteBuffer.wrap(sha1.digest()));
...
StringcomputeSha1AsHexString(String strToHash)
Computes the SHA-1 hash value of the input string's UTF-8 representation and returns the result as a hex value in string form.
return computePartialSha1AsHexString(strToHash, 160);
byte[]computeSHA1Hash(byte[] data)
compute SHA Hash
try {
    MessageDigest sha = MessageDigest.getInstance("SHA");
    sha.update(data);
    return sha.digest();
} catch (NoSuchAlgorithmException e) {
    throw new CardException("Error computing SHA1 hash.", e);
byte[]digestSHA1(byte[] data)
digest SHA
return digest(data, ALGORITHM_SHA1);
StringdigestSHA1(String data)
Digests the data with SHA-1 and returns the raw unencoded bytes.
return getDigestedValue(SHA1, data.getBytes());
StringdigestSHA1(String login, String pass)
digest SHA
String input = login + pass;
return byteArrayToHexString(digest(input.getBytes(), "SHA-1"));
StringdigestSHA1(String text)
Calculate the SHA-1 digest code for the text.
return digest(text, "SHA1");