?? md5.java
字號(hào):
package com.laoer.bbscs.tool;/** * Title: BBS-CS * Description: BBS-CS(BBS式虛擬社區(qū)系統(tǒng)) * Copyright: Copyright (c) 2002 * Company: loveroom.com.cn * @author laoer * @version 3.0 */import java.lang.reflect.*;/*************************************************md5 類實(shí)現(xiàn)了RSA Data Security, Inc.在提交給IETF的RFC1321中的MD5 message-digest 算法。*************************************************/public class MD5 { /* 下面這些S11-S44實(shí)際上是一個(gè)4*4的矩陣,在原始的C實(shí)現(xiàn)中是用#define 實(shí)現(xiàn)的, 這里把它們實(shí)現(xiàn)成為static final是表示了只讀,切能在同一個(gè)進(jìn)程空間內(nèi)的多個(gè) Instance間共享*/ static final int S11 = 7; static final int S12 = 12; static final int S13 = 17; static final int S14 = 22; static final int S21 = 5; static final int S22 = 9; static final int S23 = 14; static final int S24 = 20; static final int S31 = 4; static final int S32 = 11; static final int S33 = 16; static final int S34 = 23; static final int S41 = 6; static final int S42 = 10; static final int S43 = 15; static final int S44 = 21; static final byte[] PADDING = { -128, 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 }; /* 下面的三個(gè)成員是MD5計(jì)算過(guò)程中用到的3個(gè)核心數(shù)據(jù),在原始的C實(shí)現(xiàn)中 被定義到MD5_CTX結(jié)構(gòu)中 */ private long[] state = new long[4]; // state (ABCD) private long[] count = new long[2]; // number of bits, modulo 2^64 (lsb first) private byte[] buffer = new byte[64]; // input buffer /* digestHexStr是MD5的唯一一個(gè)公共成員,是最新一次計(jì)算結(jié)果的 16進(jìn)制ASCII表示. */ public String digestHexStr; /* digest,是最新一次計(jì)算結(jié)果的2進(jìn)制內(nèi)部表示,表示128bit的MD5值. */ private byte[] digest = new byte[16]; /* getMD5ofStr是類MD5最主要的公共方法,入口參數(shù)是你想要進(jìn)行MD5變換的字符串 返回的是變換完的結(jié)果,這個(gè)結(jié)果是從公共成員digestHexStr取得的. */ public String getMD5ofStr(String inbuf) { md5Init(); md5Update(inbuf.getBytes(), inbuf.length()); md5Final(); digestHexStr = ""; for (int i = 0; i < 16; i++) { digestHexStr += byteHEX(digest[i]); } return digestHexStr; } // 這是MD5這個(gè)類的標(biāo)準(zhǔn)構(gòu)造函數(shù),JavaBean要求有一個(gè)public的并且沒(méi)有參數(shù)的構(gòu)造函數(shù) public MD5() { md5Init(); return; } /* md5Init是一個(gè)初始化函數(shù),初始化核心變量,裝入標(biāo)準(zhǔn)的幻數(shù) */ private void md5Init() { count[0] = 0L; count[1] = 0L; ///* Load magic initialization constants. state[0] = 0x67452301L; state[1] = 0xefcdab89L; state[2] = 0x98badcfeL; state[3] = 0x10325476L; return; } /* F, G, H ,I 是4個(gè)基本的MD5函數(shù),在原始的MD5的C實(shí)現(xiàn)中,由于它們是 簡(jiǎn)單的位運(yùn)算,可能出于效率的考慮把它們實(shí)現(xiàn)成了宏,在java中,我們把它們 實(shí)現(xiàn)成了private方法,名字保持了原來(lái)C中的。 */ private long F(long x, long y, long z) { return (x & y) | ((~x) & z); } private long G(long x, long y, long z) { return (x & z) | (y & (~z)); } private long H(long x, long y, long z) { return x ^ y ^ z; } private long I(long x, long y, long z) { return y ^ (x | (~z)); } /* FF,GG,HH和II將調(diào)用F,G,H,I進(jìn)行近一步變換 FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4. Rotation is separate from addition to prevent recomputation. */ private long FF(long a, long b, long c, long d, long x, long s, long ac) { a += F (b, c, d) + x + ac; a = ((int) a << s) | ((int) a >>> (32 - s)); a += b; return a; } private long GG(long a, long b, long c, long d, long x, long s, long ac) { a += G (b, c, d) + x + ac; a = ((int) a << s) | ((int) a >>> (32 - s)); a += b; return a; } private long HH(long a, long b, long c, long d, long x, long s, long ac) { a += H (b, c, d) + x + ac; a = ((int) a << s) | ((int) a >>> (32 - s)); a += b; return a; } private long II(long a, long b, long c, long d, long x, long s, long ac) { a += I (b, c, d) + x + ac; a = ((int) a << s) | ((int) a >>> (32 - s)); a += b; return a; } /* md5Update是MD5的主計(jì)算過(guò)程,inbuf是要變換的字節(jié)串,inputlen是長(zhǎng)度,這個(gè) 函數(shù)由getMD5ofStr調(diào)用,調(diào)用之前需要調(diào)用md5init,因此把它設(shè)計(jì)成private的 */ private void md5Update(byte[] inbuf, int inputLen) { int i, index, partLen; byte[] block = new byte[64]; index = (int)(count[0] >>> 3) & 0x3F; // /* Update number of bits */ if ((count[0] += (inputLen << 3)) < (inputLen << 3)) count[1]++; count[1] += (inputLen >>> 29); partLen = 64 - index; // Transform as many times as possible. if (inputLen >= partLen) { md5Memcpy(buffer, inbuf, index, 0, partLen); md5Transform(buffer); for (i = partLen; i + 63 < inputLen; i += 64) { md5Memcpy(block, inbuf, 0, i, 64); md5Transform (block); } index = 0; } else i = 0; ///* Buffer remaining input */ md5Memcpy(buffer, inbuf, index, i, inputLen - i); } /* md5Final整理和填寫輸出結(jié)果 */ private void md5Final () {
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -