PageRenderTime 56ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 1ms

/audiobox.fm-sync/src/main/java/com/twmacinta/util/MD5.java

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