本文整理汇总了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;
}