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