blob: 1005e0a079d24e08750e51f98a1c2aef5394e219 [file] [log] [blame]
Stuart McCullochf3173222012-06-07 21:57:32 +00001package aQute.libg.cryptography;
2
3import java.io.*;
4import java.security.*;
5
Stuart McCullochf3173222012-06-07 21:57:32 +00006public class Verifier extends OutputStream {
Stuart McCulloch4482c702012-06-15 13:27:53 +00007 final Signature signature;
8 final Digest d;
9
Stuart McCullochf3173222012-06-07 21:57:32 +000010 Verifier(Signature s, Digest d) {
11 this.signature = s;
12 this.d = d;
13 }
Stuart McCulloch4482c702012-06-15 13:27:53 +000014
Stuart McCullochf3173222012-06-07 21:57:32 +000015 @Override
Stuart McCulloch4482c702012-06-15 13:27:53 +000016 public void write(byte[] buffer, int offset, int length) throws IOException {
Stuart McCullochf3173222012-06-07 21:57:32 +000017 try {
18 signature.update(buffer, offset, length);
Stuart McCulloch4482c702012-06-15 13:27:53 +000019 }
20 catch (SignatureException e) {
Stuart McCullochf3173222012-06-07 21:57:32 +000021 throw new IOException(e.getLocalizedMessage());
22 }
23 }
24
25 @Override
Stuart McCulloch4482c702012-06-15 13:27:53 +000026 public void write(int b) throws IOException {
Stuart McCullochf3173222012-06-07 21:57:32 +000027 try {
28 signature.update((byte) b);
Stuart McCulloch4482c702012-06-15 13:27:53 +000029 }
30 catch (SignatureException e) {
Stuart McCullochf3173222012-06-07 21:57:32 +000031 throw new IOException(e.getLocalizedMessage());
32 }
33 }
34
35 public boolean verify() throws Exception {
36 return signature.verify(d.digest());
37 }
Stuart McCulloch4482c702012-06-15 13:27:53 +000038
Stuart McCullochf3173222012-06-07 21:57:32 +000039}