Skip to content

Instantly share code, notes, and snippets.

@Glamdring
Created November 2, 2021 17:03
Show Gist options
  • Save Glamdring/2764b1664d480597d5e7152e9312c01c to your computer and use it in GitHub Desktop.
Save Glamdring/2764b1664d480597d5e7152e9312c01c to your computer and use it in GitHub Desktop.
public class MachineProtocolSignatureVerifier {
public static void main(String[] args) throws Exception {
String toVerify = FileUtils.readFileToString(new File("C:\\Users\\user\\Downloads\\010300076\\010300076.csv"));
String signed = "<<base64 from p7s file>>";
byte[] signedByte = Base64.getDecoder().decode(signed);
Security.addProvider(new BouncyCastleProvider());
CMSSignedData cms = new CMSSignedData(new CMSProcessableByteArray(toVerify.getBytes()), signedByte);
Store<X509CertificateHolder> store = cms.getCertificates();
SignerInformationStore signers = cms.getSignerInfos();
for (SignerInformation signer : signers.getSigners()) {
Collection<X509CertificateHolder> certCollection = store.getMatches(signer.getSID());
Iterator<X509CertificateHolder> certIt = certCollection.iterator();
X509CertificateHolder certHolder = (X509CertificateHolder) certIt.next();
X509Certificate cert = new JcaX509CertificateConverter().setProvider("BC").getCertificate(certHolder);
if (signer.verify(new JcaSimpleSignerInfoVerifierBuilder().setProvider("BC").build(cert))) {
System.out.println("verified");
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment