{"id":599,"date":"2015-04-29T19:52:21","date_gmt":"2015-04-29T11:52:21","guid":{"rendered":"https:\/\/ai.zhousir.top\/?p=599"},"modified":"2015-04-29T19:52:21","modified_gmt":"2015-04-29T11:52:21","slug":"%e4%b8%80%e4%b8%aa%e5%ae%9e%e7%8e%b0md5%e7%9a%84%e7%ae%80%e6%b4%81%e7%9a%84java%e7%b1%bb","status":"publish","type":"post","link":"https:\/\/ai.zhousir.top\/?p=599","title":{"rendered":"\u4e00\u4e2a\u5b9e\u73b0MD5\u7684\u7b80\u6d01\u7684java\u7c7b"},"content":{"rendered":"<p>\u7531\u4e8e\u6d88\u606f\u6458\u8981\u552f\u4e00\u6027\u548c\u4e0d\u53ef\u9006\u6027\u7684\u7279\u70b9\uff0c\u6240\u4ee5\u4e0d\u5931\u4e3a\u4e00\u79cd\u7b80\u5355\u7684\u5e38\u7528\u7684\u52a0\u5bc6\u624b\u6bb5\uff0c\u6bd4\u5982\u4f60\u53ef\u4ee5\u7528md5\u6765\u52a0\u5bc6\u4f60\u7684\u5e94\u7528\u4e2d\u7684\u7528\u6237\u53e3\u4ee4\u3002<br \/>\n<!--more--><\/p>\n<p>[java]package test;<br \/>\nimport java.security.MessageDigest; <\/p>\n<p>public class StringUtil { <\/p>\n<p>  private final static String[] hexDigits = {<br \/>\n      &quot;0&quot;, &quot;1&quot;, &quot;2&quot;, &quot;3&quot;, &quot;4&quot;, &quot;5&quot;, &quot;6&quot;, &quot;7&quot;,<br \/>\n      &quot;8&quot;, &quot;9&quot;, &quot;a&quot;, &quot;b&quot;, &quot;c&quot;, &quot;d&quot;, &quot;e&quot;, &quot;f&quot;}; <\/p>\n<p>  \/**<br \/>\n   * \u8f6c\u6362\u5b57\u8282\u6570\u7ec4\u4e3a16\u8fdb\u5236\u5b57\u4e32<br \/>\n   * @param b \u5b57\u8282\u6570\u7ec4<br \/>\n   * @return 16\u8fdb\u5236\u5b57\u4e32<br \/>\n   *\/ <\/p>\n<p>  public static String byteArrayToHexString(byte[] b) {<br \/>\n    StringBuffer resultSb = new StringBuffer();<br \/>\n    for (int i = 0; i &lt; b.length; i++) {<br \/>\n      resultSb.append(byteToHexString(b[i]));<br \/>\n    }<br \/>\n    return resultSb.toString();<br \/>\n  } <\/p>\n<p>  private static String byteToHexString(byte b) {<br \/>\n    int n = b;<br \/>\n    if (n &lt; 0)<br \/>\n      n = 256 + n;<br \/>\n    int d1 = n \/ 16;<br \/>\n    int d2 = n % 16;<br \/>\n    return hexDigits[d1] + hexDigits[d2];<br \/>\n  } <\/p>\n<p>  public static String MD5Encode(String origin) {<br \/>\n    String resultString = null; <\/p>\n<p>    try {<br \/>\n      resultString=new String(origin);<br \/>\n      MessageDigest md = MessageDigest.getInstance(&quot;MD5&quot;);<br \/>\n      resultString=byteArrayToHexString(md.digest(resultString.getBytes()));<br \/>\n    }<br \/>\n    catch (Exception ex) { <\/p>\n<p>    }<br \/>\n    return resultString;<br \/>\n  } <\/p>\n<p>  public static void main(String[] args){<br \/>\n    System.err.println(MD5Encode(&quot;a&quot;));<br \/>\n  }<br \/>\n} [\/java]<\/p>\n<p>\u5728RFC 1321\u4e2d\uff0c\u7ed9\u51fa\u4e86Test suite\u7528\u6765\u68c0\u9a8c\u4f60\u7684\u5b9e\u73b0\u662f\u5426\u6b63\u786e\uff1a<\/p>\n<p>MD5 (&#8220;&#8221;) = d41d8cd98f00b204e9800998ecf8427e<br \/>\nMD5 (&#8220;a&#8221;) = 0cc175b9c0f1b6a831c399e269772661<br \/>\nMD5 (&#8220;abc&#8221;) = 900150983cd24fb0d6963f7d28e17f72<br \/>\nMD5 (&#8220;message digest&#8221;) = f96b697d7cb7938d525a2f31aaf161d0<br \/>\nMD5 (&#8220;abcdefghijklmnopqrstuvwxyz&#8221;) = c3fcd3d76192e4007dfb496cca67e13b<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u7531\u4e8e\u6d88\u606f\u6458\u8981\u552f\u4e00\u6027\u548c\u4e0d\u53ef\u9006\u6027\u7684\u7279\u70b9\uff0c\u6240\u4ee5\u4e0d\u5931\u4e3a\u4e00\u79cd\u7b80\u5355\u7684\u5e38\u7528\u7684\u52a0\u5bc6\u624b\u6bb5\uff0c\u6bd4\u5982\u4f60\u53ef\u4ee5\u7528md5\u6765\u52a0\u5bc6\u4f60\u7684\u5e94\u7528\u4e2d\u7684\u7528 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[42],"tags":[57],"_links":{"self":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/599"}],"collection":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=599"}],"version-history":[{"count":1,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/599\/revisions"}],"predecessor-version":[{"id":600,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/599\/revisions\/600"}],"wp:attachment":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=599"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=599"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=599"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}