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