Java之加密工具类(依赖:java.security.MessageDigest或org.apache.commons.codec.digest.DigestUtils)

依赖于java.security.MessageDigest,支持MD5,SHA-1,SHA-256

 1 import java.security.MessageDigest;

 2 import java.security.NoSuchAlgorithmException;

 3 

 4 /**

 5  * CiphertextUtil

 6  *

 7  * @author ysj

 8  */

 9 public class CiphertextUtil {

10     public static final String MD5 = "MD5";

11     public static final String SHA_1 = "SHA-1";

12     public static final String SHA_256 = "SHA-256";

13     private static final char[] CH_HEX = {'0', '1', '2', '3', '4', '5', '6', '7', '8',

14         '9', 'A', 'B', 'C', 'D', 'E', 'F'};

15 

16     /**

17      * 加密字符串

18      * 

19      * @param sourceStr    需要加密目标字符串

20      * @param algorithmsName 算法名称(如:MD5,SHA-1,SHA-256)

21      * @return

22      */

23     public static String passAlgorithmsCiphering(String sourceStr,String algorithmsName){

24         String password = "";

25         MessageDigest md;

26         try {

27             md = MessageDigest.getInstance(algorithmsName);

28             // 使用指定byte[]更新摘要

29             md.update(sourceStr.getBytes());

30             // 完成计算,返回结果数组

31             byte[] b = md.digest();

32             password = byteArrayToHex(b);

33         } catch (NoSuchAlgorithmException e) {

34             e.printStackTrace();

35         }

36         return password;

37     }

38 

39     /**

40      * 将字节数组转为十六进制字符串

41      *

42      * @param bytes

43      * @return 返回16进制字符串

44      */

45     private static String byteArrayToHex(byte[] bytes) {

46         // 一个字节占8位,一个十六进制字符占4位;十六进制字符数组的长度为字节数组长度的两倍

47         char[] chars = new char[bytes.length * 2];

48         int index = 0;

49         for (byte b : bytes) {

50             // 取字节的高4位

51             chars[index++] = CH_HEX[b >>> 4 & 0xf];

52             // 取字节的低4位

53             chars[index++] = CH_HEX[b & 0xf];

54         }

55         return new String(chars);

56     }

57 }

 

依赖于org.apache.commons.codec.digest.DigestUtils,支持MD2,MD5,SHA-1,SHA-256,SHA-384,SHA-512

Apache下载地址:http://commons.apache.org/proper/commons-codec/download_codec.cgi

 1 import org.apache.commons.codec.digest.DigestUtils;

 2 

 3 /**

 4  * CiphertextUtil

 5  *

 6  * @author ysj

 7  */

 8 public class CiphertextUtil {

 9     public static final String MD2 = "MD2";

10     public static final String MD5 = "MD5";

11     public static final String SHA_1 = "SHA1";

12     public static final String SHA_256 = "SHA256";

13     public static final String SHA_384 = "SHA384";

14     public static final String SHA_512 = "SHA512";

15 

16     /**

17      * 加密字符串

18      * 

19      * @param sourceStr    需要加密目标字符串

20      * @param algorithmsName 算法名称(如:MD2,MD5,SHA1,SHA256,SHA384,SHA512)

21      * @return

22      */

23     public static String passAlgorithmsCiphering(String sourceStr,String algorithmsName){

24         String password = "";

25         switch(algorithmsName){

26         case "MD2":

27             password = DigestUtils.md2Hex(sourceStr);

28             break;

29         case "MD5":

30             password = DigestUtils.md5Hex(sourceStr);

31             break;

32         case "SHA1":

33             password = DigestUtils.sha1Hex(sourceStr);

34             break;

35         case "SHA256":

36             password = DigestUtils.sha256Hex(sourceStr);

37             break;

38         case "SHA384":

39             password = DigestUtils.sha384Hex(sourceStr);

40             break;

41         case "SHA512":

42             password = DigestUtils.sha512Hex(sourceStr);

43             break;

44         }

45         return password;

46     }

47 }

你可能感兴趣的:(MessageDigest)