Example usage for org.bouncycastle.crypto.digests GeneralDigest getByteLength

List of usage examples for org.bouncycastle.crypto.digests GeneralDigest getByteLength

Introduction

In this page you can find the example usage for org.bouncycastle.crypto.digests GeneralDigest getByteLength.

Prototype

public int getByteLength() 

Source Link

Usage

From source file:it.yup.util.Utils.java

/**
 * // ww  w . j  a  va 2 s  .  c o  m
 * @param data
 * @param digestType
 * @return the digest or null if the requested digest is not supported
 */
static public byte[] digest(byte data[], String digestType) {
    GeneralDigest digest = null;
    if (digestType.equals("sha1")) {
        digest = new SHA1Digest();
    } else if (digestType.equals("md5")) {
        digest = new MD5Digest();
    } else {
        return null;
    }

    // XXX too many copies of data, modify the hash functions so that they write
    // the result to a byte array
    digest.update(data, 0, data.length);
    // some emulators fail on calling getByteLength  
    byte out[] = null;
    try {
        out = new byte[digest.getByteLength()];
    } catch (Error e) {
        out = new byte[64];
    }

    int len = digest.doFinal(out, 0);
    byte result[] = new byte[len];
    System.arraycopy(out, 0, result, 0, len);
    return result;
}