1. package com.payease.chains.risk.utils;
  2.  
  3. /**
  4. * md5密码加密工具类
  5. * Created by liuxiaoming on 2017/8/28.
  6. */
  7. public class MD5 {
  8. static final int S11 = 7;
  9.  
  10. static final int S12 = 12;
  11.  
  12. static final int S13 = 17;
  13.  
  14. static final int S14 = 22;
  15.  
  16. static final int S21 = 5;
  17.  
  18. static final int S22 = 9;
  19.  
  20. static final int S23 = 14;
  21.  
  22. static final int S24 = 20;
  23.  
  24. static final int S31 = 4;
  25.  
  26. static final int S32 = 11;
  27.  
  28. static final int S33 = 16;
  29.  
  30. static final int S34 = 23;
  31.  
  32. static final int S41 = 6;
  33.  
  34. static final int S42 = 10;
  35.  
  36. static final int S43 = 15;
  37.  
  38. static final int S44 = 21;
  39.  
  40. static final byte PADDING[] = { -128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  41. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  42. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  43. 0, 0, 0, 0, 0, 0, 0 };
  44.  
  45. public static long b2iu(byte b) {
  46. return (b >= 0 ? b : b & 0xff);
  47. }
  48.  
  49. public static String byteHEX(byte ib) {
  50. char Digit[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A',
  51. 'B', 'C', 'D', 'E', 'F' };
  52. char ob[] = new char[2];
  53. ob[0] = Digit[ib >>> 4 & 0xf];
  54. ob[1] = Digit[ib & 0xf];
  55. String s = new String(ob);
  56. return s;
  57. }
  58.  
  59. public static String encode(String s) {
  60. MD5 m = new MD5();
  61. return m.getMD5ofStr(s);
  62. }
  63.  
  64. private long state[];
  65.  
  66. private long count[];
  67.  
  68. private byte buffer[];
  69.  
  70. public String digestHexStr;
  71.  
  72. private byte digest[];
  73.  
  74. public MD5() {
  75. state = new long[4];
  76. count = new long[2];
  77. buffer = new byte[64];
  78. digest = new byte[16];
  79. md5Init();
  80. }
  81.  
  82. private void Decode(long output[], byte input[], int len) {
  83. int i = 0;
  84. for (int j = 0; j < len; j += 4) {
  85. output[i] = b2iu(input[j]) | b2iu(input[j + 1]) << 8
  86. | b2iu(input[j + 2]) << 16 | b2iu(input[j + 3]) << 24;
  87. i++;
  88. }
  89.  
  90. }
  91.  
  92. private void Encode(byte output[], long input[], int len) {
  93. int i = 0;
  94. for (int j = 0; j < len; j += 4) {
  95. output[j] = (byte) (int) (input[i] & 255L);
  96. output[j + 1] = (byte) (int) (input[i] >>> 8 & 255L);
  97. output[j + 2] = (byte) (int) (input[i] >>> 16 & 255L);
  98. output[j + 3] = (byte) (int) (input[i] >>> 24 & 255L);
  99. i++;
  100. }
  101.  
  102. }
  103.  
  104. private long F(long x, long y, long z) {
  105. return x & y | (x ^ 0L - 1L) & z;
  106. }
  107.  
  108. private long FF(long a, long b, long c, long d, long x, long s, long ac) {
  109. a += F(b, c, d) + x + ac;
  110. a = (int) a << (int) s | (int) a >>> (int) (32 - s);
  111. a += b;
  112. return a;
  113. }
  114.  
  115. private long G(long x, long y, long z) {
  116. return x & z | y & (z ^ 0L - 1L);
  117. }
  118.  
  119. public String getMD5ofStr(String inbuf) {
  120.  
  121. md5Init();
  122.  
  123. md5Update(inbuf.getBytes(), inbuf.length());
  124. md5Final();
  125. digestHexStr = "";
  126. for (int i = 0; i < 16; i++)
  127. digestHexStr = String.valueOf(digestHexStr)
  128. + String.valueOf(byteHEX(digest[i]));
  129.  
  130. return digestHexStr;
  131. }
  132.  
  133. private long GG(long a, long b, long c, long d, long x, long s, long ac) {
  134. a += G(b, c, d) + x + ac;
  135. a = (int) a << (int) s | (int) a >>> (int) (32 - s);
  136. a += b;
  137. return a;
  138. }
  139.  
  140. private long H(long x, long y, long z) {
  141. return x ^ y ^ z;
  142. }
  143.  
  144. private long HH(long a, long b, long c, long d, long x, long s, long ac) {
  145. a += H(b, c, d) + x + ac;
  146. a = (int) a << (int) s | (int) a >>> (int) (32 - s);
  147. a += b;
  148. return a;
  149. }
  150.  
  151. private long I(long x, long y, long z) {
  152. return y ^ (x | z ^ 0L - 1L);
  153. }
  154.  
  155. private long II(long a, long b, long c, long d, long x, long s, long ac) {
  156. a += I(b, c, d) + x + ac;
  157. a = (int) a << (int) s | (int) a >>> (int) (32 - s);
  158. a += b;
  159. return a;
  160. }
  161.  
  162. private void md5Final() {
  163. byte bits[] = new byte[8];
  164. Encode(bits, count, 8);
  165. int index = (int) (count[0] >>> 3) & 0x3f;
  166. int padLen = index >= 56 ? 120 - index : 56 - index;
  167. md5Update(PADDING, padLen);
  168. md5Update(bits, 8);
  169. Encode(digest, state, 16);
  170. }
  171.  
  172. private void md5Init() {
  173. count[0] = 0L;
  174. count[1] = 0L;
  175. state[0] = 0x67452301L;
  176. state[1] = 0xefcdab89L;
  177. state[2] = 0x98badcfeL;
  178. state[3] = 0x10325476L;
  179. }
  180.  
  181. private void md5Memcpy(byte output[], byte input[], int outpos, int inpos,
  182. int len) {
  183. for (int i = 0; i < len; i++)
  184. output[outpos + i] = input[inpos + i];
  185.  
  186. }
  187.  
  188. private void md5Transform(byte block[]) {
  189. long a = state[0];
  190. long b = state[1];
  191. long c = state[2];
  192. long d = state[3];
  193. long x[] = new long[16];
  194. Decode(x, block, 64);
  195. a = FF(a, b, c, d, x[0], 7L, 0xd76aa478L);
  196. d = FF(d, a, b, c, x[1], 12L, 0xe8c7b756L);
  197. c = FF(c, d, a, b, x[2], 17L, 0x242070dbL);
  198. b = FF(b, c, d, a, x[3], 22L, 0xc1bdceeeL);
  199. a = FF(a, b, c, d, x[4], 7L, 0xf57c0fafL);
  200. d = FF(d, a, b, c, x[5], 12L, 0x4787c62aL);
  201. c = FF(c, d, a, b, x[6], 17L, 0xa8304613L);
  202. b = FF(b, c, d, a, x[7], 22L, 0xfd469501L);
  203. a = FF(a, b, c, d, x[8], 7L, 0x698098d8L);
  204. d = FF(d, a, b, c, x[9], 12L, 0x8b44f7afL);
  205. c = FF(c, d, a, b, x[10], 17L, 0xffff5bb1L);
  206. b = FF(b, c, d, a, x[11], 22L, 0x895cd7beL);
  207. a = FF(a, b, c, d, x[12], 7L, 0x6b901122L);
  208. d = FF(d, a, b, c, x[13], 12L, 0xfd987193L);
  209. c = FF(c, d, a, b, x[14], 17L, 0xa679438eL);
  210. b = FF(b, c, d, a, x[15], 22L, 0x49b40821L);
  211. a = GG(a, b, c, d, x[1], 5L, 0xf61e2562L);
  212. d = GG(d, a, b, c, x[6], 9L, 0xc040b340L);
  213. c = GG(c, d, a, b, x[11], 14L, 0x265e5a51L);
  214. b = GG(b, c, d, a, x[0], 20L, 0xe9b6c7aaL);
  215. a = GG(a, b, c, d, x[5], 5L, 0xd62f105dL);
  216. d = GG(d, a, b, c, x[10], 9L, 0x2441453L);
  217. c = GG(c, d, a, b, x[15], 14L, 0xd8a1e681L);
  218. b = GG(b, c, d, a, x[4], 20L, 0xe7d3fbc8L);
  219. a = GG(a, b, c, d, x[9], 5L, 0x21e1cde6L);
  220. d = GG(d, a, b, c, x[14], 9L, 0xc33707d6L);
  221. c = GG(c, d, a, b, x[3], 14L, 0xf4d50d87L);
  222. b = GG(b, c, d, a, x[8], 20L, 0x455a14edL);
  223. a = GG(a, b, c, d, x[13], 5L, 0xa9e3e905L);
  224. d = GG(d, a, b, c, x[2], 9L, 0xfcefa3f8L);
  225. c = GG(c, d, a, b, x[7], 14L, 0x676f02d9L);
  226. b = GG(b, c, d, a, x[12], 20L, 0x8d2a4c8aL);
  227. a = HH(a, b, c, d, x[5], 4L, 0xfffa3942L);
  228. d = HH(d, a, b, c, x[8], 11L, 0x8771f681L);
  229. c = HH(c, d, a, b, x[11], 16L, 0x6d9d6122L);
  230. b = HH(b, c, d, a, x[14], 23L, 0xfde5380cL);
  231. a = HH(a, b, c, d, x[1], 4L, 0xa4beea44L);
  232. d = HH(d, a, b, c, x[4], 11L, 0x4bdecfa9L);
  233. c = HH(c, d, a, b, x[7], 16L, 0xf6bb4b60L);
  234. b = HH(b, c, d, a, x[10], 23L, 0xbebfbc70L);
  235. a = HH(a, b, c, d, x[13], 4L, 0x289b7ec6L);
  236. d = HH(d, a, b, c, x[0], 11L, 0xeaa127faL);
  237. c = HH(c, d, a, b, x[3], 16L, 0xd4ef3085L);
  238. b = HH(b, c, d, a, x[6], 23L, 0x4881d05L);
  239. a = HH(a, b, c, d, x[9], 4L, 0xd9d4d039L);
  240. d = HH(d, a, b, c, x[12], 11L, 0xe6db99e5L);
  241. c = HH(c, d, a, b, x[15], 16L, 0x1fa27cf8L);
  242. b = HH(b, c, d, a, x[2], 23L, 0xc4ac5665L);
  243. a = II(a, b, c, d, x[0], 6L, 0xf4292244L);
  244. d = II(d, a, b, c, x[7], 10L, 0x432aff97L);
  245. c = II(c, d, a, b, x[14], 15L, 0xab9423a7L);
  246. b = II(b, c, d, a, x[5], 21L, 0xfc93a039L);
  247. a = II(a, b, c, d, x[12], 6L, 0x655b59c3L);
  248. d = II(d, a, b, c, x[3], 10L, 0x8f0ccc92L);
  249. c = II(c, d, a, b, x[10], 15L, 0xffeff47dL);
  250. b = II(b, c, d, a, x[1], 21L, 0x85845dd1L);
  251. a = II(a, b, c, d, x[8], 6L, 0x6fa87e4fL);
  252. d = II(d, a, b, c, x[15], 10L, 0xfe2ce6e0L);
  253. c = II(c, d, a, b, x[6], 15L, 0xa3014314L);
  254. b = II(b, c, d, a, x[13], 21L, 0x4e0811a1L);
  255. a = II(a, b, c, d, x[4], 6L, 0xf7537e82L);
  256. d = II(d, a, b, c, x[11], 10L, 0xbd3af235L);
  257. c = II(c, d, a, b, x[2], 15L, 0x2ad7d2bbL);
  258. b = II(b, c, d, a, x[9], 21L, 0xeb86d391L);
  259. state[0] += a;
  260. state[1] += b;
  261. state[2] += c;
  262. state[3] += d;
  263. }
  264.  
  265. private void md5Update(byte inbuf[], int inputLen) {
  266. byte block[] = new byte[64];
  267. int index = (int) (count[0] >>> 3) & 0x3f;
  268. if ((count[0] += inputLen << 3) < (inputLen << 3))
  269. count[1]++;
  270. count[1] += inputLen >>> 29;
  271. int partLen = 64 - index;
  272. int i;
  273. if (inputLen >= partLen) {
  274. md5Memcpy(buffer, inbuf, index, 0, partLen);
  275. md5Transform(buffer);
  276. for (i = partLen; i + 63 < inputLen; i += 64) {
  277. md5Memcpy(block, inbuf, 0, i, 64);
  278. md5Transform(block);
  279. }
  280.  
  281. index = 0;
  282. } else
  283. i = 0;
  284. md5Memcpy(buffer, inbuf, index, i, inputLen - i);
  285. }
  286. public static void main(String[] args) {
  287. System.out.println(MD5.encode("1"));
  288. }
  289. }

MD5 工具类的更多相关文章

  1. MD5工具类,提供字符串MD5加密、文件MD5值获取(校验)功能

    MD5工具类,提供字符串MD5加密(校验).文件MD5值获取(校验)功能 : package com.yzu.utils; import java.io.File; import java.io.Fi ...

  2. java MD5工具类

    package com.common.tools; import java.security.MessageDigest; /** * MD5加密工具类 * <功能详细描述> * * @a ...

  3. MD5工具类-详细

    public class MD5Code { /* * 下面这些S11-S44实际上是一个4*4的矩阵,在原始的C实现中是用#define 实现的, 这里把它们实现成为static * final是表 ...

  4. MD5工具类

    package com.liu.hellomavenweb.util; import java.security.MessageDigest; /** * * @author 刘楠 * */ publ ...

  5. 对指定文件生成数字摘要的MD5工具类

    md5特点:压缩性.不可逆性,经常用于传值过程中的值加密或文件加密static char hexdigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', ...

  6. 170405、java版MD5工具类

    package com.rick.utils; import java.security.MessageDigest; import java.security.NoSuchAlgorithmExce ...

  7. MD5工具类--可以直接拿来用不抛异常的MD5Util

    public class MD5Util { private static String byteArrayToHexString(byte b[]) { StringBuffer resultSb ...

  8. c# base64及MD5工具类

    using System; using System.Collections.Generic; using System.Data; using System.IO; using System.Lin ...

  9. 我的Java开发学习之旅------>工具类:Java获取字符串和文件进行MD5值

    ps:这几天本人用百度云盘秒传了几部大片到云盘上,几个G的文件瞬秒竟然显示"上传成功"!这真让我目瞪口呆,要是这样的话,那得多快的网速,这绝对是不可能的,也许这仅是个假象.百度了一 ...

随机推荐

  1. ADF文件在哪个地方?

    Where is ADF file on Tango Device? Ask Question up vote2down votefavorite   I have a Tango tablet de ...

  2. Android-MediaRecorder录像机(视频)

    在上一篇博客,Android-MediaRecorder录制音频,中讲解了使用Android API MediaRecorder 刻录音频,这篇博客主要是介绍 使用MediaRecorder刻录(视频 ...

  3. Dom4j的一个小例子,用于解析xml文件的元素获取方式(转)

    import java.io.File; import java.io.IOException; import javax.xml.parsers.ParserConfigurationExcepti ...

  4. C# 图书整理

    C#测试驱动开发C#设计模式C#高级编程单元测试之道C#版:使用Nunit 继续添加......

  5. Maven Compilation error [package org.testng.annotations does not exist]

    背景 在执行mvn test的时候,提示package org.testng.annotations does not exist 解决办法 Open pom.xml file. Go to &quo ...

  6. asp.net mvc5 下载文件方法

    控制器自带的 FileContentResult 可以让我们很方便的返回文件到服务端,减少了很多步骤.用于下载文件的时候,像视频.文本.图片这种浏览器支持的文件,默认就会被浏览器打开.这时候想让它变成 ...

  7. 使用Hbuilder将自己app发布到App Store(一)

    1.如果你有mac系统那请看第二步. 首先需要一台虚拟机,还需要个插件要不没法装,都在这链接里面了 链接:https://pan.baidu.com/s/1N_pWJWFk-EJILTXuFr6w5g ...

  8. Solr(一) 安装部署环境

    Solr运行环境 第一步:安装java jdk 第二步:安装tomcat服务器 安装过程:参考http://jingyan.baidu.com/article/870c6fc33e62bcb03fe4 ...

  9. cesium编程入门(九)实体 Entity

    cesium编程入门(九)实体 Entity 在cesium编程入门(五)绘制形状提到过添加实体的方法,这一节聊一聊实体相关的一些内容: 先来看 Entity 的各个属性 id 唯一标志,如果没设置, ...

  10. ES6学习之ES5之后新增的字符串方法

    1.字符串模板:用法:`${变量名}` (好像是C#6.0中也引入了类似的方法.C#中的用法:$"我是{变量名}" ---> $"我叫{name}" ,相 ...