use of com.github.zhenwei.provider.jcajce.CompositePrivateKey in project LinLong-Java by zhenwei1108.
the class JcaContentSignerBuilder method build.
public ContentSigner build(PrivateKey privateKey) throws OperatorCreationException {
if (privateKey instanceof CompositePrivateKey) {
return buildComposite((CompositePrivateKey) privateKey);
}
try {
final Signature sig = helper.createSignature(sigAlgId);
final AlgorithmIdentifier signatureAlgId = sigAlgId;
if (random != null) {
sig.initSign(privateKey, random);
} else {
sig.initSign(privateKey);
}
return new ContentSigner() {
private OutputStream stream = OutputStreamFactory.createStream(sig);
public AlgorithmIdentifier getAlgorithmIdentifier() {
return signatureAlgId;
}
public OutputStream getOutputStream() {
return stream;
}
public byte[] getSignature() {
try {
return sig.sign();
} catch (SignatureException e) {
throw new RuntimeOperatorException("exception obtaining signature: " + e.getMessage(), e);
}
}
};
} catch (GeneralSecurityException e) {
throw new OperatorCreationException("cannot create signer: " + e.getMessage(), e);
}
}
use of com.github.zhenwei.provider.jcajce.CompositePrivateKey in project LinLong-Java by zhenwei1108.
the class JcaContentSignerBuilder method buildComposite.
private ContentSigner buildComposite(CompositePrivateKey privateKey) throws OperatorCreationException {
try {
List<PrivateKey> privateKeys = privateKey.getPrivateKeys();
final ASN1Sequence sigAlgIds = ASN1Sequence.getInstance(sigAlgId.getParameters());
final Signature[] sigs = new Signature[sigAlgIds.size()];
for (int i = 0; i != sigAlgIds.size(); i++) {
sigs[i] = helper.createSignature(AlgorithmIdentifier.getInstance(sigAlgIds.getObjectAt(i)));
if (random != null) {
sigs[i].initSign(privateKeys.get(i), random);
} else {
sigs[i].initSign(privateKeys.get(i));
}
}
OutputStream sStream = OutputStreamFactory.createStream(sigs[0]);
for (int i = 1; i != sigs.length; i++) {
sStream = new TeeOutputStream(sStream, OutputStreamFactory.createStream(sigs[i]));
}
final OutputStream sigStream = sStream;
return new ContentSigner() {
OutputStream stream = sigStream;
public AlgorithmIdentifier getAlgorithmIdentifier() {
return sigAlgId;
}
public OutputStream getOutputStream() {
return stream;
}
public byte[] getSignature() {
try {
ASN1EncodableVector sigV = new ASN1EncodableVector();
for (int i = 0; i != sigs.length; i++) {
sigV.add(new DERBitString(sigs[i].sign()));
}
return new DERSequence(sigV).getEncoded(ASN1Encoding.DER);
} catch (IOException e) {
throw new RuntimeOperatorException("exception encoding signature: " + e.getMessage(), e);
} catch (SignatureException e) {
throw new RuntimeOperatorException("exception obtaining signature: " + e.getMessage(), e);
}
}
};
} catch (GeneralSecurityException e) {
throw new OperatorCreationException("cannot create signer: " + e.getMessage(), e);
}
}
Aggregations