Md5keyUtil.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. package com.benyanyi.okhttp;
  2. /**
  3. * @author myLove
  4. */
  5. class Md5keyUtil {
  6. /**
  7. * 下面这些S11-S44实际上是一个4*4的矩阵,在原始的C实现中是用#define 实现的, 这里把它们实现成为static final是表示了只读,切能在同一个进程空间内的多个 Instance间共享
  8. */
  9. private static final int S11 = 7;
  10. private static final int S12 = 12;
  11. private static final int S13 = 17;
  12. private static final int S14 = 22;
  13. private static final int S21 = 5;
  14. private static final int S22 = 9;
  15. private static final int S23 = 14;
  16. private static final int S24 = 20;
  17. private static final int S31 = 4;
  18. private static final int S32 = 11;
  19. private static final int S33 = 16;
  20. private static final int S34 = 23;
  21. private static final int S41 = 6;
  22. private static final int S42 = 10;
  23. private static final int S43 = 15;
  24. private static final int S44 = 21;
  25. private static final byte[] PADDING = {-128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  26. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  27. /**
  28. * 下面的三个成员是keyBean计算过程中用到的3个核心数据,在原始的C实现中 被定义到keyBean_CTX结构中
  29. * state (ABCD)
  30. */
  31. private static long[] state = new long[4];
  32. /**
  33. * number of bits, modulo 2^64 (lsb first)
  34. */
  35. private static long[] count = new long[2];
  36. /**
  37. * input buffer
  38. */
  39. private static byte[] buffer = new byte[64];
  40. /**
  41. * digestHexStr是keyBean的唯一一个公共成员,是最新一次计算结果的 16进制ASCII表示.
  42. * digest,是最新一次计算结果的2进制内部表示,表示128bit的keyBean值.
  43. */
  44. private byte[] digest = new byte[16];
  45. /**
  46. * getKeyBeanOfStr是类keyBean最主要的公共方法,入口参数是你想要进行keyBean变换的字符串 返回的是变换完的结果,这个结果是从公共成员digestHexStr取得的.
  47. */
  48. String getKeyBeanOfStr(String inBuf) {
  49. keyBeanInit();
  50. keyBeanUpdate(inBuf.getBytes(), inBuf.length());
  51. keyBeanFinal();
  52. StringBuilder digestHexStr = new StringBuilder();
  53. int size = 16;
  54. for (int i = 0; i < size; i++) {
  55. digestHexStr.append(byteHEX(digest[i]));
  56. }
  57. return digestHexStr.toString();
  58. }
  59. private static Md5keyUtil mBean = null;
  60. static Md5keyUtil newInstance() {
  61. if (mBean == null) {
  62. mBean = new Md5keyUtil();
  63. }
  64. keyBeanInit();
  65. return mBean;
  66. }
  67. /**
  68. * 这是keyBean这个类的标准构造函数,JavaBean要求有一个public的并且没有参数的构造函数
  69. */
  70. private Md5keyUtil() {
  71. keyBeanInit();
  72. return;
  73. }
  74. /**
  75. * keyBeanInit是一个初始化函数,初始化核心变量,装入标准的幻数
  76. */
  77. private static void keyBeanInit() {
  78. count[0] = 0L;
  79. count[1] = 0L;
  80. // /* Load magic initialization constants.
  81. state[0] = 0x67452301L;
  82. state[1] = 0xefcdab89L;
  83. state[2] = 0x98badcfeL;
  84. state[3] = 0x10325476L;
  85. return;
  86. }
  87. /**
  88. * f, g, h ,i 是4个基本的keyBean函数,在原始的keyBean的C实现中,由于它们是 简单的位运算,可能出于效率的考虑把它们实现成了宏,在java中,我们把它们 实现成了private方法,名字保持了原来C中的。
  89. */
  90. private long f(long x, long y, long z) {
  91. return (x & y) | ((~x) & z);
  92. }
  93. private long g(long x, long y, long z) {
  94. return (x & z) | (y & (~z));
  95. }
  96. private long h(long x, long y, long z) {
  97. return x ^ y ^ z;
  98. }
  99. private long i(long x, long y, long z) {
  100. return y ^ (x | (~z));
  101. }
  102. /**
  103. * ff,gg,HH和II将调用F,g,h,I进行近一步变换 ff, gg, hh, and ii transformations for rounds 1, 2, 3, and 4. Rotation is separate
  104. * from addition to prevent recomputation.
  105. */
  106. private long ff(long a, long b, long c, long d, long x, long s, long ac) {
  107. a += f(b, c, d) + x + ac;
  108. a = ((int) a << s) | ((int) a >>> (32 - s));
  109. a += b;
  110. return a;
  111. }
  112. private long gg(long a, long b, long c, long d, long x, long s, long ac) {
  113. a += g(b, c, d) + x + ac;
  114. a = ((int) a << s) | ((int) a >>> (32 - s));
  115. a += b;
  116. return a;
  117. }
  118. private long hh(long a, long b, long c, long d, long x, long s, long ac) {
  119. a += h(b, c, d) + x + ac;
  120. a = ((int) a << s) | ((int) a >>> (32 - s));
  121. a += b;
  122. return a;
  123. }
  124. private long ii(long a, long b, long c, long d, long x, long s, long ac) {
  125. a += i(b, c, d) + x + ac;
  126. a = ((int) a << s) | ((int) a >>> (32 - s));
  127. a += b;
  128. return a;
  129. }
  130. /**
  131. * keyBeanUpdate是keyBean的主计算过程,inbuf是要变换的字节串,inputlen是长度,这个 函数由getkeyBeanofStr调用,调用之前需要调用keyBeaninit,因此把它设计成private的
  132. */
  133. private void keyBeanUpdate(byte[] inbuf, int inputLen) {
  134. int i, index, partLen;
  135. byte[] block = new byte[64];
  136. index = (int) (count[0] >>> 3) & 0x3F;
  137. // /* Update number of bits */
  138. int num1 = 3;
  139. if ((count[0] += (inputLen << num1)) < (inputLen << num1)) {
  140. count[1]++;
  141. }
  142. count[1] += (inputLen >>> 29);
  143. partLen = 64 - index;
  144. // Transform as many times as possible.
  145. int num2 = 63;
  146. int num3 = 64;
  147. if (inputLen >= partLen) {
  148. keyBeanMemcpy(buffer, inbuf, index, 0, partLen);
  149. keyBeanTransform(buffer);
  150. for (i = partLen; i + num2 < inputLen; i += num3) {
  151. keyBeanMemcpy(block, inbuf, 0, i, 64);
  152. keyBeanTransform(block);
  153. }
  154. index = 0;
  155. } else {
  156. i = 0;
  157. }
  158. // /* Buffer remaining input */
  159. keyBeanMemcpy(buffer, inbuf, index, i, inputLen - i);
  160. }
  161. /**
  162. * keyBeanFinal整理和填写输出结果
  163. */
  164. private void keyBeanFinal() {
  165. byte[] bits = new byte[8];
  166. int index, padLen;
  167. // /* Save number of bits */
  168. encode(bits, count, 8);
  169. // /* Pad out to 56 mod 64.
  170. index = (int) (count[0] >>> 3) & 0x3f;
  171. padLen = (index < 56) ? (56 - index) : (120 - index);
  172. keyBeanUpdate(PADDING, padLen);
  173. // /* Append length (before padding) */
  174. keyBeanUpdate(bits, 8);
  175. // /* Store state in digest */
  176. encode(digest, state, 16);
  177. }
  178. /**
  179. * keyBeanMemcpy是一个内部使用的byte数组的块拷贝函数,从input的inpos开始把len长度的 字节拷贝到output的outpos位置开始
  180. */
  181. private void keyBeanMemcpy(byte[] output, byte[] input, int outpos, int inpos, int len) {
  182. int i;
  183. for (i = 0; i < len; i++) {
  184. output[outpos + i] = input[inpos + i];
  185. }
  186. }
  187. /**
  188. * keyBeanTransform是keyBean核心变换程序,有keyBeanUpdate调用,block是分块的原始字节
  189. */
  190. private void keyBeanTransform(byte[] block) {
  191. long a = state[0], b = state[1], c = state[2], d = state[3];
  192. long[] x = new long[16];
  193. decode(x, block, 64);
  194. /* Round 1 */
  195. /* 1 */
  196. a = ff(a, b, c, d, x[0], S11, 0xd76aa478L);
  197. /* 2 */
  198. d = ff(d, a, b, c, x[1], S12, 0xe8c7b756L);
  199. /* 3 */
  200. c = ff(c, d, a, b, x[2], S13, 0x242070dbL);
  201. /* 4 */
  202. b = ff(b, c, d, a, x[3], S14, 0xc1bdceeeL);
  203. /* 5 */
  204. a = ff(a, b, c, d, x[4], S11, 0xf57c0fafL);
  205. /* 6 */
  206. d = ff(d, a, b, c, x[5], S12, 0x4787c62aL);
  207. /* 7 */
  208. c = ff(c, d, a, b, x[6], S13, 0xa8304613L);
  209. /* 8 */
  210. b = ff(b, c, d, a, x[7], S14, 0xfd469501L);
  211. /* 9 */
  212. a = ff(a, b, c, d, x[8], S11, 0x698098d8L);
  213. /* 10 */
  214. d = ff(d, a, b, c, x[9], S12, 0x8b44f7afL);
  215. /* 11 */
  216. c = ff(c, d, a, b, x[10], S13, 0xffff5bb1L);
  217. /* 12 */
  218. b = ff(b, c, d, a, x[11], S14, 0x895cd7beL);
  219. /* 13 */
  220. a = ff(a, b, c, d, x[12], S11, 0x6b901122L);
  221. /* 14 */
  222. d = ff(d, a, b, c, x[13], S12, 0xfd987193L);
  223. /* 15 */
  224. c = ff(c, d, a, b, x[14], S13, 0xa679438eL);
  225. /* 16 */
  226. b = ff(b, c, d, a, x[15], S14, 0x49b40821L);
  227. /* Round 2 */
  228. /* 17 */
  229. a = gg(a, b, c, d, x[1], S21, 0xf61e2562L);
  230. /* 18 */
  231. d = gg(d, a, b, c, x[6], S22, 0xc040b340L);
  232. /* 19 */
  233. c = gg(c, d, a, b, x[11], S23, 0x265e5a51L);
  234. /* 20 */
  235. b = gg(b, c, d, a, x[0], S24, 0xe9b6c7aaL);
  236. /* 21 */
  237. a = gg(a, b, c, d, x[5], S21, 0xd62f105dL);
  238. /* 22 */
  239. d = gg(d, a, b, c, x[10], S22, 0x2441453L);
  240. /* 23 */
  241. c = gg(c, d, a, b, x[15], S23, 0xd8a1e681L);
  242. /* 24 */
  243. b = gg(b, c, d, a, x[4], S24, 0xe7d3fbc8L);
  244. /* 25 */
  245. a = gg(a, b, c, d, x[9], S21, 0x21e1cde6L);
  246. /* 26 */
  247. d = gg(d, a, b, c, x[14], S22, 0xc33707d6L);
  248. /* 27 */
  249. c = gg(c, d, a, b, x[3], S23, 0xf4d50d87L);
  250. /* 28 */
  251. b = gg(b, c, d, a, x[8], S24, 0x455a14edL);
  252. /* 29 */
  253. a = gg(a, b, c, d, x[13], S21, 0xa9e3e905L);
  254. /* 30 */
  255. d = gg(d, a, b, c, x[2], S22, 0xfcefa3f8L);
  256. /* 31 */
  257. c = gg(c, d, a, b, x[7], S23, 0x676f02d9L);
  258. /* 32 */
  259. b = gg(b, c, d, a, x[12], S24, 0x8d2a4c8aL);
  260. /* Round 3 */
  261. /* 33 */
  262. a = hh(a, b, c, d, x[5], S31, 0xfffa3942L);
  263. /* 34 */
  264. d = hh(d, a, b, c, x[8], S32, 0x8771f681L);
  265. /* 35 */
  266. c = hh(c, d, a, b, x[11], S33, 0x6d9d6122L);
  267. /* 36 */
  268. b = hh(b, c, d, a, x[14], S34, 0xfde5380cL);
  269. /* 37 */
  270. a = hh(a, b, c, d, x[1], S31, 0xa4beea44L);
  271. /* 38 */
  272. d = hh(d, a, b, c, x[4], S32, 0x4bdecfa9L);
  273. /* 39 */
  274. c = hh(c, d, a, b, x[7], S33, 0xf6bb4b60L);
  275. b = hh(b, c, d, a, x[10], S34, 0xbebfbc70L);
  276. /* 41 */
  277. a = hh(a, b, c, d, x[13], S31, 0x289b7ec6L);
  278. /* 42 */
  279. d = hh(d, a, b, c, x[0], S32, 0xeaa127faL);
  280. /* 43 */
  281. c = hh(c, d, a, b, x[3], S33, 0xd4ef3085L);
  282. /* 44 */
  283. b = hh(b, c, d, a, x[6], S34, 0x4881d05L);
  284. /* 45 */
  285. a = hh(a, b, c, d, x[9], S31, 0xd9d4d039L);
  286. /* 46 */
  287. d = hh(d, a, b, c, x[12], S32, 0xe6db99e5L);
  288. /* 47 */
  289. c = hh(c, d, a, b, x[15], S33, 0x1fa27cf8L);
  290. /* 48 */
  291. b = hh(b, c, d, a, x[2], S34, 0xc4ac5665L);
  292. /* Round 4 */
  293. /* 49 */
  294. a = ii(a, b, c, d, x[0], S41, 0xf4292244L);
  295. /* 50 */
  296. d = ii(d, a, b, c, x[7], S42, 0x432aff97L);
  297. /* 51 */
  298. c = ii(c, d, a, b, x[14], S43, 0xab9423a7L);
  299. /* 52 */
  300. b = ii(b, c, d, a, x[5], S44, 0xfc93a039L);
  301. /* 53 */
  302. a = ii(a, b, c, d, x[12], S41, 0x655b59c3L);
  303. /* 54 */
  304. d = ii(d, a, b, c, x[3], S42, 0x8f0ccc92L);
  305. /* 55 */
  306. c = ii(c, d, a, b, x[10], S43, 0xffeff47dL);
  307. /* 56 */
  308. b = ii(b, c, d, a, x[1], S44, 0x85845dd1L);
  309. /* 57 */
  310. a = ii(a, b, c, d, x[8], S41, 0x6fa87e4fL);
  311. /* 58 */
  312. d = ii(d, a, b, c, x[15], S42, 0xfe2ce6e0L);
  313. /* 59 */
  314. c = ii(c, d, a, b, x[6], S43, 0xa3014314L);
  315. /* 60 */
  316. b = ii(b, c, d, a, x[13], S44, 0x4e0811a1L);
  317. /* 61 */
  318. a = ii(a, b, c, d, x[4], S41, 0xf7537e82L);
  319. /* 62 */
  320. d = ii(d, a, b, c, x[11], S42, 0xbd3af235L);
  321. /* 63 */
  322. c = ii(c, d, a, b, x[2], S43, 0x2ad7d2bbL);
  323. /* 64 */
  324. b = ii(b, c, d, a, x[9], S44, 0xeb86d391L);
  325. state[0] += a;
  326. state[1] += b;
  327. state[2] += c;
  328. state[3] += d;
  329. }
  330. /**
  331. * Encode把long数组按顺序拆成byte数组,因为java的long类型是64bit的, 只拆低32bit,以适应原始C实现的用途
  332. */
  333. private void encode(byte[] output, long[] input, int len) {
  334. int i, j;
  335. int num = 4;
  336. for (i = 0, j = 0; j < len; i++, j += num) {
  337. output[j] = (byte) (input[i] & 0xffL);
  338. output[j + 1] = (byte) ((input[i] >>> 8) & 0xffL);
  339. output[j + 2] = (byte) ((input[i] >>> 16) & 0xffL);
  340. output[j + 3] = (byte) ((input[i] >>> 24) & 0xffL);
  341. }
  342. }
  343. /**
  344. * Decode把byte数组按顺序合成成long数组,因为java的long类型是64bit的, 只合成低32bit,高32bit清零,以适应原始C实现的用途
  345. */
  346. private void decode(long[] output, byte[] input, int len) {
  347. int i, j;
  348. int num = 4;
  349. for (i = 0, j = 0; j < len; i++, j += num) {
  350. output[i] = b2iu(input[j]) | (b2iu(input[j + 1]) << 8) | (b2iu(input[j + 2]) << 16) | (b2iu(input[j + 3]) << 24);
  351. }
  352. return;
  353. }
  354. /**
  355. * b2iu是我写的一个把byte按照不考虑正负号的原则的"升位"程序,因为java没有unsigned运算
  356. */
  357. private static long b2iu(byte b) {
  358. return b < 0 ? b & 0x7F + 128 : b;
  359. }
  360. /**
  361. * byteHEX(),用来把一个byte类型的数转换成十六进制的ASCII表示, 因为java中的byte的toString无法实现这一点,我们又没有C语言中的 sprintf(outbuf,"%02X",ib)
  362. */
  363. private static String byteHEX(byte ib) {
  364. char[] digit = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'f'};
  365. char[] ob = new char[2];
  366. ob[0] = digit[(ib >>> 4) & 0X0F];
  367. ob[1] = digit[ib & 0X0F];
  368. return new String(ob);
  369. }
  370. }