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

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

Introduction

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

Prototype

public int getByteLength() 

Source Link

Usage

From source file:com.facebook.delegatedrecovery.CountersignedRecoveryToken.java

License:Open Source License

/**
 * Utility method to quickly get a hex-encoded SHA256 digest of the data field
 * of the countersigned token, which contains the original token.
 * /* w ww. j a va  2s.  c  o m*/
 * @return hex encoded string of SHA256 digest
 */
public String getInnerTokenHash() {
    final SHA256Digest digest = new SHA256Digest();
    final byte[] hash = new byte[digest.getByteLength()];
    digest.update(data, 0, data.length);
    digest.doFinal(hash, 0);
    return DelegatedRecoveryUtils.encodeHex(hash);
}

From source file:com.facebook.delegatedrecovery.DelegatedRecoveryUtils.java

License:Open Source License

/**
 * Simple utility method to return a hex-encoded string of the SHA256 digest
 * of a byte[]//from  w  ww .jav a2s  . c  o  m
 * 
 * @param bytes The bytes to encode
 * @return The hex encoded bytes in a String
 */
public static String sha256(final byte[] bytes) {
    // hash of the token to re-identify it later
    final SHA256Digest digest = new SHA256Digest();
    final byte[] hash = new byte[digest.getByteLength()];
    digest.update(bytes, 0, bytes.length);
    digest.doFinal(hash, 0);
    return DelegatedRecoveryUtils.encodeHex(hash);
}

From source file:com.facebook.delegatedrecovery.RecoveryToken.java

License:Open Source License

private byte[] getSignature(final byte[] rawArray, final ECPrivateKey privateKey) throws IOException {
    if (this.signature != null) {
        throw new IllegalStateException("This token already has a signature.");
    }// w ww .  jav  a  2  s.  co  m
    final BigInteger privatePoint = privateKey.getS();

    final SHA256Digest digest = new SHA256Digest();
    final byte[] hash = new byte[digest.getByteLength()];
    digest.update(rawArray, 0, rawArray.length);
    digest.doFinal(hash, 0);

    final ECDSASigner signer = new ECDSASigner(new HMacDSAKCalculator(new SHA256Digest()));
    signer.init(true, new ECPrivateKeyParameters(privatePoint, DelegatedRecoveryUtils.P256_DOMAIN_PARAMS));
    final BigInteger[] signature = signer.generateSignature(hash);
    final ByteArrayOutputStream s = new ByteArrayOutputStream();
    final DERSequenceGenerator seq = new DERSequenceGenerator(s);
    seq.addObject(new ASN1Integer(signature[0]));
    seq.addObject(new ASN1Integer(signature[1]));
    seq.close();

    return s.toByteArray();
}