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;
}
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);
}
}
Aggregations