Search in sources :

Example 11 with Digest

use of org.bouncycastle.crypto.Digest in project fabric-sdk-java by hyperledger.

the class CryptoPrimitives method hash.

// public PrivateKey ecdsaKeyFromPrivate(byte[] key) throws CryptoException {
// try {
// EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(key);
// KeyFactory generator = KeyFactory.getInstance("ECDSA", SECURITY_PROVIDER_NAME);
// PrivateKey privateKey = generator.generatePrivate(privateKeySpec);
// 
// return privateKey;
// } catch (Exception exp) {
// throw new CryptoException("Unable to convert byte[] into PrivateKey", exp);
// }
// }
@Override
public byte[] hash(byte[] input) {
    Digest digest = getHashDigest();
    byte[] retValue = new byte[digest.getDigestSize()];
    digest.update(input, 0, input.length);
    digest.doFinal(retValue, 0);
    return retValue;
}
Also used : Digest(org.bouncycastle.crypto.Digest) SHA256Digest(org.bouncycastle.crypto.digests.SHA256Digest) SHA3Digest(org.bouncycastle.crypto.digests.SHA3Digest)

Example 12 with Digest

use of org.bouncycastle.crypto.Digest in project vsDiaryWriter by shilongdai.

the class HMac method getMac.

@Override
protected Mac getMac(String mode) {
    if (!currentMode.equals(mode) || mac == null) {
        Digest dig = Digesters.getDigester(mode);
        mac = new org.bouncycastle.crypto.macs.HMac(dig);
        currentMode = mode;
    }
    return mac;
}
Also used : Digest(org.bouncycastle.crypto.Digest)

Example 13 with Digest

use of org.bouncycastle.crypto.Digest in project Conversations by siacs.

the class ScramMechanism method digest.

public byte[] digest(byte[] bytes) {
    final Digest digest = getDigest();
    digest.reset();
    digest.update(bytes, 0, bytes.length);
    final byte[] out = new byte[digest.getDigestSize()];
    digest.doFinal(out, 0);
    return out;
}
Also used : Digest(org.bouncycastle.crypto.Digest)

Example 14 with Digest

use of org.bouncycastle.crypto.Digest in project robovm by robovm.

the class DSAParametersGenerator method generateParameters_FIPS186_3.

/**
     * generate suitable parameters for DSA, in line with
     * <i>FIPS 186-3 A.1 Generation of the FFC Primes p and q</i>.
     */
private DSAParameters generateParameters_FIPS186_3() {
    // A.1.1.2 Generation of the Probable Primes p and q Using an Approved Hash Function
    // FIXME This should be configurable (digest size in bits must be >= N)
    Digest d = digest;
    int outlen = d.getDigestSize() * 8;
    // 1. Check that the (L, N) pair is in the list of acceptable (L, N pairs) (see Section 4.2). If
    //    the pair is not in the list, then return INVALID.
    // Note: checked at initialisation
    // 2. If (seedlen < N), then return INVALID.
    // FIXME This should be configurable (must be >= N)
    int seedlen = N;
    byte[] seed = new byte[seedlen / 8];
    // 3. n = ceiling(L ⁄ outlen) – 1.
    int n = (L - 1) / outlen;
    // 4. b = L – 1 – (n ∗ outlen).
    int b = (L - 1) % outlen;
    byte[] output = new byte[d.getDigestSize()];
    for (; ; ) {
        // 5. Get an arbitrary sequence of seedlen bits as the domain_parameter_seed.
        random.nextBytes(seed);
        // 6. U = Hash (domain_parameter_seed) mod 2^(N–1).
        hash(d, seed, output);
        BigInteger U = new BigInteger(1, output).mod(ONE.shiftLeft(N - 1));
        // 7. q = 2^(N–1) + U + 1 – ( U mod 2).
        BigInteger q = ONE.shiftLeft(N - 1).add(U).add(ONE).subtract(U.mod(TWO));
        // TODO Review C.3 for primality checking
        if (!q.isProbablePrime(certainty)) {
            // 9. If q is not a prime, then go to step 5.
            continue;
        }
        // 10. offset = 1.
        // Note: 'offset' value managed incrementally
        byte[] offset = Arrays.clone(seed);
        // 11. For counter = 0 to (4L – 1) do
        int counterLimit = 4 * L;
        for (int counter = 0; counter < counterLimit; ++counter) {
            // 11.1 For j = 0 to n do
            //      Vj = Hash ((domain_parameter_seed + offset + j) mod 2^seedlen).
            // 11.2 W = V0 + (V1 ∗ 2^outlen) + ... + (V^(n–1) ∗ 2^((n–1) ∗ outlen)) + ((Vn mod 2^b) ∗ 2^(n ∗ outlen)).
            // TODO Assemble w as a byte array
            BigInteger W = ZERO;
            for (int j = 0, exp = 0; j <= n; ++j, exp += outlen) {
                inc(offset);
                hash(d, offset, output);
                BigInteger Vj = new BigInteger(1, output);
                if (j == n) {
                    Vj = Vj.mod(ONE.shiftLeft(b));
                }
                W = W.add(Vj.shiftLeft(exp));
            }
            // 11.3 X = W + 2^(L–1). Comment: 0 ≤ W < 2L–1; hence, 2L–1 ≤ X < 2L.
            BigInteger X = W.add(ONE.shiftLeft(L - 1));
            // 11.4 c = X mod 2q.
            BigInteger c = X.mod(q.shiftLeft(1));
            // 11.5 p = X - (c - 1). Comment: p ≡ 1 (mod 2q).
            BigInteger p = X.subtract(c.subtract(ONE));
            // 11.6 If (p < 2^(L - 1)), then go to step 11.9
            if (p.bitLength() != L) {
                continue;
            }
            // TODO Review C.3 for primality checking
            if (p.isProbablePrime(certainty)) {
                //      (optionally) the values of domain_parameter_seed and counter.
                if (usageIndex >= 0) {
                    BigInteger g = calculateGenerator_FIPS186_3_Verifiable(d, p, q, seed, usageIndex);
                    if (g != null) {
                        return new DSAParameters(p, q, g, new DSAValidationParameters(seed, counter, usageIndex));
                    }
                }
                BigInteger g = calculateGenerator_FIPS186_3_Unverifiable(p, q, random);
                return new DSAParameters(p, q, g, new DSAValidationParameters(seed, counter));
            }
        // 11.9 offset = offset + n + 1.      Comment: Increment offset; then, as part of
        //                                    the loop in step 11, increment counter; if
        //                                    counter < 4L, repeat steps 11.1 through 11.8.
        // Note: 'offset' value already incremented in inner loop
        }
    // 12. Go to step 5.
    }
}
Also used : Digest(org.bouncycastle.crypto.Digest) DSAValidationParameters(org.bouncycastle.crypto.params.DSAValidationParameters) BigInteger(java.math.BigInteger) DSAParameters(org.bouncycastle.crypto.params.DSAParameters)

Example 15 with Digest

use of org.bouncycastle.crypto.Digest in project robovm by robovm.

the class SubjectKeyIdentifier method getDigest.

private static byte[] getDigest(SubjectPublicKeyInfo spki) {
    // BEGIN android-changed
    Digest digest = AndroidDigestFactory.getSHA1();
    // END android-changed
    byte[] resBuf = new byte[digest.getDigestSize()];
    byte[] bytes = spki.getPublicKeyData().getBytes();
    digest.update(bytes, 0, bytes.length);
    digest.doFinal(resBuf, 0);
    return resBuf;
}
Also used : Digest(org.bouncycastle.crypto.Digest)

Aggregations

Digest (org.bouncycastle.crypto.Digest)31 SHA256Digest (org.bouncycastle.crypto.digests.SHA256Digest)9 SHA1Digest (org.bouncycastle.crypto.digests.SHA1Digest)8 MGF1ParameterSpec (java.security.spec.MGF1ParameterSpec)5 RSABlindedEngine (org.bouncycastle.crypto.engines.RSABlindedEngine)5 AlgorithmIdentifier (org.bouncycastle.asn1.x509.AlgorithmIdentifier)4 OAEPEncoding (org.bouncycastle.crypto.encodings.OAEPEncoding)4 InvalidParameterException (java.security.InvalidParameterException)3 BigInteger (java.math.BigInteger)2 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)2 InvalidKeyException (java.security.InvalidKeyException)2 SecureRandom (java.security.SecureRandom)2 RSAPrivateKey (java.security.interfaces.RSAPrivateKey)2 RSAPublicKey (java.security.interfaces.RSAPublicKey)2 NoSuchPaddingException (javax.crypto.NoSuchPaddingException)2 OAEPParameterSpec (javax.crypto.spec.OAEPParameterSpec)2 CipherParameters (org.bouncycastle.crypto.CipherParameters)2 DSAParameters (org.bouncycastle.crypto.params.DSAParameters)2 DSAValidationParameters (org.bouncycastle.crypto.params.DSAValidationParameters)2 ParametersWithRandom (org.bouncycastle.crypto.params.ParametersWithRandom)2