本文整理匯總了Java中org.spongycastle.asn1.x509.DigestInfo類的典型用法代碼示例。如果您正苦於以下問題:Java DigestInfo類的具體用法?Java DigestInfo怎麽用?Java DigestInfo使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
DigestInfo類屬於org.spongycastle.asn1.x509包,在下文中一共展示了DigestInfo類的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: signPkcs1
import org.spongycastle.asn1.x509.DigestInfo; //導入依賴的package包/類
public synchronized byte[] signPkcs1(byte[] hash, DigestAlg alg, Key key) throws IOException, UserCancelException {
//Prepare the card for signing
byte[] rsp;
switch (key) {
case NON_REPUDIATION:
rsp = cardReader.transmitApdu(INIT_SIGN_NONREP_RAWPKCS1);
break;
case AUTHENTICATION:
rsp = cardReader.transmitApdu(INIT_SIGN_AUTH_RAWPKCS1);
break;
default:
throw new IllegalArgumentException("Unknown key type");
}
if (validateResponse(rsp) != ApduSwCode.OK) {
throw new APDUException(String.format("The card returned an error: SW=%X %X", rsp[0], rsp[1]), rsp[0], rsp[1]);
}
//Unlock the card with the pin if needed
if (key == Key.NON_REPUDIATION) verifyPin();
//Calculate the signature
byte [] digestInfo;
switch (alg) {
case RAW:
digestInfo = hash;
break;
case SHA1:
digestInfo = new DigestInfo(new AlgorithmIdentifier(X509ObjectIdentifiers.id_SHA1,
DERNull.INSTANCE), hash).getEncoded(ASN1Encoding.DER);
break;
case SHA256:
digestInfo = new DigestInfo(new AlgorithmIdentifier(NISTObjectIdentifiers.id_sha256,
DERNull.INSTANCE), hash).getEncoded(ASN1Encoding.DER);
break;
default:
throw new IllegalArgumentException("Unknown digest algorithm");
}
byte [] cmd = new byte[DO_SIGN.length + digestInfo.length];
System.arraycopy(DO_SIGN, 0, cmd, 0, DO_SIGN.length);
cmd[4] = (byte) digestInfo.length;
System.arraycopy(digestInfo, 0, cmd, DO_SIGN.length, digestInfo.length);
byte[] data;
rsp = cardReader.transmitApdu(cmd);
switch (validateResponse(rsp)) {
case OK:
data = new byte[rsp.length-2];
System.arraycopy(rsp, 0, data, 0, data.length);
break;
case OkGetRsp:
data = getResponse(rsp[1]);
break;
case SecConNotSatisfied:
verifyPin();
rsp = cardReader.transmitApdu(cmd);
switch (validateResponse(rsp)) {
case OK:
data = new byte[rsp.length-2];
System.arraycopy(rsp, 0, data, 0, data.length);
break;
case OkGetRsp:
data = getResponse(rsp[1]);
break;
default:
throw new APDUException(String.format("The card returned an error: SW=%X %X", rsp[0], rsp[1]), rsp[0], rsp[1]);
}
break;
default:
throw new APDUException(String.format("The card returned an error: SW=%X %X", rsp[0], rsp[1]), rsp[0], rsp[1]);
}
return data;
}