Stuart McCulloch | 26e7a5a | 2011-10-17 10:31:43 +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 | } catch (SignatureException e) { |
Stuart McCulloch | 08ef6ed | 2011-10-17 10:46:45 +0000 | [diff] [blame] | 19 | throw new IOException(e.getMessage()); |
Stuart McCulloch | 26e7a5a | 2011-10-17 10:31:43 +0000 | [diff] [blame] | 20 | } |
| 21 | } |
| 22 | |
| 23 | @Override public void write(int b) throws IOException { |
| 24 | try { |
| 25 | signature.update((byte) b); |
| 26 | } catch (SignatureException e) { |
Stuart McCulloch | 08ef6ed | 2011-10-17 10:46:45 +0000 | [diff] [blame] | 27 | throw new IOException(e.getMessage()); |
Stuart McCulloch | 26e7a5a | 2011-10-17 10:31:43 +0000 | [diff] [blame] | 28 | } |
| 29 | } |
| 30 | |
| 31 | |
| 32 | public D signature() throws Exception { |
| 33 | return digester.digest(signature().digest()); |
| 34 | } |
| 35 | } |