Stuart McCulloch | bb01437 | 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 | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 6 | public class MD5 extends Digest { |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 7 | public final static String ALGORITHM = "MD5"; |
| 8 | |
| 9 | public static Digester<MD5> getDigester(OutputStream... out) throws Exception { |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 10 | return new Digester<MD5>(MessageDigest.getInstance(ALGORITHM), out) { |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 11 | |
| 12 | @Override |
| 13 | public MD5 digest() throws Exception { |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 14 | return new MD5(md.digest()); |
| 15 | } |
| 16 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 17 | @Override |
| 18 | public MD5 digest(byte[] bytes) { |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 19 | return new MD5(bytes); |
| 20 | } |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 21 | |
| 22 | @Override |
| 23 | public String getAlgorithm() { |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 24 | return ALGORITHM; |
| 25 | } |
| 26 | }; |
| 27 | } |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 28 | |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 29 | public MD5(byte[] digest) { |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 30 | super(digest, 16); |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 31 | } |
| 32 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 33 | @Override |
| 34 | public String getAlgorithm() { |
| 35 | return ALGORITHM; |
| 36 | } |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 37 | |
Stuart McCulloch | 39cc9ac | 2012-07-16 13:43:38 +0000 | [diff] [blame] | 38 | public static MD5 digest(byte [] data) throws Exception { |
Stuart McCulloch | b215bfd | 2012-09-06 18:28:06 +0000 | [diff] [blame] | 39 | return getDigester().from(data); |
| 40 | } |
| 41 | |
| 42 | public static MD5 digest(File f) throws NoSuchAlgorithmException, Exception { |
| 43 | return getDigester().from(f); |
| 44 | } |
| 45 | public static MD5 digest(InputStream f) throws NoSuchAlgorithmException, Exception { |
| 46 | return getDigester().from(f); |
Stuart McCulloch | 39cc9ac | 2012-07-16 13:43:38 +0000 | [diff] [blame] | 47 | } |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 48 | } |