Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 1 | package aQute.libg.cryptography; |
| 2 | |
| 3 | import java.io.*; |
| 4 | import java.security.*; |
| 5 | |
| 6 | public class Signer<D extends Digest> extends OutputStream { |
| 7 | Signature signature; |
| 8 | Digester<D> digester; |
| 9 | |
| 10 | Signer(Signature s, Digester<D> digester) { |
| 11 | this.signature = s; |
| 12 | this.digester = digester; |
| 13 | } |
| 14 | |
| 15 | @Override public void write(byte[] buffer, int offset, int length) throws IOException { |
| 16 | try { |
| 17 | signature.update(buffer, offset, length); |
| 18 | digester.write(buffer, offset, length); |
| 19 | } catch (SignatureException e) { |
| 20 | throw new IOException(e.getLocalizedMessage()); |
| 21 | } |
| 22 | } |
| 23 | |
| 24 | @Override public void write(int b) throws IOException { |
| 25 | try { |
| 26 | signature.update((byte) b); |
| 27 | digester.write(b); |
| 28 | } catch (SignatureException e) { |
| 29 | throw new IOException(e.getLocalizedMessage()); |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | |
| 34 | public Signature signature() throws Exception { |
| 35 | return signature; |
| 36 | } |
| 37 | |
| 38 | public D digest() throws Exception { |
| 39 | return digester.digest(); |
| 40 | } |
| 41 | } |