⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 md5.java

📁 OA典型例子
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package com.sure.util;

/**
 * Fast implementation of RSA's MD5 hash generator in Java JDK Beta-2 or higher<br>
 * Originally written by Santeri Paavolainen, Helsinki Finland 1996 <br>
 * (c) Santeri Paavolainen, Helsinki Finland 1996 <br>
 * Some changes Copyright (c) 2002 Timothy W Macinta <br>
 * <p>
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * <p>
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 * <p>
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the Free
 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 * <p>
 * See http://www.cs.hut.fi/~santtu/java/ for more information on this
 * class.
 * <p>
 * This is rather straight re-implementation of the reference implementation
 * given in RFC1321 by RSA.
 * <p>
 * Passes MD5 test suite as defined in RFC1321.
 * <p>
 * Some optimizations made by Timothy W Macinta.  Reduced time to checksum a
 * test file from 1m31.210s to 0m14.300s.  This is roughly twice as fast as
 * using java.security.MessageDigest which takes 0m28.900s.  For further
 * optimization, the Transform() and Decode() methods are natural choices
 * for re-implementation using native methods.
 * <p>
 * Some bug fixes also made by Timothy W Macinta.
 * <p>
 * Please note: I (Timothy Macinta) have put this code in the
 * com.twmacinta.util package only because it came without a package.  I
 * was not the the original author of the code, although I did
 * optimize it (substantially) and fix some bugs.
 * <p>
 *
 * This Java class has been derived from the RSA Data Security, Inc. MD5
 * Message-Digest Algorithm and its reference implementation.
 *
 * @author	Santeri Paavolainen <sjpaavol@cc.helsinki.fi>
 * @author	Timothy W Macinta (twm@alum.mit.edu) (optimizations and bug fixes)
 */


public class MD5 {
  /**
   * MD5 state
   */
  MD5State	state;

  /**
   * If Final() has been called, finals is set to the current finals
   * state. Any Update() causes this to be set to null.
   */
  MD5State 	finals;

  /**
   * Padding for Final()
   */
  static byte	padding[] = {
    (byte) 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  };

  /**
   * Initialize MD5 internal state (object can be reused just by
   * calling Init() after every Final()
   */
  public synchronized void Init () {
    state = new MD5State();
    finals = null;
  }

  /**
   * Class constructor
   */
  public MD5 () {
    this.Init();
  }

  /**
   * Initialize class, and update hash with ob.toString()
   *
   * @param	ob	Object, ob.toString() is used to update hash
   *			after initialization
   */
  public MD5 (Object ob) {
    this();
    Update(ob.toString());
  }

  private void Decode (byte buffer[], int shift, int[] out) {
    /*len += shift;
    for (int i = 0; shift < len; i++, shift += 4) {
      out[i] = ((int) (buffer[shift] & 0xff)) |
	(((int) (buffer[shift + 1] & 0xff)) << 8) |
	(((int) (buffer[shift + 2] & 0xff)) << 16) |
	(((int)  buffer[shift + 3]) << 24);
    }*/

    // unrolled loop (original loop shown above)

    out[0] = ((int) (buffer[shift] & 0xff)) |
      (((int) (buffer[shift + 1] & 0xff)) << 8) |
      (((int) (buffer[shift + 2] & 0xff)) << 16) |
      (((int)  buffer[shift + 3]) << 24);
    out[1] = ((int) (buffer[shift + 4] & 0xff)) |
      (((int) (buffer[shift + 5] & 0xff)) << 8) |
      (((int) (buffer[shift + 6] & 0xff)) << 16) |
      (((int)  buffer[shift + 7]) << 24);
    out[2] = ((int) (buffer[shift + 8] & 0xff)) |
      (((int) (buffer[shift + 9] & 0xff)) << 8) |
      (((int) (buffer[shift + 10] & 0xff)) << 16) |
      (((int)  buffer[shift + 11]) << 24);
    out[3] = ((int) (buffer[shift + 12] & 0xff)) |
      (((int) (buffer[shift + 13] & 0xff)) << 8) |
      (((int) (buffer[shift + 14] & 0xff)) << 16) |
      (((int)  buffer[shift + 15]) << 24);
    out[4] = ((int) (buffer[shift + 16] & 0xff)) |
      (((int) (buffer[shift + 17] & 0xff)) << 8) |
      (((int) (buffer[shift + 18] & 0xff)) << 16) |
      (((int)  buffer[shift + 19]) << 24);
    out[5] = ((int) (buffer[shift + 20] & 0xff)) |
      (((int) (buffer[shift + 21] & 0xff)) << 8) |
      (((int) (buffer[shift + 22] & 0xff)) << 16) |
      (((int)  buffer[shift + 23]) << 24);
    out[6] = ((int) (buffer[shift + 24] & 0xff)) |
      (((int) (buffer[shift + 25] & 0xff)) << 8) |
      (((int) (buffer[shift + 26] & 0xff)) << 16) |
      (((int)  buffer[shift + 27]) << 24);
    out[7] = ((int) (buffer[shift + 28] & 0xff)) |
      (((int) (buffer[shift + 29] & 0xff)) << 8) |
      (((int) (buffer[shift + 30] & 0xff)) << 16) |
      (((int)  buffer[shift + 31]) << 24);
    out[8] = ((int) (buffer[shift + 32] & 0xff)) |
      (((int) (buffer[shift + 33] & 0xff)) << 8) |
      (((int) (buffer[shift + 34] & 0xff)) << 16) |
      (((int)  buffer[shift + 35]) << 24);
    out[9] = ((int) (buffer[shift + 36] & 0xff)) |
      (((int) (buffer[shift + 37] & 0xff)) << 8) |
      (((int) (buffer[shift + 38] & 0xff)) << 16) |
      (((int)  buffer[shift + 39]) << 24);
    out[10] = ((int) (buffer[shift + 40] & 0xff)) |
      (((int) (buffer[shift + 41] & 0xff)) << 8) |
      (((int) (buffer[shift + 42] & 0xff)) << 16) |
      (((int)  buffer[shift + 43]) << 24);
    out[11] = ((int) (buffer[shift + 44] & 0xff)) |
      (((int) (buffer[shift + 45] & 0xff)) << 8) |
      (((int) (buffer[shift + 46] & 0xff)) << 16) |
      (((int)  buffer[shift + 47]) << 24);
    out[12] = ((int) (buffer[shift + 48] & 0xff)) |
      (((int) (buffer[shift + 49] & 0xff)) << 8) |
      (((int) (buffer[shift + 50] & 0xff)) << 16) |
      (((int)  buffer[shift + 51]) << 24);
    out[13] = ((int) (buffer[shift + 52] & 0xff)) |
      (((int) (buffer[shift + 53] & 0xff)) << 8) |
      (((int) (buffer[shift + 54] & 0xff)) << 16) |
      (((int)  buffer[shift + 55]) << 24);
    out[14] = ((int) (buffer[shift + 56] & 0xff)) |
      (((int) (buffer[shift + 57] & 0xff)) << 8) |
      (((int) (buffer[shift + 58] & 0xff)) << 16) |
      (((int)  buffer[shift + 59]) << 24);
    out[15] = ((int) (buffer[shift + 60] & 0xff)) |
      (((int) (buffer[shift + 61] & 0xff)) << 8) |
      (((int) (buffer[shift + 62] & 0xff)) << 16) |
      (((int)  buffer[shift + 63]) << 24);
  }

  private void Transform (MD5State state, byte buffer[], int shift, int[] decode_buf) {
    int
      a = state.state[0],
      b = state.state[1],
      c = state.state[2],
      d = state.state[3],
      x[] = decode_buf;

    Decode(buffer, shift, decode_buf);

    /* Round 1 */
    a += ((b & c) | (~b & d)) + x[ 0] + 0xd76aa478; /* 1 */
    a = ((a << 7) | (a >>> 25)) + b;
    d += ((a & b) | (~a & c)) + x[ 1] + 0xe8c7b756; /* 2 */
    d = ((d << 12) | (d >>> 20)) + a;
    c += ((d & a) | (~d & b)) + x[ 2] + 0x242070db; /* 3 */
    c = ((c << 17) | (c >>> 15)) + d;
    b += ((c & d) | (~c & a)) + x[ 3] + 0xc1bdceee; /* 4 */
    b = ((b << 22) | (b >>> 10)) + c;

    a += ((b & c) | (~b & d)) + x[ 4] + 0xf57c0faf; /* 5 */
    a = ((a << 7) | (a >>> 25)) + b;
    d += ((a & b) | (~a & c)) + x[ 5] + 0x4787c62a; /* 6 */
    d = ((d << 12) | (d >>> 20)) + a;
    c += ((d & a) | (~d & b)) + x[ 6] + 0xa8304613; /* 7 */
    c = ((c << 17) | (c >>> 15)) + d;
    b += ((c & d) | (~c & a)) + x[ 7] + 0xfd469501; /* 8 */
    b = ((b << 22) | (b >>> 10)) + c;

    a += ((b & c) | (~b & d)) + x[ 8] + 0x698098d8; /* 9 */
    a = ((a << 7) | (a >>> 25)) + b;
    d += ((a & b) | (~a & c)) + x[ 9] + 0x8b44f7af; /* 10 */
    d = ((d << 12) | (d >>> 20)) + a;
    c += ((d & a) | (~d & b)) + x[10] + 0xffff5bb1; /* 11 */
    c = ((c << 17) | (c >>> 15)) + d;
    b += ((c & d) | (~c & a)) + x[11] + 0x895cd7be; /* 12 */
    b = ((b << 22) | (b >>> 10)) + c;

    a += ((b & c) | (~b & d)) + x[12] + 0x6b901122; /* 13 */
    a = ((a << 7) | (a >>> 25)) + b;
    d += ((a & b) | (~a & c)) + x[13] + 0xfd987193; /* 14 */
    d = ((d << 12) | (d >>> 20)) + a;
    c += ((d & a) | (~d & b)) + x[14] + 0xa679438e; /* 15 */
    c = ((c << 17) | (c >>> 15)) + d;
    b += ((c & d) | (~c & a)) + x[15] + 0x49b40821; /* 16 */
    b = ((b << 22) | (b >>> 10)) + c;


    /* Round 2 */
    a += ((b & d) | (c & ~d)) + x[ 1] + 0xf61e2562; /* 17 */
    a = ((a << 5) | (a >>> 27)) + b;
    d += ((a & c) | (b & ~c)) + x[ 6] + 0xc040b340; /* 18 */
    d = ((d << 9) | (d >>> 23)) + a;
    c += ((d & b) | (a & ~b)) + x[11] + 0x265e5a51; /* 19 */
    c = ((c << 14) | (c >>> 18)) + d;
    b += ((c & a) | (d & ~a)) + x[ 0] + 0xe9b6c7aa; /* 20 */
    b = ((b << 20) | (b >>> 12)) + c;

    a += ((b & d) | (c & ~d)) + x[ 5] + 0xd62f105d; /* 21 */
    a = ((a << 5) | (a >>> 27)) + b;
    d += ((a & c) | (b & ~c)) + x[10] + 0x02441453; /* 22 */
    d = ((d << 9) | (d >>> 23)) + a;
    c += ((d & b) | (a & ~b)) + x[15] + 0xd8a1e681; /* 23 */
    c = ((c << 14) | (c >>> 18)) + d;
    b += ((c & a) | (d & ~a)) + x[ 4] + 0xe7d3fbc8; /* 24 */
    b = ((b << 20) | (b >>> 12)) + c;

    a += ((b & d) | (c & ~d)) + x[ 9] + 0x21e1cde6; /* 25 */
    a = ((a << 5) | (a >>> 27)) + b;
    d += ((a & c) | (b & ~c)) + x[14] + 0xc33707d6; /* 26 */
    d = ((d << 9) | (d >>> 23)) + a;
    c += ((d & b) | (a & ~b)) + x[ 3] + 0xf4d50d87; /* 27 */
    c = ((c << 14) | (c >>> 18)) + d;
    b += ((c & a) | (d & ~a)) + x[ 8] + 0x455a14ed; /* 28 */
    b = ((b << 20) | (b >>> 12)) + c;

    a += ((b & d) | (c & ~d)) + x[13] + 0xa9e3e905; /* 29 */
    a = ((a << 5) | (a >>> 27)) + b;
    d += ((a & c) | (b & ~c)) + x[ 2] + 0xfcefa3f8; /* 30 */
    d = ((d << 9) | (d >>> 23)) + a;
    c += ((d & b) | (a & ~b)) + x[ 7] + 0x676f02d9; /* 31 */
    c = ((c << 14) | (c >>> 18)) + d;
    b += ((c & a) | (d & ~a)) + x[12] + 0x8d2a4c8a; /* 32 */
    b = ((b << 20) | (b >>> 12)) + c;


    /* Round 3 */
    a += (b ^ c ^ d) + x[ 5] + 0xfffa3942;      /* 33 */
    a = ((a << 4) | (a >>> 28)) + b;
    d += (a ^ b ^ c) + x[ 8] + 0x8771f681;      /* 34 */
    d = ((d << 11) | (d >>> 21)) + a;
    c += (d ^ a ^ b) + x[11] + 0x6d9d6122;      /* 35 */
    c = ((c << 16) | (c >>> 16)) + d;
    b += (c ^ d ^ a) + x[14] + 0xfde5380c;      /* 36 */
    b = ((b << 23) | (b >>> 9)) + c;

    a += (b ^ c ^ d) + x[ 1] + 0xa4beea44;      /* 37 */
    a = ((a << 4) | (a >>> 28)) + b;
    d += (a ^ b ^ c) + x[ 4] + 0x4bdecfa9;      /* 38 */
    d = ((d << 11) | (d >>> 21)) + a;
    c += (d ^ a ^ b) + x[ 7] + 0xf6bb4b60;      /* 39 */
    c = ((c << 16) | (c >>> 16)) + d;
    b += (c ^ d ^ a) + x[10] + 0xbebfbc70;      /* 40 */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -