java版的MD5

王朝java/jsp·作者佚名  2008-05-31
窄屏简体版  字體: |||超大  

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);

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航