Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 1 | package aQute.libg.cryptography; |
| 2 | |
| 3 | import java.io.*; |
| 4 | import java.security.*; |
| 5 | |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 6 | public class SHA256 extends Digest { |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 7 | public final static String ALGORITHM = "SHA256"; |
| 8 | |
| 9 | public static Digester<SHA256> getDigester(OutputStream... out) throws NoSuchAlgorithmException { |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 10 | MessageDigest md = MessageDigest.getInstance(ALGORITHM); |
| 11 | return new Digester<SHA256>(md, out) { |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 12 | @Override |
| 13 | public SHA256 digest() throws Exception { |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 14 | return new SHA256(md.digest()); |
| 15 | } |
| 16 | |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 17 | @Override |
| 18 | public SHA256 digest(byte[] bytes) { |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 19 | return new SHA256(bytes); |
| 20 | } |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 21 | |
| 22 | @Override |
| 23 | public String getAlgorithm() { |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 24 | return ALGORITHM; |
| 25 | } |
| 26 | }; |
| 27 | } |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 28 | |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 29 | public SHA256(byte[] b) { |
| 30 | super(b, 32); |
| 31 | } |
| 32 | |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 33 | @Override |
| 34 | public String getAlgorithm() { |
| 35 | return ALGORITHM; |
| 36 | } |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 37 | |
Stuart McCulloch | 2a0afd6 | 2012-09-06 18:28:06 +0000 | [diff] [blame] | 38 | |
Stuart McCulloch | 42151ee | 2012-07-16 13:43:38 +0000 | [diff] [blame] | 39 | public static SHA256 digest(byte [] data) throws Exception { |
Stuart McCulloch | 2a0afd6 | 2012-09-06 18:28:06 +0000 | [diff] [blame] | 40 | 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 McCulloch | 42151ee | 2012-07-16 13:43:38 +0000 | [diff] [blame] | 48 | } |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 49 | } |