Search in sources :

Example 1 with SHA256Digest

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);
    }
}
Also used : SHA256Digest(org.openecard.bouncycastle.crypto.digests.SHA256Digest) X500Name(org.openecard.bouncycastle.asn1.x500.X500Name) ASN1Encodable(org.openecard.bouncycastle.asn1.ASN1Encodable) ASN1String(org.openecard.bouncycastle.asn1.ASN1String) ASN1String(org.openecard.bouncycastle.asn1.ASN1String) ASN1OctetString(org.openecard.bouncycastle.asn1.ASN1OctetString) IOException(java.io.IOException) RDN(org.openecard.bouncycastle.asn1.x500.RDN) AttributeTypeAndValue(org.openecard.bouncycastle.asn1.x500.AttributeTypeAndValue)

Aggregations

IOException (java.io.IOException)1 ASN1Encodable (org.openecard.bouncycastle.asn1.ASN1Encodable)1 ASN1OctetString (org.openecard.bouncycastle.asn1.ASN1OctetString)1 ASN1String (org.openecard.bouncycastle.asn1.ASN1String)1 AttributeTypeAndValue (org.openecard.bouncycastle.asn1.x500.AttributeTypeAndValue)1 RDN (org.openecard.bouncycastle.asn1.x500.RDN)1 X500Name (org.openecard.bouncycastle.asn1.x500.X500Name)1 SHA256Digest (org.openecard.bouncycastle.crypto.digests.SHA256Digest)1