1 16 17 package org.apache.commons.codec.digest; 18 19 import java.security.MessageDigest ; 20 import java.security.NoSuchAlgorithmException ; 21 22 import org.apache.commons.codec.binary.Hex; 23 24 30 public class DigestUtils { 31 32 39 static MessageDigest getDigest(String algorithm) { 40 try { 41 return MessageDigest.getInstance(algorithm); 42 } catch (NoSuchAlgorithmException e) { 43 throw new RuntimeException (e.getMessage()); 44 } 45 } 46 47 53 private static MessageDigest getMd5Digest() { 54 return getDigest("MD5"); 55 } 56 57 63 private static MessageDigest getShaDigest() { 64 return getDigest("SHA"); 65 } 66 67 74 public static byte[] md5(byte[] data) { 75 return getMd5Digest().digest(data); 76 } 77 78 85 public static byte[] md5(String data) { 86 return md5(data.getBytes()); 87 } 88 89 96 public static String md5Hex(byte[] data) { 97 return new String (Hex.encodeHex(md5(data))); 98 } 99 100 107 public static String md5Hex(String data) { 108 return new String (Hex.encodeHex(md5(data))); 109 } 110 111 118 public static byte[] sha(byte[] data) { 119 return getShaDigest().digest(data); 120 } 121 122 129 public static byte[] sha(String data) { 130 return sha(data.getBytes()); 131 } 132 133 139 public static String shaHex(byte[] data) { 140 return new String (Hex.encodeHex(sha(data))); 141 } 142 143 149 public static String shaHex(String data) { 150 return new String (Hex.encodeHex(sha(data))); 151 } 152 153 } 154 | Popular Tags |