blob: 42baf4e24b5242ffbb21e5ffd1a9dd86a7836a74 [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 SHA256 extends Digest {
Stuart McCulloch4482c702012-06-15 13:27:53 +00007 public final static String ALGORITHM = "SHA256";
8
9 public static Digester<SHA256> getDigester(OutputStream... out) throws NoSuchAlgorithmException {
Stuart McCullochf3173222012-06-07 21:57:32 +000010 MessageDigest md = MessageDigest.getInstance(ALGORITHM);
11 return new Digester<SHA256>(md, out) {
Stuart McCulloch4482c702012-06-15 13:27:53 +000012 @Override
13 public SHA256 digest() throws Exception {
Stuart McCullochf3173222012-06-07 21:57:32 +000014 return new SHA256(md.digest());
15 }
16
Stuart McCulloch4482c702012-06-15 13:27:53 +000017 @Override
18 public SHA256 digest(byte[] bytes) {
Stuart McCullochf3173222012-06-07 21:57:32 +000019 return new SHA256(bytes);
20 }
Stuart McCulloch4482c702012-06-15 13:27:53 +000021
22 @Override
23 public String getAlgorithm() {
Stuart McCullochf3173222012-06-07 21:57:32 +000024 return ALGORITHM;
25 }
26 };
27 }
Stuart McCulloch4482c702012-06-15 13:27:53 +000028
Stuart McCullochf3173222012-06-07 21:57:32 +000029 public SHA256(byte[] b) {
30 super(b, 32);
31 }
32
Stuart McCulloch4482c702012-06-15 13:27:53 +000033 @Override
34 public String getAlgorithm() {
35 return ALGORITHM;
36 }
Stuart McCullochf3173222012-06-07 21:57:32 +000037
Stuart McCulloch2a0afd62012-09-06 18:28:06 +000038
Stuart McCulloch42151ee2012-07-16 13:43:38 +000039 public static SHA256 digest(byte [] data) throws Exception {
Stuart McCulloch2a0afd62012-09-06 18:28:06 +000040 return getDigester().from(data);
41 }
42
43 public static SHA256 digest(File f) throws NoSuchAlgorithmException, Exception {
44 return getDigester().from(f);
45 }
46 public static SHA256 digest(InputStream f) throws NoSuchAlgorithmException, Exception {
47 return getDigester().from(f);
Stuart McCulloch42151ee2012-07-16 13:43:38 +000048 }
Stuart McCullochf3173222012-06-07 21:57:32 +000049}