blob: 977eda68adead3a347df39b0a24843cfa003a0ce [file] [log] [blame]
Stuart McCullochbb014372012-06-07 21:57:32 +00001package aQute.libg.cryptography;
2
3import java.io.*;
4import java.security.*;
5
6public class Signer<D extends Digest> extends OutputStream {
7 Signature signature;
Stuart McCulloch2286f232012-06-15 13:27:53 +00008 Digester<D> digester;
9
Stuart McCullochbb014372012-06-07 21:57:32 +000010 Signer(Signature s, Digester<D> digester) {
11 this.signature = s;
Stuart McCulloch2286f232012-06-15 13:27:53 +000012 this.digester = digester;
Stuart McCullochbb014372012-06-07 21:57:32 +000013 }
14
Stuart McCulloch2286f232012-06-15 13:27:53 +000015 @Override
16 public void write(byte[] buffer, int offset, int length) throws IOException {
Stuart McCullochbb014372012-06-07 21:57:32 +000017 try {
18 signature.update(buffer, offset, length);
19 digester.write(buffer, offset, length);
Stuart McCulloch2286f232012-06-15 13:27:53 +000020 }
21 catch (SignatureException e) {
Stuart McCullochbb014372012-06-07 21:57:32 +000022 throw new IOException(e.getLocalizedMessage());
23 }
24 }
25
Stuart McCulloch2286f232012-06-15 13:27:53 +000026 @Override
27 public void write(int b) throws IOException {
Stuart McCullochbb014372012-06-07 21:57:32 +000028 try {
29 signature.update((byte) b);
30 digester.write(b);
Stuart McCulloch2286f232012-06-15 13:27:53 +000031 }
32 catch (SignatureException e) {
Stuart McCullochbb014372012-06-07 21:57:32 +000033 throw new IOException(e.getLocalizedMessage());
34 }
35 }
Stuart McCullochbb014372012-06-07 21:57:32 +000036
37 public Signature signature() throws Exception {
38 return signature;
39 }
Stuart McCulloch2286f232012-06-15 13:27:53 +000040
Stuart McCullochbb014372012-06-07 21:57:32 +000041 public D digest() throws Exception {
42 return digester.digest();
43 }
44}