WEB开发网
开发学院WEB开发Jsp java版的MD5 阅读

java版的MD5

 2008-01-05 09:07:35 来源:WEB开发网   
核心提示:public class md5{ /* * A java implementation of the RSA Data Security, Inc. MD5 Message * Digest Algorithm, as defined in RFC 1321. * Based on the ja

  public class md5
{
/*
* A java implementation of the RSA Data Security, Inc. MD5 Message
* Digest Algorithm, as defined in RFC 1321.
* Based on the javascript implementation of Paul Johnston
* Copyright (C) Paul Johnston 1999 - 2000.
* See http://pajhome.org.uk/site/legal.Html for details.
* Java Version by Thomas Weber (Orange Interactive GmbH)
*/

/*
* Convert a 32-bit number to a hex string with ls-byte first
*/
String hex_chr = "0123456789abcdef";
PRivate String rhex(int num)
{
String str = "";
for(int j = 0; j <= 3; j++)
str = str + hex_chr.charAt((num >> (j * 8 + 4)) & 0x0F) + hex_chr.charAt((num >> (j * 8)) & 0x0F);
return str;
}

/*
* Convert a string to a sequence of 16-Word blocks, stored as an array.
* Append padding bits and the length, as described in the MD5 standard.
*/
private int[] str2blks_MD5(String str)
{
int nblk = ((str.length() + 8) >> 6) + 1;
int[] blks = new int[nblk * 16];
int i = 0;
for(i = 0; i < nblk * 16; i++) {
blks[i] = 0;
}
for(i = 0; i < str.length(); i++) {
blks[i >> 2] = str.charAt(i) << ((i % 4) * 8);
}
blks[i >> 2] = 0x80 << ((i % 4) * 8);
blks[nblk * 16 - 2] = str.length()*8;

return blks;
}

/*
* Add integers, wrapping at 2^32
*/
private int add(int x, int y)
{
return ((x&0x7FFFFFFF) + (y&0x7FFFFFFF)) ^ (x&0x80000000) ^ (y&0x80000000);
}

/*
* Bitwise rotate a 32-bit number to the left
*/
private int rol(int num, int cnt)
{
return (num << cnt) (num >>> (32 - cnt));
}

/*
* These functions implement the basic Operation for each round of the
* algorithm.
*/
private int cmn(int q, int a, int b, int x, int s, int t)
{
return add(rol(add(add(a, q), add(x, t)), s), b);
}
private int ff(int a, int b, int c, int d, int x, int s, int t)
{
return cmn((b & c) ((~b) & d), a, b, x, s, t);
}
private int gg(int a, int b, int c, int d, int x, int s, int t)
{
return cmn((b & d) (c & (~d)), a, b, x, s, t);

Tags:java MD

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接