Commit d178abfa authored by Lena Heimberger's avatar Lena Heimberger
Browse files

added PDF signature code

parent e6b6469c
package sphincs;
/**
* @author Franco Nieddu, edited by Lena Heimberger (thanks!)
*/
import at.iaik.pq.sphincs.keys.SphincsKeyPairGenerator;
import at.iaik.pq.sphincs.utils.SphincsParams;
import iaik.asn1.ObjectID;
import iaik.asn1.structures.AlgorithmID;
import iaik.asn1.structures.Name;
import iaik.pdf.parameters.PadesBESParameters;
import iaik.pdf.pdfbox.PdfSignatureInstancePdfbox;
import iaik.pdf.signature.PdfSignatureEngine;
import iaik.security.provider.IAIK;
import at.iaik.pq.sphincs.provider.SphincsProvider;
import iaik.x509.X509Certificate;
import java.math.BigInteger;
import java.security.*;
import java.util.Calendar;
import static at.iaik.pq.sphincs.utils.SphincsParams.SphincsParamSet.SHA256_128f_simple;
import static at.iaik.pq.sphincs.utils.SphincsParams.SphincsParamSet.SHA256_256f_simple;
public class SphincsDemo {
public static X509Certificate createCertificate(PublicKey publicKey) throws Exception
{
KeyPair selfsignedKeyPair = KeyPairGenerator.getInstance("RSA").generateKeyPair();
X509Certificate certificate = new X509Certificate();
certificate.setPublicKey(publicKey);
certificate.setSignatureAlgorithm(AlgorithmID.sha256WithRSAEncryption);
certificate.setSerialNumber(BigInteger.TEN);
Calendar calendar = Calendar.getInstance();
certificate.setValidNotBefore(calendar.getTime());
calendar.add(Calendar.YEAR, 1);
certificate.setValidNotAfter(calendar.getTime());
Name name = new Name();
name.addRDN(ObjectID.commonName, "Lena Heimberger");
certificate.setIssuerDN(name);
certificate.setSubjectDN(name);
certificate.sign(AlgorithmID.sha256WithRSAEncryption, selfsignedKeyPair.getPrivate());
return certificate;
}
public static void main(String[] args) throws Exception {
IAIK.addAsProvider();
SphincsProvider.addAsProvider();
String pdfToBeSigned = "sphincs+-round2-specification.pdf";
String pdfSigned = "sphincs+-specification_signed.pdf";
//create SpincsKeyPair
SphincsKeyPairGenerator generator = new SphincsKeyPairGenerator();
SphincsParams sphincsParams=new SphincsParams(SHA256_128f_simple);
generator.initialize(sphincsParams, new SecureRandom());
KeyPair kp = generator.generateKeyPair();
X509Certificate[] chain = new iaik.x509.X509Certificate[1];
chain[0] = createCertificate(kp.getPublic());
PdfSignatureInstancePdfbox instance = (PdfSignatureInstancePdfbox) PdfSignatureEngine.getInstance();
PadesBESParameters p=new PadesBESParameters();
PadesBESParameters params = new PadesBESParameters();
params.setSignatureReason("Showcase for SPHINCS+");
params.setSignatureLocation("Graz");
params.setSignatureContactInfo("Lena Heimberger");
params.setSignatureAlgorithm(sphincsParams.getName());
instance.initSign(pdfToBeSigned, null, pdfSigned, kp.getPrivate(), chain, params);
instance.sign();
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment