PageRenderTime 57ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Utilities/MD5.java

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