Search in sources :

Example 21 with Digest

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

the class ResponseCacher method deriveId.

private long deriveId(int issuerId, byte[] identBytes) {
    ConcurrentBagEntry<Digest> digest0 = null;
    try {
        digest0 = idDigesters.borrow(2, TimeUnit.SECONDS);
    } catch (InterruptedException ex) {
    // do nothing
    }
    boolean newDigest = (digest0 == null);
    if (newDigest) {
        digest0 = new ConcurrentBagEntry<Digest>(HashAlgo.SHA1.createDigest());
    }
    byte[] hash = new byte[20];
    try {
        Digest digest = digest0.value();
        digest.reset();
        digest.update(int2Bytes(issuerId), 0, 2);
        digest.update(identBytes, 0, identBytes.length);
        digest.doFinal(hash, 0);
    } finally {
        if (newDigest) {
            idDigesters.add(digest0);
        } else {
            idDigesters.requite(digest0);
        }
    }
    return // ignore the first bit
    (0x7FL & hash[0]) << 56 | (0xFFL & hash[1]) << 48 | (0xFFL & hash[2]) << 40 | (0xFFL & hash[3]) << 32 | (0xFFL & hash[4]) << 24 | (0xFFL & hash[5]) << 16 | (0xFFL & hash[6]) << 8 | (0xFFL & hash[7]);
}
Also used : Digest(org.bouncycastle.crypto.Digest)

Example 22 with Digest

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

the class FpIdCalculator method hash.

/**
 * Hash the data and returns the first 8 bytes of the hash value.
 * @param data data over which the hash value is calculated.
 * @return long represented of the first 8 bytes
 */
public static long hash(byte[] data) {
    ParamUtil.requireNonNull("data", data);
    ConcurrentBagEntry<Digest> md0 = null;
    for (int i = 0; i < 3; i++) {
        try {
            md0 = MDS.borrow(10, TimeUnit.SECONDS);
            break;
        } catch (InterruptedException ex) {
        // CHECKSTYLE:SKIP
        }
    }
    if (md0 == null) {
        throw new RuntimeOperatorException("could not get idle MessageDigest");
    }
    try {
        Digest md = md0.value();
        md.reset();
        md.update(data, 0, data.length);
        byte[] bytes = new byte[md.getDigestSize()];
        md.doFinal(bytes, 0);
        return bytesToLong(bytes);
    } finally {
        MDS.requite(md0);
    }
}
Also used : RuntimeOperatorException(org.bouncycastle.operator.RuntimeOperatorException) Digest(org.bouncycastle.crypto.Digest) SHA1Digest(org.bouncycastle.crypto.digests.SHA1Digest)

Example 23 with Digest

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

the class FpIdCalculator method getMD5MessageDigests.

private static ConcurrentBag<ConcurrentBagEntry<Digest>> getMD5MessageDigests() {
    ConcurrentBag<ConcurrentBagEntry<Digest>> mds = new ConcurrentBag<>();
    for (int i = 0; i < PARALLELISM; i++) {
        Digest md = new SHA1Digest();
        mds.add(new ConcurrentBagEntry<>(md));
    }
    return mds;
}
Also used : Digest(org.bouncycastle.crypto.Digest) SHA1Digest(org.bouncycastle.crypto.digests.SHA1Digest) ConcurrentBagEntry(org.xipki.common.concurrent.ConcurrentBagEntry) SHA1Digest(org.bouncycastle.crypto.digests.SHA1Digest) ConcurrentBag(org.xipki.common.concurrent.ConcurrentBag)

Example 24 with Digest

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

the class HashCalculator method hash.

public static byte[] hash(HashAlgo hashAlgo, byte[] data, int offset, int len) {
    ParamUtil.requireNonNull("hashAlgo", hashAlgo);
    ParamUtil.requireNonNull("data", data);
    if (data.length - offset < len) {
        throw new IndexOutOfBoundsException("data.length - offset < len");
    }
    if (!MDS_MAP.containsKey(hashAlgo)) {
        throw new IllegalArgumentException("unknown hash algo " + hashAlgo);
    }
    ConcurrentBag<ConcurrentBagEntry<Digest>> mds = MDS_MAP.get(hashAlgo);
    ConcurrentBagEntry<Digest> md0 = null;
    for (int i = 0; i < 3; i++) {
        try {
            md0 = mds.borrow(10, TimeUnit.SECONDS);
            break;
        } catch (InterruptedException ex) {
        // CHECKSTYLE:SKIP
        }
    }
    if (md0 == null) {
        throw new RuntimeOperatorException("could not get idle MessageDigest");
    }
    try {
        Digest md = md0.value();
        md.reset();
        md.update(data, offset, len);
        byte[] bytes = new byte[md.getDigestSize()];
        md.doFinal(bytes, 0);
        return bytes;
    } finally {
        mds.requite(md0);
    }
}
Also used : RuntimeOperatorException(org.bouncycastle.operator.RuntimeOperatorException) Digest(org.bouncycastle.crypto.Digest) ConcurrentBagEntry(org.xipki.common.concurrent.ConcurrentBagEntry)

Example 25 with Digest

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

the class XiECContentVerifierProviderBuilder method createSigner.

protected Signer createSigner(AlgorithmIdentifier sigAlgId) throws OperatorCreationException {
    boolean plainDsa = AlgorithmUtil.isPlainECDSASigAlg(sigAlgId);
    if (plainDsa) {
        AlgorithmIdentifier digAlg = digestAlgorithmFinder.find(sigAlgId);
        Digest dig = digestProvider.get(digAlg);
        return new DSAPlainDigestSigner(new ECDSASigner(), dig);
    }
    boolean sm2 = AlgorithmUtil.isSM2SigAlg(sigAlgId);
    if (sm2) {
        AlgorithmIdentifier digAlg = digestAlgorithmFinder.find(sigAlgId);
        if (GMObjectIdentifiers.sm3.equals(digAlg.getAlgorithm())) {
            return new SM2Signer();
        } else {
            throw new OperatorCreationException("cannot create SM2 signer for hash algorithm " + digAlg.getAlgorithm().getId());
        }
    }
    return super.createSigner(sigAlgId);
}
Also used : Digest(org.bouncycastle.crypto.Digest) ECDSASigner(org.bouncycastle.crypto.signers.ECDSASigner) OperatorCreationException(org.bouncycastle.operator.OperatorCreationException) SM2Signer(org.bouncycastle.crypto.signers.SM2Signer) AlgorithmIdentifier(org.bouncycastle.asn1.x509.AlgorithmIdentifier) DSAPlainDigestSigner(org.xipki.security.pkcs12.DSAPlainDigestSigner)

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