use of org.openecard.bouncycastle.crypto.digests.SHA256Digest in project open-ecard by ecsec.
the class ListCertificates method getUniqueIdentifier.
private String getUniqueIdentifier(X509Certificate cert) {
// try to get SERIALNUMBER from subject
X500Name sub = X500Name.getInstance(cert.getSubjectX500Principal().getEncoded());
RDN[] serials = sub.getRDNs(BCStyle.SERIALNUMBER);
if (serials.length >= 1) {
AttributeTypeAndValue serialValueType = serials[0].getFirst();
ASN1Encodable serialValue = serialValueType.getValue();
if (ASN1String.class.isInstance(serialValue)) {
return ASN1String.class.cast(serialValue).getString();
}
}
// no SERIALNUMBER, hash subject and cross fingers that this is unique across replacement cards
try {
SHA256Digest digest = new SHA256Digest();
byte[] subData = sub.getEncoded();
digest.update(subData, 0, subData.length);
byte[] hashResult = new byte[digest.getDigestSize()];
digest.doFinal(hashResult, 0);
String hashedSub = ByteUtils.toWebSafeBase64String(hashResult);
return hashedSub;
} catch (IOException ex) {
throw new RuntimeException("Failed to encode subject.", ex);
}
}
Aggregations