blob: eeb2c8f0977188b2ca5ed5881a28a51803dfc6d5 [file] [log] [blame]
Stuart McCullochbb014372012-06-07 21:57:32 +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 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}