public class MD
{
/*
* A Java implementation of the RSA Data Security
* Digest Algorithm
* Based on the JavaScript implementation of Paul Johnston
* Copyright (C) Paul Johnston
* See for details
* Java Version by Thomas Weber (Orange Interactive GmbH)
*/
/*
* Convert a
*/
String hex_chr =
private String rhex(int num)
{
String str =
for(int j =
str = str + hex_chr.charAt((num >> (j *
return str;
}
/*
* Convert a string to a sequence of
* Append padding bits and the length
*/
private int[] str
{
int nblk = ((str
int[] blks = new int[nblk *
int i =
for(i =
blks[i] =
}
for(i =
blks[i >>
}
blks[i >>
blks[nblk *
return blks;
}
/*
* Add integers
*/
private int add(int x
{
return ((x&
}
/*
* Bitwise rotate a
*/
private int rol(int num
{
return (num << cnt) | (num >>> (
}
/*
* These functions implement the basic operation for each round of the
* algorithm
*/
private int cmn(int q
{
return add(rol(add(add(a
}
private int ff(int a
{
return cmn((b & c) | ((~b) & d)
}
private int gg(int a
{
return cmn((b & d) | (c & (~d))
}
private int hh(int a
{
return cmn(b ^ c ^ d
}
private int ii(int a
{
return cmn(c ^ (b | (~d))
}
/*
* Take a string and return the hex representation of its MD
*/
public String calcMD
{
int[] x = str
int a =
int b =
int c =
int d =
for(int i =
{
int olda = a;
int oldb = b;
int oldc = c;
int oldd = d;
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
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
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
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
a = add(a
b = add(b
c = add(c
d = add(d
}
return rhex(a) + rhex(b) + rhex(c) + rhex(d);
}
}
另: 一中國人寫的md
/************************************************
MD
@author:Topcat Tuppin
Last Modified:
*************************************************/
//package beartool;
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
From:http://tw.wingwit.com/Article/program/Java/hx/201311/26958.html