可變在這裡含義很簡單
Java代碼
package **
import java
/**
* 標准MD
* @author Sarin
*/
public class MD
/**
* 獲得MD
*/
public static String getMD
String origMD
try {
MessageDigest md
byte[] result = md
origMD
} catch (Exception e) {
e
}
return origMD
}
/**
* 處理字節數組得到MD
*/
private static String byteArray
StringBuffer sb = new StringBuffer();
for (byte b : bs) {
sb
}
return sb
}
/**
* 字節標准移位轉十六進制方法
*/
private static String byte
String hexStr = null;
int n = b;
if (n <
//若需要自定義加密
n = b &
}
hexStr = Integer
return hexStr
}
/**
* 提供一個MD
*/
public static String getMD
String md
for (int i =
md
}
return getMD
}
/**
* 密碼驗證方法
*/
public static boolean verifyPassword(String inputStr
return getMD
}
/**
* 重載一個多次加密時的密碼驗證方法
*/
public static boolean verifyPassword(String inputStr
return getMD
}
/**
* 提供一個測試的主函數
*/
public static void main(String[] args) {
System
System
System
System
}
}
可以看出實現的過程非常簡單
MD
為了更可變
這樣在MIS系統中使用
我們最後看看由MD
Java代碼
import java
/**
* **********************************************
* md
* 的RFC
* ***********************************************
*/
public class MD
/* 下面這些S
這裡把它們實現成為static final是表示了只讀
Instance間共享*/
static final int S
static final int S
static final int S
static final int S
static final int S
static final int S
static final int S
static final int S
static final int S
static final int S
static final int S
static final int S
static final int S
static final int S
static final int S
static final int S
static final byte[] PADDING = {
/* 下面的三個成員是MD
被定義到MD
*/
private long[] state = new long[
private long[] count = new long[
private byte[] buffer = new byte[
/* digestHexStr是MD
*/
public String digestHexStr;
/* digest
*/
private byte[] digest = new byte[
/*
getMD
返回的是變換完的結果
*/
public String getMD
md
md
md
digestHexStr =
for (int i =
digestHexStr += byteHEX(digest[i]);
}
return digestHexStr;
}
// 這是MD
public MD
md
return;
}
/* md
private void md
count[
count[
///* Load magic initialization constants
state[
state[
state[
state[
return;
}
/* F
簡單的位運算
實現成了private方法
private long F(long x
return (x & y) | ((~x) & z);
}
private long G(long x
return (x & z) | (y & (~z));
}
private long H(long x
return x ^ y ^ z;
}
private long I(long x
return y ^ (x | (~z));
}
/*
FF
FF
Rotation is separate from addition to prevent recomputation
*/
private long FF(long a
a += F(b
a = ((int) a << s) | ((int) a >>> (
a += b;
return a;
}
private long GG(long a
a += G(b
a = ((int) a << s) | ((int) a >>> (
a += b;
return a;
}
private long HH(long a
a += H(b
a = ((int) a << s) | ((int) a >>> (
a += b;
return a;
}
private long II(long a
a += I(b
a = ((int) a << s) | ((int) a >>> (
a += b;
return a;
}
/*
md
函數由getMD
*/
private void md
int i
byte[] block = new byte[
index = (int) (count[
// /* Update number of bits */
if ((count[
count[
count[
partLen =
// Transform as many times as possible
if (inputLen >= partLen) {
md
md
for (i = partLen; i +
md
md
}
index =
} else
i =
///* Buffer remaining input */
md
}
/*
md
*/
private void md
byte[] bits = new byte[
int index
///* Save number of bits */
Encode(bits
///* Pad out to
index = (int) (count[
padLen = (index <
md
///* Append length (before padding) */
md
///* Store state in digest */
Encode(digest
}
/* md
字節拷貝到output的outpos位置開始
*/
private void md
int i;
for (i =
output[outpos + i] = input[inpos + i];
}
/*
md
*/
private void md
long a = state[
long[] x = new long[
Decode(x
/* Round
a = FF(a
d = FF(d
c = FF(c
b = FF(b
a = FF(a
d = FF(d
c = FF(c
b = FF(b
a = FF(a
d = FF(d
c = FF(c
b = FF(b
a = FF(a
d = FF(d
c = FF(c
b = FF(b
/* Round
a = GG(a
d = GG(d
c = GG(c
b = GG(b
a = GG(a
d = GG(d
c = GG(c
b = GG(b
a = GG(a
d = GG(d
c = GG(c
b = GG(b
a = GG(a
d = GG(d
c = GG(c
b = GG(b
/* Round
a = HH(a
d = HH(d
c = HH(c
b = HH(b
a = HH(a
d = HH(d
c = HH(c
b = HH(b
a = HH(a
d = HH(d
c = HH(c
b = HH(b
a = HH(a
d = HH(d
c = HH(c
b = HH(b
/* Round
a = II(a
d = II(d
c = II(c
b = II(b
a = II(a
d = II(d
c = II(c
b = II(b
a = II(a
d = II(d
c = II(c
b = II(b
a = II(a
d = II(d
c = II(c
b = II(b
state[
state[
state[
state[
}
/*Encode把long數組按順序拆成byte數組
只拆低
*/
private void Encode(byte[] output
int i
for (i =
output[j] = (byte) (input[i] &
output[j +
output[j +
output[j +
}
}
/*Decode把byte數組按順序合成成long數組
只合成低
*/
private void Decode(long[] output
int i
for (i =
output[i] = b
| (b
return;
}
/*
b
*/
public static long b
return b <
}
/*byteHEX()
因為java中的byte的toString無法實現這一點
sprintf(outbuf
*/
public static String byteHEX(byte ib) {
char[] Digit = {
char[] ob = new char[
ob[
ob[
String s = new String(ob);
return s;
}
public static void main(String args[]) {
MD
if (Array
System
System
System
System
System
System
System
System
System
+ m
} else
System
}
}
From:http://tw.wingwit.com/Article/program/Java/hx/201311/26604.html