当前位置: 首页>>代码示例>>Java>>正文


Java DigestInfo类代码示例

本文整理汇总了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;
}
 
开发者ID:egelke,项目名称:eIDSuite,代码行数:74,代码来源:EidCardReader.java


注:本文中的org.spongycastle.asn1.x509.DigestInfo类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。