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