PageRenderTime 66ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/factory/factory-service-voip-conferences/src/main/java/com/twmacinta/util/MD5.java

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