blob: 50b965974dab344e41410ac0b5ec5bff69127daf [file] [log] [blame]
Stuart McCulloch3fdcd852011-10-17 10:31:43 +00001package aQute.libg.cryptography;
2
3import java.io.*;
4import java.security.*;
5
6import aQute.lib.io.*;
7
8public 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}