Java Code Examples for org.bouncycastle.crypto.digests.SHA256Digest#getDigestSize()

The following examples show how to use org.bouncycastle.crypto.digests.SHA256Digest#getDigestSize() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: KeyBlobCurve25519Unwrap.java    From InflatableDonkey with MIT License 6 votes vote down vote up
public static Optional<byte[]> curve25519Unwrap(
        byte[] myPublicKey,
        byte[] myPrivateKey,
        byte[] otherPublicKey,
        byte[] wrappedKey) {

    SHA256Digest sha256 = new SHA256Digest();

    byte[] shared = Curve25519.agreement(otherPublicKey, myPrivateKey);
    logger.debug("-- curve25519Unwrap() - shared agreement: 0x{}", Hex.toHexString(shared));

    // Stripped down NIST SP 800-56A KDF.
    byte[] counter = new byte[]{0x00, 0x00, 0x00, 0x01};
    byte[] hash = new byte[sha256.getDigestSize()];

    sha256.reset();
    sha256.update(counter, 0, counter.length);
    sha256.update(shared, 0, shared.length);
    sha256.update(otherPublicKey, 0, otherPublicKey.length);
    sha256.update(myPublicKey, 0, myPublicKey.length);
    sha256.doFinal(hash, 0);

    logger.debug("-- curve25519Unwrap() - kek: {}", Hex.toHexString(hash));
    return RFC3394Wrap.unwrapAES(hash, wrappedKey);
}
 
Example 2
Source File: BouncyCastleSHA256.java    From pluotsorbet with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String args[]) {
    byte[] digest = new byte[4096];
    for (int i = 0; i < digest.length; i++) {
        digest[i] = (byte)i;
    }

    long start = JVM.monotonicTimeMillis();
    for (int i = 0; i < 20; i++) {
        SHA256Digest digester = new SHA256Digest();
        byte[] retValue = new byte[digester.getDigestSize()];
        for (int j = 0; j < UPDATES; j++) {
            digester.update(digest, 0, digest.length);
        }
        digester.doFinal(retValue, 0);
    }
    long time = JVM.monotonicTimeMillis() - start;
    System.out.println("BouncyCastleSHA256: " + time);
}
 
Example 3
Source File: TestBouncyCastleSHA256.java    From pluotsorbet with GNU General Public License v2.0 6 votes vote down vote up
public void test(TestHarness th) {
    SHA256Digest md = new SHA256Digest();
    byte[] retValue = new byte[md.getDigestSize()];

    for (int i = 0; i < messages.length; i++) {
        byte[] bytes = messages[i].getBytes();
        md.update(bytes, 0, bytes.length);
        md.doFinal(retValue, 0);
        th.check(Util.hexEncode(retValue).toLowerCase(), digests[i]);
    }

    for (int i = 0; i < 1000000; i++) {
        md.update((byte)'a');
    }
    md.doFinal(retValue, 0);
    th.check(Util.hexEncode(retValue).toLowerCase(), MILLION_A_DIGEST);
}
 
Example 4
Source File: Crypto.java    From webauthndemo with Apache License 2.0 5 votes vote down vote up
public static byte[] sha256Digest(byte[] input) {
  SHA256Digest digest = new SHA256Digest();
  digest.update(input, 0, input.length);
  byte[] result = new byte[digest.getDigestSize()];
  digest.doFinal(result, 0);
  return result;
}
 
Example 5
Source File: FileKeyFactory.java    From LiquidDonkey with MIT License 5 votes vote down vote up
ByteString unwrapCurve25519(KeyBag keyBag, int protectionClass, ByteString key, AESWrap aesWrap, SHA256Digest sha256) {
    if (key.size() != 0x48) {
        logger.warn("-- unwrapCurve25519() > bad key length: {}", Bytes.hex(key));
        return null;
    }

    byte[] myPrivateKey = keyBag.classKey(protectionClass, "KEY").toByteArray();
    if (myPrivateKey == null) {
        logger.warn("-- unwrapCurve25519() > no KEY key for protection class: {}", protectionClass);
        return null;
    }

    byte[] myPublicKey = keyBag.classKey(protectionClass, "PBKY").toByteArray();
    if (myPublicKey == null) {
        logger.warn("-- unwrapCurve25519() > no PBKY key for protection class: {}", protectionClass);
        return null;
    }

    byte[] otherPublicKey = key.substring(0, 32).toByteArray();
    byte[] shared = Curve25519.create().agreement(otherPublicKey, myPrivateKey);
    byte[] pad = new byte[]{0x00, 0x00, 0x00, 0x01};
    byte[] hash = new byte[sha256.getDigestSize()];

    sha256.reset();
    sha256.update(pad, 0, pad.length);
    sha256.update(shared, 0, shared.length);
    sha256.update(otherPublicKey, 0, otherPublicKey.length);
    sha256.update(myPublicKey, 0, myPublicKey.length);
    sha256.doFinal(hash, 0);

    try {
        return ByteString.copyFrom(aesWrap.unwrap(hash, key.substring(0x20, key.size()).toByteArray()));
    } catch (IllegalStateException | InvalidCipherTextException ex) {
        logger.warn("-- unwrapCurve25519() > failed to unwrap key: {} protection class: {} exception: {}",
                Bytes.hex(key), protectionClass, ex);
        return null;
    }
}
 
Example 6
Source File: ValidationChecksum.java    From pgpverify-maven-plugin with Apache License 2.0 5 votes vote down vote up
private byte[] calculateChecksum() {
    final SHA256Digest digest = new SHA256Digest();
    final byte[] result = new byte[digest.getDigestSize()];
    for (final Artifact artifact : this.artifacts) {
        final byte[] id = artifact.getId().getBytes(UTF_8);
        digest.update(id, 0, id.length);
        digest.update((byte) '\0');
    }
    digest.doFinal(result, 0);
    if (LOG.isDebugEnabled()) {
        LOG.debug("Checksum of resolved artifacts: {}", ByteUtils.toHexString(result, "0x", ""));
    }
    return result;
}