Example usage for org.bouncycastle.crypto.digests SHAKEDigest update

List of usage examples for org.bouncycastle.crypto.digests SHAKEDigest update

Introduction

In this page you can find the example usage for org.bouncycastle.crypto.digests SHAKEDigest update.

Prototype

public void update(byte[] in, int inOff, int len) 

Source Link

Usage

From source file:net.java.otr4j.crypto.ed448.Shake256.java

License:LGPL

/**
 * SHAKE-256 hash function.//from  w  w w  . j a v a 2  s  .  com
 *
 * @param input      The input data for the hash function.
 * @param outputSize The output size of the digest.
 */
@Nonnull
static byte[] shake256(final byte[] input, final int outputSize) {
    requireAtLeast(0, outputSize);
    assert !allZeroBytes(
            input) : "Expected non-zero bytes for input. This may indicate that a critical bug is present, or it may be a false warning.";
    final SHAKEDigest digest = new SHAKEDigest(SHAKE_256_LENGTH_BITS);
    digest.update(input, 0, input.length);
    final byte[] result = new byte[outputSize];
    digest.doFinal(result, 0, outputSize);
    return result;
}

From source file:net.java.otr4j.crypto.OtrCryptoEngine4.java

License:LGPL

/**
 * KDF_1 key derivation function.//  w  w  w  . j a  v a  2s.  co m
 * <p>
 * "KDF_1(usageID || values, output_size) = SHAKE-256("OTRv4" || usageID || values, size)"
 *
 * @param dst        The destination byte array, with 32 bytes available for KDF_1 result.
 * @param offset     The offset position to start writing to the destination byte array.
 * @param usageID    The usage ID to be mixed in with the input to KDF1.
 * @param input      The input data to KDF_1.
 * @param outputSize The size of the derivative output.
 */
private static void shake256(final byte[] dst, final int offset, final KDFUsage usageID, final int outputSize,
        final byte[]... input) {
    requireNonNull(dst);
    requireAtLeast(0, outputSize);
    final SHAKEDigest digest = new SHAKEDigest(SHAKE_256_LENGTH_BITS);
    digest.update(OTR4_PREFIX, 0, OTR4_PREFIX.length);
    digest.update(usageID.value);
    for (final byte[] entry : input) {
        assert !allZeroBytes(
                entry) : "Expected non-zero bytes for input. This may indicate that a critical bug is present, or it may be a false warning.";
        digest.update(entry, 0, entry.length);
    }
    digest.doFinal(dst, offset, outputSize);
}

From source file:net.java.otr4j.crypto.OtrCryptoEngine4.java

License:LGPL

/**
 * Generate a new random value in Z_q.//from   w w w .ja  v a 2s .  c  om
 *
 * @param random SecureRandom instance
 * @return Returns a newly generated random value.
 */
public static Scalar generateRandomValueInZq(final SecureRandom random) {
    final byte[] value = randomBytes(random, new byte[SCALAR_LENGTH_BYTES]);
    final byte[] h = new byte[SCALAR_LENGTH_BYTES];
    final SHAKEDigest digest = new SHAKEDigest(SHAKE_256_LENGTH_BITS);
    digest.update(value, 0, value.length);
    digest.doFinal(h, 0, h.length);
    prune(h);
    return decodeScalar(h);
}

From source file:net.java.otr4j.crypto.OtrCryptoEngine4.java

License:LGPL

/**
 * Derive additional extra symmetric keys from the extra symmetric key, that is used as basis.
 *
 * @param index   the index, i.e. the counter for which key is derived.
 * @param context the context value from the TLV payload. (first 4 bytes of the TLV payload)
 * @param baseKey the extra symmetric key, acquired through the Double Ratchet algorithm.
 * @return Returns the derived extra symmetric key.
 *//*  w  w  w  .j  a v  a 2s .c o  m*/
@Nonnull
public static byte[] deriveExtraSymmetricKey(final int index, final byte[] context, final byte[] baseKey) {
    final byte[] idx = { (byte) (index & 0xff), (byte) ((index >>> 8) & 0xff) };
    requireLengthExactly(EXTRA_SYMMETRIC_KEY_CONTEXT_LENGTH_BYTES, context);
    requireLengthExactly(EXTRA_SYMMETRIC_KEY_LENGTH_BYTES, baseKey);
    final byte[] instanceKey = new byte[EXTRA_SYMMETRIC_KEY_LENGTH_BYTES];
    final SHAKEDigest digest = new SHAKEDigest(SHAKE_256_LENGTH_BITS);
    digest.update(OTR4_PREFIX, 0, OTR4_PREFIX.length);
    digest.update(idx, 0, idx.length);
    digest.update(context, 0, context.length);
    digest.update(baseKey, 0, baseKey.length);
    digest.doFinal(instanceKey, 0, EXTRA_SYMMETRIC_KEY_LENGTH_BYTES);
    return instanceKey;
}

From source file:org.hyperledger.fabric.sdk.security.CryptoPrimitives.java

License:Open Source License

/**
 * shake256 do shake256 hashing/*from w  w  w. j  ava 2  s  .c  om*/
 *
 * @param in        byte array to be hashed.
 * @param bitLength of the result.
 * @return
 */
public byte[] shake256(byte[] in, int bitLength) {

    if (bitLength % 8 != 0) {
        throw new IllegalArgumentException("bit length not modulo 8");

    }

    final int byteLen = bitLength / 8;

    SHAKEDigest sd = new SHAKEDigest(256);

    sd.update(in, 0, in.length);

    byte[] out = new byte[byteLen];

    sd.doFinal(out, 0, byteLen);

    return out;

}