PageRenderTime 65ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 1ms

/base/src/main/java/com/twmacinta/util/MD5.java

https://bitbucket.org/rapidpm/rapidpm
Java | 845 lines | 492 code | 108 blank | 245 comment | 73 complexity | b3e2570cf96c1e65254809ac6d1280f1 MD5 | raw file
  1. package com.twmacinta.util;
  2. import java.io.*;
  3. /**
  4. * Fast implementation of RSA's MD5 hash generator in Java JDK Beta-2 or higher.
  5. * <p/>
  6. * Originally written by Santeri Paavolainen, Helsinki Finland 1996.<br>
  7. * (c) Santeri Paavolainen, Helsinki Finland 1996<br>
  8. * Many changes Copyright (c) 2002 - 2008 Timothy W Macinta<br>
  9. * <p/>
  10. * This library is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU Library General Public
  12. * License as published by the Free Software Foundation; either
  13. * version 2.1 of the License, or (at your option) any later version.
  14. * <p/>
  15. * This library is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * Library General Public License for more details.
  19. * <p/>
  20. * You should have received a copy of the GNU Library General Public
  21. * License along with this library; if not, write to the Free
  22. * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  23. * <p/>
  24. * See http://www.twmacinta.com/myjava/fast_md5.php for more information
  25. * on this file and the related files.
  26. * <p/>
  27. * This was originally a rather straight re-implementation of the
  28. * reference implementation given in RFC1321 by RSA. It passes the MD5
  29. * test suite as defined in RFC1321.
  30. * <p/>
  31. * Many optimizations made by Timothy W Macinta. Reduced time to checksum a
  32. * test file in Java alone to roughly half the time taken compared with
  33. * java.security.MessageDigest (within an intepretter). Also added an
  34. * optional native method to reduce the time even further.
  35. * See http://www.twmacinta.com/myjava/fast_md5.php for further information
  36. * on the time improvements achieved.
  37. * <p/>
  38. * Some bug fixes also made by Timothy W Macinta.
  39. * <p/>
  40. * Please note: I (Timothy Macinta) have put this code in the
  41. * com.twmacinta.util package only because it came without a package. I
  42. * was not the the original author of the code, although I did
  43. * optimize it (substantially) and fix some bugs.
  44. * <p/>
  45. * This Java class has been derived from the RSA Data Security, Inc. MD5
  46. * Message-Digest Algorithm and its reference implementation.
  47. * <p/>
  48. * This class will attempt to use a native method to quickly compute
  49. * checksums when the appropriate native library is available. On Linux,
  50. * this library should be named "MD5.so" and on Windows it should be
  51. * named "MD5.dll". The code will attempt to locate the library in the
  52. * following locations in the order given:
  53. * <p/>
  54. * <ol>
  55. * <li>The path specified by the system property
  56. * "MD5.NATIVE_LIB_FILE"
  57. * (be sure to include "MD5.so" or "MD5.dll"
  58. * as appropriate at the end of the path).
  59. * <li>A platform specific directory beneath the "lib/arch/" directory.
  60. * On Linux for x86, this is "lib/arch/linux_x86/". On Windows for
  61. * x86, this is "lib/arch/win32_x86/".
  62. * <li>Within the "lib/" directory.
  63. * <li>Within the current directory.
  64. * </ol>
  65. * <p/>
  66. * <p/>
  67. * If the library is not found, the code will fall back to the default
  68. * (slower) Java code.
  69. * <p/>
  70. * As a side effect of having the code search for the native library,
  71. * SecurityExceptions might be thrown on JVMs that have a restrictive
  72. * SecurityManager. The initialization code attempts to silently discard
  73. * these exceptions and continue, but many SecurityManagers will
  74. * attempt to notify the user directly of all SecurityExceptions thrown.
  75. * Consequently, the code has provisions for skipping the search for
  76. * the native library. Any of these provisions may be used to skip the
  77. * search as long as they are performed <i>before</i> the first
  78. * instance of a MD5 object is constructed (note that
  79. * the convenience stream objects will implicitly create an MD5 object).
  80. * <p/>
  81. * The first option is to set the system property
  82. * "MD5.NO_NATIVE_LIB" to "true" or "1".
  83. * Unfortunately, SecurityManagers may also choose to disallow system
  84. * property setting, so this won't be of use in all cases.
  85. * <p/>
  86. * The second option is to call
  87. * MD5.initNativeLibrary(true) before any MD5 objects
  88. * are constructed.
  89. *
  90. * @author Santeri Paavolainen <sjpaavol@cc.helsinki.fi>
  91. * @author Timothy W Macinta (twm@alum.mit.edu) (optimizations and bug fixes)
  92. */
  93. public class MD5 {
  94. /**
  95. * MD5 state
  96. */
  97. MD5State state;
  98. /**
  99. * If Final() has been called, finals is set to the current finals
  100. * state. Any Update() causes this to be set to null.
  101. */
  102. MD5State finals;
  103. /**
  104. * Padding for Final()
  105. */
  106. static byte[] padding = {
  107. (byte) 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  108. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  109. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  110. };
  111. private static boolean native_lib_loaded = false;
  112. private static boolean native_lib_init_pending = true;
  113. /**
  114. * Initialize MD5 internal state (object can be reused just by
  115. * calling Init() after every Final()
  116. */
  117. public synchronized void Init() {
  118. state = new MD5State();
  119. finals = null;
  120. }
  121. /**
  122. * Class constructor
  123. */
  124. public MD5() {
  125. if (native_lib_init_pending) _initNativeLibrary();
  126. this.Init();
  127. }
  128. /**
  129. * Initialize class, and update hash with ob.toString()
  130. *
  131. * @param ob Object, ob.toString() is used to update hash
  132. * after initialization
  133. */
  134. public MD5(final Object ob) {
  135. this();
  136. Update(ob.toString());
  137. }
  138. private void Decode(final byte[] buffer, final int shift, final int[] out) {
  139. /*len += shift;
  140. for (int i = 0; shift < len; i++, shift += 4) {
  141. out[i] = ((int) (buffer[shift] & 0xff)) |
  142. (((int) (buffer[shift + 1] & 0xff)) << 8) |
  143. (((int) (buffer[shift + 2] & 0xff)) << 16) |
  144. (((int) buffer[shift + 3]) << 24);
  145. }*/
  146. // unrolled loop (original loop shown above)
  147. out[0] = buffer[shift] & 0xff |
  148. ((buffer[shift + 1] & 0xff) << 8) |
  149. ((buffer[shift + 2] & 0xff) << 16) |
  150. (((int) buffer[shift + 3]) << 24);
  151. out[1] = buffer[shift + 4] & 0xff |
  152. ((buffer[shift + 5] & 0xff) << 8) |
  153. ((buffer[shift + 6] & 0xff) << 16) |
  154. (((int) buffer[shift + 7]) << 24);
  155. out[2] = buffer[shift + 8] & 0xff |
  156. ((buffer[shift + 9] & 0xff) << 8) |
  157. ((buffer[shift + 10] & 0xff) << 16) |
  158. (((int) buffer[shift + 11]) << 24);
  159. out[3] = buffer[shift + 12] & 0xff |
  160. ((buffer[shift + 13] & 0xff) << 8) |
  161. ((buffer[shift + 14] & 0xff) << 16) |
  162. (((int) buffer[shift + 15]) << 24);
  163. out[4] = buffer[shift + 16] & 0xff |
  164. ((buffer[shift + 17] & 0xff) << 8) |
  165. ((buffer[shift + 18] & 0xff) << 16) |
  166. (((int) buffer[shift + 19]) << 24);
  167. out[5] = buffer[shift + 20] & 0xff |
  168. ((buffer[shift + 21] & 0xff) << 8) |
  169. ((buffer[shift + 22] & 0xff) << 16) |
  170. (((int) buffer[shift + 23]) << 24);
  171. out[6] = buffer[shift + 24] & 0xff |
  172. ((buffer[shift + 25] & 0xff) << 8) |
  173. ((buffer[shift + 26] & 0xff) << 16) |
  174. (((int) buffer[shift + 27]) << 24);
  175. out[7] = buffer[shift + 28] & 0xff |
  176. ((buffer[shift + 29] & 0xff) << 8) |
  177. ((buffer[shift + 30] & 0xff) << 16) |
  178. (((int) buffer[shift + 31]) << 24);
  179. out[8] = buffer[shift + 32] & 0xff |
  180. ((buffer[shift + 33] & 0xff) << 8) |
  181. ((buffer[shift + 34] & 0xff) << 16) |
  182. (((int) buffer[shift + 35]) << 24);
  183. out[9] = buffer[shift + 36] & 0xff |
  184. ((buffer[shift + 37] & 0xff) << 8) |
  185. ((buffer[shift + 38] & 0xff) << 16) |
  186. (((int) buffer[shift + 39]) << 24);
  187. out[10] = buffer[shift + 40] & 0xff |
  188. ((buffer[shift + 41] & 0xff) << 8) |
  189. ((buffer[shift + 42] & 0xff) << 16) |
  190. (((int) buffer[shift + 43]) << 24);
  191. out[11] = buffer[shift + 44] & 0xff |
  192. ((buffer[shift + 45] & 0xff) << 8) |
  193. ((buffer[shift + 46] & 0xff) << 16) |
  194. (((int) buffer[shift + 47]) << 24);
  195. out[12] = buffer[shift + 48] & 0xff |
  196. ((buffer[shift + 49] & 0xff) << 8) |
  197. ((buffer[shift + 50] & 0xff) << 16) |
  198. (((int) buffer[shift + 51]) << 24);
  199. out[13] = buffer[shift + 52] & 0xff |
  200. ((buffer[shift + 53] & 0xff) << 8) |
  201. ((buffer[shift + 54] & 0xff) << 16) |
  202. (((int) buffer[shift + 55]) << 24);
  203. out[14] = buffer[shift + 56] & 0xff |
  204. ((buffer[shift + 57] & 0xff) << 8) |
  205. ((buffer[shift + 58] & 0xff) << 16) |
  206. (((int) buffer[shift + 59]) << 24);
  207. out[15] = buffer[shift + 60] & 0xff |
  208. ((buffer[shift + 61] & 0xff) << 8) |
  209. ((buffer[shift + 62] & 0xff) << 16) |
  210. (((int) buffer[shift + 63]) << 24);
  211. }
  212. private native void Transform_native(int[] state, byte[] buffer, int shift, int length);
  213. private void Transform(final MD5State state, final byte[] buffer, final int shift, final int[] decode_buf) {
  214. int
  215. a = state.state[0];
  216. int b = state.state[1];
  217. int c = state.state[2];
  218. int d = state.state[3];
  219. final int[] x = decode_buf;
  220. Decode(buffer, shift, decode_buf);
  221. /* Round 1 */
  222. a += ((b & c) | (~b & d)) + x[0] + 0xd76aa478; /* 1 */
  223. a = ((a << 7) | (a >>> 25)) + b;
  224. d += ((a & b) | (~a & c)) + x[1] + 0xe8c7b756; /* 2 */
  225. d = ((d << 12) | (d >>> 20)) + a;
  226. c += ((d & a) | (~d & b)) + x[2] + 0x242070db; /* 3 */
  227. c = ((c << 17) | (c >>> 15)) + d;
  228. b += ((c & d) | (~c & a)) + x[3] + 0xc1bdceee; /* 4 */
  229. b = ((b << 22) | (b >>> 10)) + c;
  230. a += ((b & c) | (~b & d)) + x[4] + 0xf57c0faf; /* 5 */
  231. a = ((a << 7) | (a >>> 25)) + b;
  232. d += ((a & b) | (~a & c)) + x[5] + 0x4787c62a; /* 6 */
  233. d = ((d << 12) | (d >>> 20)) + a;
  234. c += ((d & a) | (~d & b)) + x[6] + 0xa8304613; /* 7 */
  235. c = ((c << 17) | (c >>> 15)) + d;
  236. b += ((c & d) | (~c & a)) + x[7] + 0xfd469501; /* 8 */
  237. b = ((b << 22) | (b >>> 10)) + c;
  238. a += ((b & c) | (~b & d)) + x[8] + 0x698098d8; /* 9 */
  239. a = ((a << 7) | (a >>> 25)) + b;
  240. d += ((a & b) | (~a & c)) + x[9] + 0x8b44f7af; /* 10 */
  241. d = ((d << 12) | (d >>> 20)) + a;
  242. c += ((d & a) | (~d & b)) + x[10] + 0xffff5bb1; /* 11 */
  243. c = ((c << 17) | (c >>> 15)) + d;
  244. b += ((c & d) | (~c & a)) + x[11] + 0x895cd7be; /* 12 */
  245. b = ((b << 22) | (b >>> 10)) + c;
  246. a += ((b & c) | (~b & d)) + x[12] + 0x6b901122; /* 13 */
  247. a = ((a << 7) | (a >>> 25)) + b;
  248. d += ((a & b) | (~a & c)) + x[13] + 0xfd987193; /* 14 */
  249. d = ((d << 12) | (d >>> 20)) + a;
  250. c += ((d & a) | (~d & b)) + x[14] + 0xa679438e; /* 15 */
  251. c = ((c << 17) | (c >>> 15)) + d;
  252. b += ((c & d) | (~c & a)) + x[15] + 0x49b40821; /* 16 */
  253. b = ((b << 22) | (b >>> 10)) + c;
  254. /* Round 2 */
  255. a += ((b & d) | (c & ~d)) + x[1] + 0xf61e2562; /* 17 */
  256. a = ((a << 5) | (a >>> 27)) + b;
  257. d += ((a & c) | (b & ~c)) + x[6] + 0xc040b340; /* 18 */
  258. d = ((d << 9) | (d >>> 23)) + a;
  259. c += ((d & b) | (a & ~b)) + x[11] + 0x265e5a51; /* 19 */
  260. c = ((c << 14) | (c >>> 18)) + d;
  261. b += ((c & a) | (d & ~a)) + x[0] + 0xe9b6c7aa; /* 20 */
  262. b = ((b << 20) | (b >>> 12)) + c;
  263. a += ((b & d) | (c & ~d)) + x[5] + 0xd62f105d; /* 21 */
  264. a = ((a << 5) | (a >>> 27)) + b;
  265. d += ((a & c) | (b & ~c)) + x[10] + 0x02441453; /* 22 */
  266. d = ((d << 9) | (d >>> 23)) + a;
  267. c += ((d & b) | (a & ~b)) + x[15] + 0xd8a1e681; /* 23 */
  268. c = ((c << 14) | (c >>> 18)) + d;
  269. b += ((c & a) | (d & ~a)) + x[4] + 0xe7d3fbc8; /* 24 */
  270. b = ((b << 20) | (b >>> 12)) + c;
  271. a += ((b & d) | (c & ~d)) + x[9] + 0x21e1cde6; /* 25 */
  272. a = ((a << 5) | (a >>> 27)) + b;
  273. d += ((a & c) | (b & ~c)) + x[14] + 0xc33707d6; /* 26 */
  274. d = ((d << 9) | (d >>> 23)) + a;
  275. c += ((d & b) | (a & ~b)) + x[3] + 0xf4d50d87; /* 27 */
  276. c = ((c << 14) | (c >>> 18)) + d;
  277. b += ((c & a) | (d & ~a)) + x[8] + 0x455a14ed; /* 28 */
  278. b = ((b << 20) | (b >>> 12)) + c;
  279. a += ((b & d) | (c & ~d)) + x[13] + 0xa9e3e905; /* 29 */
  280. a = ((a << 5) | (a >>> 27)) + b;
  281. d += ((a & c) | (b & ~c)) + x[2] + 0xfcefa3f8; /* 30 */
  282. d = ((d << 9) | (d >>> 23)) + a;
  283. c += ((d & b) | (a & ~b)) + x[7] + 0x676f02d9; /* 31 */
  284. c = ((c << 14) | (c >>> 18)) + d;
  285. b += ((c & a) | (d & ~a)) + x[12] + 0x8d2a4c8a; /* 32 */
  286. b = ((b << 20) | (b >>> 12)) + c;
  287. /* Round 3 */
  288. a += (b ^ c ^ d) + x[5] + 0xfffa3942; /* 33 */
  289. a = ((a << 4) | (a >>> 28)) + b;
  290. d += (a ^ b ^ c) + x[8] + 0x8771f681; /* 34 */
  291. d = ((d << 11) | (d >>> 21)) + a;
  292. c += (d ^ a ^ b) + x[11] + 0x6d9d6122; /* 35 */
  293. c = ((c << 16) | (c >>> 16)) + d;
  294. b += (c ^ d ^ a) + x[14] + 0xfde5380c; /* 36 */
  295. b = ((b << 23) | (b >>> 9)) + c;
  296. a += (b ^ c ^ d) + x[1] + 0xa4beea44; /* 37 */
  297. a = ((a << 4) | (a >>> 28)) + b;
  298. d += (a ^ b ^ c) + x[4] + 0x4bdecfa9; /* 38 */
  299. d = ((d << 11) | (d >>> 21)) + a;
  300. c += (d ^ a ^ b) + x[7] + 0xf6bb4b60; /* 39 */
  301. c = ((c << 16) | (c >>> 16)) + d;
  302. b += (c ^ d ^ a) + x[10] + 0xbebfbc70; /* 40 */
  303. b = ((b << 23) | (b >>> 9)) + c;
  304. a += (b ^ c ^ d) + x[13] + 0x289b7ec6; /* 41 */
  305. a = ((a << 4) | (a >>> 28)) + b;
  306. d += (a ^ b ^ c) + x[0] + 0xeaa127fa; /* 42 */
  307. d = ((d << 11) | (d >>> 21)) + a;
  308. c += (d ^ a ^ b) + x[3] + 0xd4ef3085; /* 43 */
  309. c = ((c << 16) | (c >>> 16)) + d;
  310. b += (c ^ d ^ a) + x[6] + 0x04881d05; /* 44 */
  311. b = ((b << 23) | (b >>> 9)) + c;
  312. a += (b ^ c ^ d) + x[9] + 0xd9d4d039; /* 33 */
  313. a = ((a << 4) | (a >>> 28)) + b;
  314. d += (a ^ b ^ c) + x[12] + 0xe6db99e5; /* 34 */
  315. d = ((d << 11) | (d >>> 21)) + a;
  316. c += (d ^ a ^ b) + x[15] + 0x1fa27cf8; /* 35 */
  317. c = ((c << 16) | (c >>> 16)) + d;
  318. b += (c ^ d ^ a) + x[2] + 0xc4ac5665; /* 36 */
  319. b = ((b << 23) | (b >>> 9)) + c;
  320. /* Round 4 */
  321. a += (c ^ (b | ~d)) + x[0] + 0xf4292244; /* 49 */
  322. a = ((a << 6) | (a >>> 26)) + b;
  323. d += (b ^ (a | ~c)) + x[7] + 0x432aff97; /* 50 */
  324. d = ((d << 10) | (d >>> 22)) + a;
  325. c += (a ^ (d | ~b)) + x[14] + 0xab9423a7; /* 51 */
  326. c = ((c << 15) | (c >>> 17)) + d;
  327. b += (d ^ (c | ~a)) + x[5] + 0xfc93a039; /* 52 */
  328. b = ((b << 21) | (b >>> 11)) + c;
  329. a += (c ^ (b | ~d)) + x[12] + 0x655b59c3; /* 53 */
  330. a = ((a << 6) | (a >>> 26)) + b;
  331. d += (b ^ (a | ~c)) + x[3] + 0x8f0ccc92; /* 54 */
  332. d = ((d << 10) | (d >>> 22)) + a;
  333. c += (a ^ (d | ~b)) + x[10] + 0xffeff47d; /* 55 */
  334. c = ((c << 15) | (c >>> 17)) + d;
  335. b += (d ^ (c | ~a)) + x[1] + 0x85845dd1; /* 56 */
  336. b = ((b << 21) | (b >>> 11)) + c;
  337. a += (c ^ (b | ~d)) + x[8] + 0x6fa87e4f; /* 57 */
  338. a = ((a << 6) | (a >>> 26)) + b;
  339. d += (b ^ (a | ~c)) + x[15] + 0xfe2ce6e0; /* 58 */
  340. d = ((d << 10) | (d >>> 22)) + a;
  341. c += (a ^ (d | ~b)) + x[6] + 0xa3014314; /* 59 */
  342. c = ((c << 15) | (c >>> 17)) + d;
  343. b += (d ^ (c | ~a)) + x[13] + 0x4e0811a1; /* 60 */
  344. b = ((b << 21) | (b >>> 11)) + c;
  345. a += (c ^ (b | ~d)) + x[4] + 0xf7537e82; /* 61 */
  346. a = ((a << 6) | (a >>> 26)) + b;
  347. d += (b ^ (a | ~c)) + x[11] + 0xbd3af235; /* 62 */
  348. d = ((d << 10) | (d >>> 22)) + a;
  349. c += (a ^ (d | ~b)) + x[2] + 0x2ad7d2bb; /* 63 */
  350. c = ((c << 15) | (c >>> 17)) + d;
  351. b += (d ^ (c | ~a)) + x[9] + 0xeb86d391; /* 64 */
  352. b = ((b << 21) | (b >>> 11)) + c;
  353. state.state[0] += a;
  354. state.state[1] += b;
  355. state.state[2] += c;
  356. state.state[3] += d;
  357. }
  358. /**
  359. * Updates hash with the bytebuffer given (using at maximum length bytes from
  360. * that buffer)
  361. *
  362. * @param stat Which state is updated
  363. * @param buffer Array of bytes to be hashed
  364. * @param offset Offset to buffer array
  365. * @param length Use at maximum `length' bytes (absolute
  366. * maximum is buffer.length)
  367. */
  368. public void Update(final MD5State stat, final byte[] buffer, final int offset, int length) {
  369. int index;
  370. int partlen;
  371. int i;
  372. final int start;
  373. finals = null;
  374. /* Length can be told to be shorter, but not inter */
  375. if ((length - offset) > buffer.length)
  376. length = buffer.length - offset;
  377. /* compute number of bytes mod 64 */
  378. index = (int) (stat.count & 0x3f);
  379. stat.count += length;
  380. partlen = 64 - index;
  381. if (length >= partlen) {
  382. // update state (using native method) to reflect input
  383. if (native_lib_loaded) {
  384. if (partlen == 64) {
  385. partlen = 0;
  386. } else {
  387. for (i = 0; i < partlen; i++)
  388. stat.buffer[i + index] = buffer[i + offset];
  389. Transform_native(stat.state, stat.buffer, 0, 64);
  390. }
  391. i = partlen + ((length - partlen) / 64) * 64;
  392. // break into chunks to guard against stack overflow in JNI
  393. int transformLength = length - partlen;
  394. int transformOffset = partlen + offset;
  395. final int MAX_LENGTH = 65536; // prevent stack overflow in JNI
  396. while (true) {
  397. if (transformLength > MAX_LENGTH) {
  398. Transform_native(stat.state, buffer, transformOffset, MAX_LENGTH);
  399. transformLength -= MAX_LENGTH;
  400. transformOffset += MAX_LENGTH;
  401. } else {
  402. Transform_native(stat.state, buffer, transformOffset, transformLength);
  403. break;
  404. }
  405. }
  406. }
  407. // update state (using only Java) to reflect input
  408. else {
  409. final int[] decode_buf = new int[16];
  410. if (partlen == 64) {
  411. partlen = 0;
  412. } else {
  413. for (i = 0; i < partlen; i++)
  414. stat.buffer[i + index] = buffer[i + offset];
  415. Transform(stat, stat.buffer, 0, decode_buf);
  416. }
  417. for (i = partlen; (i + 63) < length; i += 64) {
  418. Transform(stat, buffer, i + offset, decode_buf);
  419. }
  420. }
  421. index = 0;
  422. } else {
  423. i = 0;
  424. }
  425. /* buffer remaining input */
  426. if (i < length) {
  427. start = i;
  428. for (; i < length; i++) {
  429. stat.buffer[index + i - start] = buffer[i + offset];
  430. }
  431. }
  432. }
  433. /*
  434. * Update()s for other datatypes than byte[] also. Update(byte[], int)
  435. * is only the main driver.
  436. */
  437. /**
  438. * Plain update, updates this object
  439. *
  440. * @param buffer
  441. * @param offset
  442. * @param length
  443. */
  444. public void Update(final byte[] buffer, final int offset, final int length) {
  445. Update(this.state, buffer, offset, length);
  446. }
  447. public void Update(final byte[] buffer, final int length) {
  448. Update(this.state, buffer, 0, length);
  449. }
  450. /**
  451. * Updates hash with given array of bytes
  452. *
  453. * @param buffer Array of bytes to use for updating the hash
  454. */
  455. public void Update(final byte[] buffer) {
  456. Update(buffer, 0, buffer.length);
  457. }
  458. /**
  459. * Updates hash with a single byte
  460. *
  461. * @param b Single byte to update the hash
  462. */
  463. public void Update(final byte b) {
  464. final byte[] buffer = new byte[1];
  465. buffer[0] = b;
  466. Update(buffer, 1);
  467. }
  468. /**
  469. * Update buffer with given string. Note that because the version of
  470. * the s.getBytes() method without parameters is used to convert the
  471. * string to a byte array, the results of this method may be different
  472. * on different platforms. The s.getBytes() method converts the string
  473. * into a byte array using the current platform's default character set
  474. * and may therefore have different results on platforms with different
  475. * default character sets. If a version that works consistently
  476. * across platforms with different default character sets is desired,
  477. * use the overloaded version of the Update() method which takes a
  478. * string and a character encoding.
  479. *
  480. * @param s String to be update to hash (is used as s.getBytes())
  481. */
  482. public void Update(final String s) {
  483. final byte[] chars = s.getBytes();
  484. Update(chars, chars.length);
  485. }
  486. /**
  487. * Update buffer with given string using the given encoding. If the
  488. * given encoding is null, the encoding "ISO8859_1" is used.
  489. *
  490. * @param s String to be update to hash (is used as
  491. * s.getBytes(charset_name))
  492. * @param charset_name The character set to use to convert s to a
  493. * byte array, or null if the "ISO8859_1"
  494. * character set is desired.
  495. * @throws UnsupportedEncodingException If the named
  496. * charset is not supported.
  497. */
  498. public void Update(final String s, String charset_name) throws UnsupportedEncodingException {
  499. if (charset_name == null) charset_name = "ISO8859_1";
  500. final byte[] chars = s.getBytes(charset_name);
  501. Update(chars, chars.length);
  502. }
  503. /**
  504. * Update buffer with a single integer (only & 0xff part is used,
  505. * as a byte)
  506. *
  507. * @param i Integer value, which is then converted to byte as i & 0xff
  508. */
  509. public void Update(final int i) {
  510. Update((byte) (i & 0xff));
  511. }
  512. private byte[] Encode(final int[] input, final int len) {
  513. int i;
  514. int j;
  515. final byte[] out;
  516. out = new byte[len];
  517. for (i = j = 0; j < len; i++, j += 4) {
  518. out[j] = (byte) (input[i] & 0xff);
  519. out[j + 1] = (byte) ((input[i] >>> 8) & 0xff);
  520. out[j + 2] = (byte) ((input[i] >>> 16) & 0xff);
  521. out[j + 3] = (byte) ((input[i] >>> 24) & 0xff);
  522. }
  523. return out;
  524. }
  525. /**
  526. * Returns array of bytes (16 bytes) representing hash as of the
  527. * current state of this object. Note: getting a hash does not
  528. * invalidate the hash object, it only creates a copy of the real
  529. * state which is finalized.
  530. *
  531. * @return Array of 16 bytes, the hash of all updated bytes
  532. */
  533. public synchronized byte[] Final() {
  534. final byte[] bits;
  535. final int index;
  536. final int padlen;
  537. final MD5State fin;
  538. if (finals == null) {
  539. fin = new MD5State(state);
  540. final int[] count_ints = {(int) (fin.count << 3), (int) (fin.count >> 29)};
  541. bits = Encode(count_ints, 8);
  542. index = (int) (fin.count & 0x3f);
  543. padlen = (index < 56) ? (56 - index) : (120 - index);
  544. Update(fin, padding, 0, padlen);
  545. Update(fin, bits, 0, 8);
  546. /* Update() sets finals to null */
  547. finals = fin;
  548. }
  549. return Encode(finals.state, 16);
  550. }
  551. private static final char[] HEX_CHARS = {'0', '1', '2', '3',
  552. '4', '5', '6', '7',
  553. '8', '9', 'a', 'b',
  554. 'c', 'd', 'e', 'f',};
  555. /**
  556. * Turns array of bytes into string representing each byte as
  557. * unsigned hex number.
  558. *
  559. * @param hash Array of bytes to convert to hex-string
  560. * @return Generated hex string
  561. */
  562. public static String asHex(final byte[] hash) {
  563. final char[] buf = new char[hash.length * 2];
  564. for (int i = 0, x = 0; i < hash.length; i++) {
  565. buf[x++] = HEX_CHARS[(hash[i] >>> 4) & 0xf];
  566. buf[x++] = HEX_CHARS[hash[i] & 0xf];
  567. }
  568. return new String(buf);
  569. }
  570. /**
  571. * Returns 32-character hex representation of this objects hash
  572. *
  573. * @return String of this object's hash
  574. */
  575. public String asHex() {
  576. return asHex(this.Final());
  577. }
  578. public static final synchronized void initNativeLibrary(final boolean disallow_lib_loading) {
  579. if (disallow_lib_loading) {
  580. native_lib_init_pending = false;
  581. } else {
  582. _initNativeLibrary();
  583. }
  584. }
  585. private static synchronized void _initNativeLibrary() {
  586. if (!native_lib_init_pending) return;
  587. native_lib_loaded = _loadNativeLibrary();
  588. native_lib_init_pending = false;
  589. }
  590. private static synchronized boolean _loadNativeLibrary() {
  591. try {
  592. // don't try to load if the right property is set
  593. String prop = System.getProperty("MD5.NO_NATIVE_LIB");
  594. if (prop != null) {
  595. prop = prop.trim();
  596. if (prop.equalsIgnoreCase("true") || prop.equals("1")) return false;
  597. }
  598. // the library to load can be specified as a property
  599. File f;
  600. prop = System.getProperty("MD5.NATIVE_LIB_FILE");
  601. if (prop != null) {
  602. f = new File(prop);
  603. if (f.canRead()) {
  604. System.load(f.getAbsolutePath());
  605. return true;
  606. }
  607. }
  608. // determine the operating system and architecture
  609. String os_name = System.getProperty("os.name");
  610. String os_arch = System.getProperty("os.arch");
  611. if (os_name == null || os_arch == null) return false;
  612. os_name = os_name.toLowerCase();
  613. os_arch = os_arch.toLowerCase();
  614. // define settings which are OS arch architecture independent
  615. File arch_lib_path = null;
  616. String arch_libfile_suffix = null;
  617. // fill in settings for Linux on x86
  618. if (os_name.equals("linux") &&
  619. (os_arch.equals("x86") ||
  620. os_arch.equals("i386") ||
  621. os_arch.equals("i486") ||
  622. os_arch.equals("i586") ||
  623. os_arch.equals("i686"))) {
  624. arch_lib_path = new File(new File(new File("lib"), "arch"), "linux_x86");
  625. arch_libfile_suffix = ".so";
  626. }
  627. // fill in settings for Windows on x86
  628. else if (os_name.startsWith("windows ") &&
  629. (os_arch.equals("x86") ||
  630. os_arch.equals("i386") ||
  631. os_arch.equals("i486") ||
  632. os_arch.equals("i586") ||
  633. os_arch.equals("i686"))) {
  634. arch_lib_path = new File(new File(new File("lib"), "arch"), "win32_x86");
  635. arch_libfile_suffix = ".dll";
  636. }
  637. // fill in settings for Mac OS X on PPC
  638. else if (os_name.startsWith("mac os x") &&
  639. (os_arch.equals("ppc"))) {
  640. arch_lib_path = new File(new File(new File("lib"), "arch"), "darwin_ppc");
  641. arch_libfile_suffix = ".jnilib";
  642. }
  643. // fill in settings for Mac OS X on x86
  644. else if (os_name.startsWith("mac os x") &&
  645. (os_arch.equals("x86") ||
  646. os_arch.equals("i386") ||
  647. os_arch.equals("i486") ||
  648. os_arch.equals("i586") ||
  649. os_arch.equals("i686"))) {
  650. arch_lib_path = new File(new File(new File("lib"), "arch"), "darwin_x86");
  651. arch_libfile_suffix = ".jnilib";
  652. }
  653. // default to .so files with no architecture specific subdirectory
  654. else {
  655. arch_libfile_suffix = ".so";
  656. }
  657. // build the required filename
  658. final String fname = "MD5" + arch_libfile_suffix;
  659. // try the architecture specific directory
  660. if (arch_lib_path != null) {
  661. f = new File(arch_lib_path, fname);
  662. if (f.canRead()) {
  663. System.load(f.getAbsolutePath());
  664. return true;
  665. }
  666. }
  667. // try the "lib" subdirectory
  668. f = new File(new File("lib"), fname);
  669. if (f.canRead()) {
  670. System.load(f.getAbsolutePath());
  671. return true;
  672. }
  673. // try the working directory
  674. f = new File(fname);
  675. if (f.canRead()) {
  676. System.load(f.getAbsolutePath());
  677. return true;
  678. }
  679. }
  680. // discard SecurityExceptions
  681. catch (SecurityException ignored) {
  682. }
  683. // unable to load
  684. return false;
  685. }
  686. /**
  687. * Calculates and returns the hash of the contents of the given file.
  688. *
  689. * @param f
  690. * @return
  691. * @throws IOException
  692. */
  693. public static byte[] getHash(final File f) throws IOException {
  694. if (!f.exists()) throw new FileNotFoundException(f.toString());
  695. InputStream close_me = null;
  696. try {
  697. long buf_size = f.length();
  698. if (buf_size < 512) buf_size = 512;
  699. if (buf_size > 65536) buf_size = 65536;
  700. final byte[] buf = new byte[(int) buf_size];
  701. final MD5InputStream in = new MD5InputStream(new FileInputStream(f));
  702. close_me = in;
  703. while (in.read(buf) != -1) ;
  704. in.close();
  705. return in.hash();
  706. } catch (IOException e) {
  707. if (close_me != null) try {
  708. close_me.close();
  709. } catch (Exception ignored) {
  710. }
  711. throw e;
  712. }
  713. }
  714. /**
  715. * @param hash1
  716. * @param hash2
  717. * @return true iff the first 16 bytes of both hash1 and hash2 are
  718. * equal; both hash1 and hash2 are null; or either hash
  719. * array is less than 16 bytes in length and their lengths and
  720. * all of their bytes are equal.
  721. */
  722. public static boolean hashesEqual(final byte[] hash1, final byte[] hash2) {
  723. if (hash1 == null) return hash2 == null;
  724. if (hash2 == null) return false;
  725. int targ = 16;
  726. if (hash1.length < 16) {
  727. if (hash2.length != hash1.length) return false;
  728. targ = hash1.length;
  729. } else if (hash2.length < 16) {
  730. return false;
  731. }
  732. for (int i = 0; i < targ; i++) {
  733. if (hash1[i] != hash2[i]) return false;
  734. }
  735. return true;
  736. }
  737. }