use of org.demoiselle.signer.timestamp.signer.RequestSigner in project signer by demoiselle.
the class TimeStampOperator method createRequest.
/**
* Creates a time stamp request, signed with the users's certificate.
*
* @param privateKey private key to sign with
* @param certificates certificate chain
* @param content set null if signing only hash
* @param hash set null if signing content
* @return A time stamp request
* @throws CertificateCoreException exception
*/
public byte[] createRequest(PrivateKey privateKey, Certificate[] certificates, byte[] content, byte[] hash) throws CertificateCoreException {
try {
logger.info(timeStampMessagesBundle.getString("info.timestamp.digest"));
Digest digest = DigestFactory.getInstance().factoryDefault();
digest.setAlgorithm(DigestAlgorithmEnum.SHA_256);
byte[] hashedMessage = null;
if (content != null) {
hashedMessage = digest.digest(content);
// logger.info(Base64.toBase64String(hashedMessage));
} else {
hashedMessage = hash;
}
logger.info(timeStampMessagesBundle.getString("info.timestamp.prepare.request"));
TimeStampRequestGenerator timeStampRequestGenerator = new TimeStampRequestGenerator();
timeStampRequestGenerator.setReqPolicy(new ASN1ObjectIdentifier(TimeStampConfig.getInstance().getTSPOid()));
timeStampRequestGenerator.setCertReq(true);
BigInteger nonce = BigInteger.valueOf(100);
timeStampRequest = timeStampRequestGenerator.generate(new ASN1ObjectIdentifier(TSPAlgorithms.SHA256.getId()), hashedMessage, nonce);
byte[] request = timeStampRequest.getEncoded();
logger.info(timeStampMessagesBundle.getString("info.timestamp.sign.request"));
RequestSigner requestSigner = new RequestSigner();
byte[] signedRequest = requestSigner.signRequest(privateKey, certificates, request, "SHA256withRSA");
return signedRequest;
} catch (IOException ex) {
throw new CertificateCoreException(ex.getMessage());
}
}
Aggregations