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

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

Introduction

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

Prototype

public int doFinal(byte[] out, int outOff);

Source Link

Document

close the digest, producing the final digest value.

Usage

From source file:com.stg.crypto.CryptoHelper.java

License:Open Source License

/**
 * This is a general purpose checksum generator method.
 * /*from www .  j  a v  a  2  s .  c o  m*/
 * @param digest Any class that extends {@link GeneralDigest}.
 * @param bytes bytes on which the checksum is to be generated.
 * @return checksum as per the digest.
 * @see CryptoHelper#generateMD5Checksum(byte[])
 * @see CryptoHelper#generateMD5Checksum(InputStream)
 * @see CryptoHelper#generateMD5Checksum(String)
 * @see CryptoHelper#generateSHA1Checksum(byte[])
 * @see CryptoHelper#generateSHA1Checksum(InputStream)
 * @see CryptoHelper#generateSHA1Checksum(String)
 */
public static String generateChecksum(GeneralDigest digest, byte[] bytes) {
    digest.update(bytes, 0, bytes.length);
    byte[] checkSum = new byte[digest.getDigestSize()];
    digest.doFinal(checkSum, 0);
    return toString(Hex.encode(checkSum));
}

From source file:edu.wisc.doit.tcrypt.BouncyCastleTokenDecrypter.java

License:Apache License

@Override
public String decrypt(String ciphertext) throws InvalidCipherTextException {
    final Matcher tokenMatcher = TOKEN_PATTERN.matcher(ciphertext);
    if (tokenMatcher.matches()) {
        ciphertext = tokenMatcher.group(1);
    } else {//from w  w w  .j  a  v  a 2  s  .  c o m
        final Matcher base64Matcher = BASE64_PATTERN.matcher(ciphertext);
        if (!base64Matcher.matches()) {
            throw new IllegalArgumentException("Specified ciphertext is not valid");
        }
    }

    //Decode the cipher text
    final byte[] encryptedTokenWithHash = Base64.decodeBase64(ciphertext);

    final AsymmetricBlockCipher e = getDecryptCipher();
    final byte[] tokenWithHashBytes = e.processBlock(encryptedTokenWithHash, 0, encryptedTokenWithHash.length);

    //Split the decrypted text into the password and the hash
    final String tokenWithHash = new String(tokenWithHashBytes, BouncyCastleTokenEncrypter.CHARSET);
    final int seperatorIndex = tokenWithHash.lastIndexOf(BouncyCastleTokenEncrypter.SEPARATOR);
    if (seperatorIndex < 0) {
        throw new IllegalArgumentException(
                "token/hash string doesn't contain seperator: " + BouncyCastleTokenEncrypter.SEPARATOR);
    }
    final byte[] passwordBytes = tokenWithHash.substring(0, seperatorIndex)
            .getBytes(BouncyCastleTokenEncrypter.CHARSET);
    final byte[] passwordHashBytes = Base64.decodeBase64(
            tokenWithHash.substring(seperatorIndex + 1).getBytes(BouncyCastleTokenEncrypter.CHARSET));

    //Generate hash of the decrypted password
    final GeneralDigest digest = this.createDigester();
    digest.update(passwordBytes, 0, passwordBytes.length);
    final byte[] expectedHashBytes = new byte[digest.getDigestSize()];
    digest.doFinal(expectedHashBytes, 0);

    //Verify the generated hash against the decrypted hash
    if (!Arrays.equals(expectedHashBytes, passwordHashBytes)) {
        throw new IllegalArgumentException("Hash of the decrypted token does not match the decrypted hash");
    }

    return new String(passwordBytes, BouncyCastleTokenEncrypter.CHARSET);
}

From source file:edu.wisc.doit.tcrypt.BouncyCastleTokenEncrypter.java

License:Apache License

@Override
public String encrypt(String token) throws InvalidCipherTextException {
    //Convert the token into a byte[]
    final byte[] tokenBytes = token.getBytes(CHARSET);

    //Generate the Base64 encoded hash of the token
    final GeneralDigest digest = createDigester();
    digest.update(tokenBytes, 0, tokenBytes.length);
    final byte[] hashBytes = new byte[digest.getDigestSize()];
    digest.doFinal(hashBytes, 0);
    final byte[] encodedHashBytes = Base64.encodeBase64(hashBytes);

    //Create the pre-encryption byte[] to hold the token, separator, and hash
    final byte[] tokenWithHashBytes = new byte[tokenBytes.length + SEPARATOR_BYTES.length
            + encodedHashBytes.length];/*from   w  w w .j av  a2s .  c  o  m*/

    //Copy in password bytes
    System.arraycopy(tokenBytes, 0, tokenWithHashBytes, 0, tokenBytes.length);

    //Copy in separator bytes
    System.arraycopy(SEPARATOR_BYTES, 0, tokenWithHashBytes, tokenBytes.length, SEPARATOR_BYTES.length);

    //Copy in encoded hash bytes
    System.arraycopy(encodedHashBytes, 0, tokenWithHashBytes, tokenBytes.length + SEPARATOR_BYTES.length,
            encodedHashBytes.length);

    AsymmetricBlockCipher e = getEncryptCipher();

    //Encrypt the bytes
    final byte[] encryptedTokenWithHash = e.processBlock(tokenWithHashBytes, 0, tokenWithHashBytes.length);

    //Encode the encrypted data and convert it into a string
    final String encryptedToken = new String(Base64.encodeBase64(encryptedTokenWithHash), CHARSET);
    return TOKEN_PREFIX + encryptedToken + TOKEN_SUFFIX;
}

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

/**
 * /*from   w ww .j av  a2  s  . co 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;
}

From source file:org.bunkr.cli.commands.HashCommand.java

License:Open Source License

private byte[] calculateHash(ArchiveInfoContext context, FileInventoryItem target, String algorithm,
        boolean showProgress) throws IOException, CLIException {
    ProgressBar pb = new ProgressBar(120, target.getActualSize(), "Calculating hash: ");
    pb.setEnabled(showProgress);/*from   w w w . j a  v  a  2  s  . c  om*/
    pb.startFresh();
    GeneralDigest digest = getDigest(algorithm);
    digest.reset();
    try (MultilayeredInputStream ms = new MultilayeredInputStream(context, target)) {
        byte[] buffer = new byte[(int) Units.MEBIBYTE];
        int n;
        while ((n = ms.read(buffer)) != -1) {
            digest.update(buffer, 0, n);
            pb.inc(n);
        }
        Arrays.fill(buffer, (byte) 0);
    }
    pb.finish();

    int length = digest.getDigestSize();
    byte[] buffer = new byte[length];
    digest.doFinal(buffer, 0);

    return buffer;
}

From source file:org.bunkr.core.usersec.PasswordProvider.java

License:Open Source License

private byte[] hash(byte[] input) {
    GeneralDigest digest = new SHA256Digest();
    digest.update(input, 0, input.length);
    byte[] buffer = new byte[digest.getDigestSize()];
    digest.doFinal(buffer, 0);
    Arrays.fill(input, (byte) 0);
    return buffer;
}

From source file:test.bunkr.core.streams.TestBlockReaderInputStream.java

License:Open Source License

public byte[] hashUp(byte[] input) {
    GeneralDigest d = new SHA1Digest();
    d.update(input, 0, input.length);/*from  www. j  av  a2s  .c  om*/
    byte[] b = new byte[d.getDigestSize()];
    d.doFinal(b, 0);
    return b;
}