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

來源:互聯網
上載者:User

標籤:

依賴於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      * @return22      */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 bytes43      * @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      * @return22      */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 }

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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.