Stuart McCulloch | bb01437 | 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 | import aQute.lib.io.*; |
| 7 | |
| 8 | public abstract class Digester<T extends Digest> extends OutputStream { |
| 9 | protected MessageDigest md; |
| 10 | OutputStream out[]; |
| 11 | |
| 12 | public Digester(MessageDigest instance, OutputStream ... out) { |
| 13 | md = instance; |
| 14 | this.out = out; |
| 15 | } |
| 16 | |
| 17 | @Override |
| 18 | public void write( byte[] buffer, int offset, int length) throws IOException{ |
| 19 | md.update(buffer,offset,length); |
| 20 | for ( OutputStream o : out ) { |
| 21 | o.write(buffer, offset, length); |
| 22 | } |
| 23 | } |
| 24 | @Override |
| 25 | public void write( int b) throws IOException{ |
| 26 | md.update((byte) b); |
| 27 | for ( OutputStream o : out ) { |
| 28 | o.write(b); |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | public MessageDigest getMessageDigest() throws Exception { |
| 33 | return md; |
| 34 | } |
| 35 | |
| 36 | public T from(InputStream in) throws Exception { |
| 37 | IO.copy(in,this); |
| 38 | return digest(); |
| 39 | } |
| 40 | |
| 41 | public void setOutputs(OutputStream ...out) { |
| 42 | this.out = out; |
| 43 | } |
| 44 | public abstract T digest() throws Exception; |
| 45 | public abstract T digest( byte [] bytes) throws Exception; |
| 46 | public abstract String getAlgorithm(); |
| 47 | } |