如何为每个数字证书生成唯一长度的数字签名?

时间:2018-10-29 12:50:59

标签: java certificate bouncycastle x509certificate

我正在使用以下代码生成数字签名,请建议为每个数字证书获取唯一长度的数字签名?

Security.addProvider(new BouncyCastleProvider());
try {
    Signature signature = Signature.getInstance(DIGEST_SHA1);
    signature.initSign(privateKey);
    signature.update("abc".getBytes("UTF-8"));

//  X509Certificate certFromKeystore = (X509Certificate) ks.getCertificate(signName);
    X509Certificate certFromKeystore = x509CertificateToSign;
    // System.out.println(alias+"***"+certFromKeystore.toString());
    List certList = new ArrayList();
    CMSTypedData data = new CMSProcessableByteArray("abc".getBytes());
    certList.add(certFromKeystore);
    Store certs = new JcaCertStore(certList);
    CMSSignedDataGenerator gen = new CMSSignedDataGenerator();
    ContentSigner sha1Signer = new JcaContentSignerBuilder(DIGEST_SHA1)
            .setProvider("SunMSCAPI").build(privateKey);
    gen.addSignerInfoGenerator(new JcaSignerInfoGeneratorBuilder(
            new JcaDigestCalculatorProviderBuilder().setProvider(BC_PROVIDER).build())
                    .build(sha1Signer, certFromKeystore));
    gen.addCertificates(certs);
    CMSSignedData signedData = gen.generate(data, false);
    BASE64Encoder encoder = new BASE64Encoder();
    String signedContent = encoder.encode((byte[]) signedData.getSignedContent().getContent());
    System.out.println(
            "=============Signed content:=============" + "\n" + signedContent + "\n");
    String envelopedData = encoder.encode(signedData.getEncoded());
    System.out.println("==============Enveloped data===============" + "\n"
            + envelopedData + "==" + envelopedData.length());

0 个答案:

没有答案