use of org.xipki.util.concurrent.ConcurrentBagEntry in project xipki by xipki.
the class HashCalculator method hash.
public static byte[] hash(HashAlgo hashAlgo, byte[]... datas) {
notNull(hashAlgo, "hashAlgo");
notNull(datas, "datas");
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) {
}
}
if (md0 == null) {
throw new RuntimeOperatorException("could not get idle MessageDigest");
}
try {
Digest md = md0.value();
md.reset();
for (byte[] data : datas) {
if (data != null && data.length > 0) {
md.update(data, 0, data.length);
}
}
byte[] bytes = new byte[md.getDigestSize()];
md.doFinal(bytes, 0);
return bytes;
} finally {
mds.requite(md0);
}
}
use of org.xipki.util.concurrent.ConcurrentBagEntry in project xipki by xipki.
the class HashCalculator method hash.
// method hash
public static byte[] hash(HashAlgo hashAlgo, byte[] data, int offset, int len) {
notNull(hashAlgo, "hashAlgo");
notNull(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) {
}
}
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.xipki.util.concurrent.ConcurrentBagEntry in project xipki by xipki.
the class EmulatorP11Identity method init.
private synchronized void init() throws P11TokenException {
if (initialized) {
return;
}
try {
if (this.publicKey instanceof RSAPublicKey) {
String providerName = "BC";
LOG.info("use provider {}", providerName);
for (int i = 0; i < maxSessions; i++) {
Cipher rsaCipher;
try {
final String algo = "RSA/ECB/NoPadding";
rsaCipher = Cipher.getInstance(algo, providerName);
LOG.info("use cipher algorithm {}", algo);
} catch (NoSuchPaddingException ex) {
throw new P11TokenException("NoSuchPadding", ex);
} catch (NoSuchAlgorithmException ex) {
final String algo = "RSA/NONE/NoPadding";
try {
rsaCipher = Cipher.getInstance(algo, providerName);
LOG.info("use cipher algorithm {}", algo);
} catch (NoSuchPaddingException e1) {
throw new P11TokenException("NoSuchPadding", ex);
}
}
rsaCipher.init(Cipher.ENCRYPT_MODE, signingKey);
rsaCiphers.add(new ConcurrentBagEntry<>(rsaCipher));
}
} else {
String algorithm;
if (this.publicKey instanceof ECPublicKey) {
boolean sm2curve = GMUtil.isSm2primev2Curve(((ECPublicKey) this.publicKey).getParams().getCurve());
algorithm = sm2curve ? null : "NONEwithECDSA";
} else if (this.publicKey instanceof DSAPublicKey) {
algorithm = "NONEwithDSA";
} else if (this.publicKey instanceof EdDSAKey) {
algorithm = null;
} else if (this.publicKey instanceof XDHKey) {
algorithm = null;
} else {
throw new P11TokenException("Currently only RSA, DSA, EC, EC Edwards and EC " + "Montgomery public key are supported, but not " + this.publicKey.getAlgorithm() + " (class: " + this.publicKey.getClass().getName() + ")");
}
if (algorithm != null) {
for (int i = 0; i < maxSessions; i++) {
Signature dsaSignature = Signature.getInstance(algorithm, "BC");
dsaSignature.initSign((PrivateKey) signingKey, random);
dsaSignatures.add(new ConcurrentBagEntry<>(dsaSignature));
}
} else if (this.publicKey instanceof EdDSAKey) {
algorithm = this.publicKey.getAlgorithm();
for (int i = 0; i < maxSessions; i++) {
Signature signature = Signature.getInstance(algorithm, "BC");
signature.initSign((PrivateKey) signingKey);
eddsaSignatures.add(new ConcurrentBagEntry<>(signature));
}
} else if (this.publicKey instanceof XDHKey) {
// do nothing. not suitable for sign.
} else {
for (int i = 0; i < maxSessions; i++) {
SM2Signer sm2signer = new SM2Signer(ECUtil.generatePrivateKeyParameter((PrivateKey) signingKey));
sm2Signers.add(new ConcurrentBagEntry<>(sm2signer));
}
}
}
} catch (GeneralSecurityException ex) {
throw new P11TokenException(ex);
} finally {
initialized = true;
}
}
Aggregations