Как добавить метку времени при генерации цифровой подписи для файла с помощью BouncyCastle?Как добавить метку времени при генерации цифровой подписи для файла с помощью BouncyCastle?
public String generateSignature(String filePath, PrivateKey privateKey, String alias) throws Exception {
KeyStore ks = KeyStore.getInstance("WINDOWS-MY", "SunMSCAPI");
String pin = "";
ks.load(null, pin.toCharArray());
Security.addProvider(new BouncyCastleProvider());
byte[] fileContent = Files.readAllBytes(new File(filePath).toPath());
Signature signature = Signature.getInstance("SHA1withRSA");
signature.initSign(privateKey);
signature.update(fileContent);
// Build CMS
X509Certificate certFromKeystore = (X509Certificate) ks.getCertificate(alias);
List certList = new ArrayList();
CMSTypedData data = new CMSProcessableByteArray(fileContent);
certList.add(certFromKeystore);
Store certs = new JcaCertStore(certList);
CMSSignedDataGenerator gen = new CMSSignedDataGenerator(); //generate PKCS7 siganture messgae
ContentSigner sha1Signer = new JcaContentSignerBuilder("SHA1withRSA").setProvider("SunMSCAPI")
.build(privateKey);
gen.addSignerInfoGenerator(new JcaSignerInfoGeneratorBuilder(new JcaDigestCalculatorProviderBuilder().setProvider("BC").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 + "===" + signedContent.length() + "\n");
String envelopedData = encoder.encode(signedData.getEncoded());
System.out.println("Enveloped data===" + "\n" + envelopedData + "===" + envelopedData.length());
return envelopedData;
}