Search in sources :

Example 1 with ConcurrentBagEntry

use of org.xipki.common.concurrent.ConcurrentBagEntry 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 2 with ConcurrentBagEntry

use of org.xipki.common.concurrent.ConcurrentBagEntry 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)

Aggregations

Digest (org.bouncycastle.crypto.Digest)2 ConcurrentBagEntry (org.xipki.common.concurrent.ConcurrentBagEntry)2 SHA1Digest (org.bouncycastle.crypto.digests.SHA1Digest)1 RuntimeOperatorException (org.bouncycastle.operator.RuntimeOperatorException)1 ConcurrentBag (org.xipki.common.concurrent.ConcurrentBag)1