The
looked up online and collected two examples of Java and JavaScript used in the Web, tested and shared.
1, Java:
Package org.bearfly.test.md5;
Import java.io.UnsupportedEncodingException;
Import Java.security.MessageDigest;
Import java.security.NoSuchAlgorithmException;
public class Md5utils {public static void main (string[] args) {System.out.println (Getmd5string (""));
System.out.println (MD5 ("good")); } public final static string MD5 (string s) {char hexdigits[] = {' 0 ', ' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', '
9 ', ' A ', ' B ', ' C ', ' D ', ' E ', ' F '};
try {byte[] btinput = S.getbytes ();
Obtain the MessageDigest object messagedigest mdinst = messagedigest.getinstance ("MD5") of MD5 Digest algorithm;
Use the specified Byte update digest mdinst.update (Btinput);
Get ciphertext byte[] MD = Mdinst.digest ();
Converts cipher text into a 16-binary string form int j = md.length;
Char str[] = new CHAR[J * 2];
int k = 0;
for (int i = 0; i < J; i++) {byte byte0 = md[i];
str[k++] = hexdigits[byte0 >>> 4 & 0xf];
str[k++] = hexdigits[byte0 & 0xf];
return new String (str);
catch (Exception e) {e.printstacktrace (); Return null;
} public static string getmd5string (String str) {messagedigest messagedigest = null;
try {messagedigest = messagedigest.getinstance ("MD5");
Messagedigest.reset ();
Messagedigest.update (Str.getbytes ("UTF-8"));
catch (NoSuchAlgorithmException e) {System.out.println ("nosuchalgorithmexception caught!");
System.exit (-1);
catch (Unsupportedencodingexception e) {e.printstacktrace ();
} byte[] ByteArray = Messagedigest.digest ();
StringBuffer Md5strbuff = new StringBuffer (); for (int i = 0; i < bytearray.length i++) {if (Integer.tohexstring (0xFF & Bytearray[i)). Length () = 1) MD5
Strbuff.append ("0"). Append (integer.tohexstring (0xFF & Bytearray[i));
Else Md5strbuff.append (integer.tohexstring (0xFF & Bytearray[i]));
return md5strbuff.tostring (); }
}
2, JavaScript:
md5.js
var hexcase = 0; /* Hex output format. 0-lowercase; 1-uppercase * * var b64pad = ""; /* base-64 pad character. "=" for strict RFC compliance */var Chrsz = 8; /* bits per input character. 8-ascii; 16-unicode * * * These are the functions "ll usually want to call * They take string arguments and return eithe R hex or BASE-64 encoded strings */function Hex_md5 (s) {return Binl2hex (CORE_MD5 (s), STR2BINL * s.length)); Chrsz Ion B64_md5 (s) {return binl2b64 (CORE_MD5 (Str2binl (s), s.length * chrsz))} function Str_md5 (s) {return binl2str (core_md5
(Str2binl (s), s.length * Chrsz));} function Hex_hmac_md5 (key, data) {return Binl2hex (CORE_HMAC_MD5 (key, data);} function B64_hmac_md5 (key, data) {return Binl2b64 (CORE_HMAC_MD5 (key, data));
function Str_hmac_md5 (key, data) {return binl2str (CORE_HMAC_MD5 (key, data);} * * Perform a simple self-test to, if the VM is working/function md5_vm_test () {return HEX_MD5 ("abc") = "90015
0983cd24fb0d6963f7d28e17f72 ";
}* * Calculate The MD5 of an array of Little-endian words, and a bit length/function core_md5 (x, len) {/* Append PA
dding * * X[len >> 5] |= 0x80 << ((len)% 32);
X[((len +) >>> 9) << 4) + [] = Len;
var a = 1732584193;
var b =-271733879;
var c =-1732584194;
var d = 271733878;
for (var i = 0; i < x.length i + =) {var Olda = A;
var oldb = b;
var oldc = c;
var oldd = D;
A = Md5_ff (A, B, C, D, x[i+ 0], 7,-680876936);
D = md5_ff (d, a, B, C, x[i+ 1], 12,-389564586);
c = md5_ff (c, D, a, B, x[i+ 2], 17, 606105819);
b = md5_ff (b, C, D, A, x[i+ 3], 22,-1044525330);
A = Md5_ff (A, B, C, D, x[i+ 4], 7,-176418897);
D = md5_ff (d, a, B, C, x[i+ 5], 12, 1200080426);
c = md5_ff (c, D, a, B, x[i+ 6], 17,-1473231341);
b = md5_ff (b, C, D, A, x[i+ 7], 22,-45705983);
A = Md5_ff (A, B, C, D, x[i+ 8], 7, 1770035416);
D = md5_ff (d, a, B, C, x[i+ 9], 12,-1958414417);
c = md5_ff (c, D, a, B, x[i+10], 17,-42063); b = md5_ff (b, C, D, A, x[i+11], 22,-1990404162);
A = Md5_ff (A, B, C, D, X[i+12], 7, 1804603682);
D = md5_ff (d, a, B, C, x[i+13], 12,-40341101);
c = md5_ff (c, D, a, B, x[i+14], 17,-1502002290);
b = md5_ff (b, C, D, A, x[i+15], 22, 1236535329);
A = Md5_gg (A, B, C, D, x[i+ 1], 5,-165796510);
D = Md5_gg (d, a, B, C, x[i+ 6], 9,-1069501632);
c = Md5_gg (c, D, a, B, x[i+11], 14, 643717713);
b = Md5_gg (b, C, D, A, x[i+ 0], 20,-373897302);
A = Md5_gg (A, B, C, D, x[i+ 5], 5,-701558691);
D = Md5_gg (d, a, B, C, x[i+10], 9, 38016083);
c = Md5_gg (c, D, a, B, x[i+15], 14,-660478335);
b = Md5_gg (b, C, D, A, x[i+ 4], 20,-405537848);
A = Md5_gg (A, B, C, D, x[i+ 9], 5, 568446438);
D = Md5_gg (d, a, B, C, x[i+14], 9,-1019803690);
c = Md5_gg (c, D, a, B, x[i+ 3], 14,-187363961);
b = Md5_gg (b, C, D, A, x[i+ 8], 20, 1163531501);
A = Md5_gg (A, B, C, D, X[i+13], 5,-1444681467);
D = Md5_gg (d, a, B, C, x[i+ 2], 9,-51403784);
c = Md5_gg (c, D, a, B, x[i+ 7], 14, 1735328473); b = Md5_gg (b, C, D, A, x[i+12], 20,-1926607734);
A = Md5_hh (A, B, C, D, x[i+ 5], 4,-378558);
D = md5_hh (d, a, B, C, x[i+ 8], 11,-2022574463);
c = md5_hh (c, D, a, B, x[i+11], 16, 1839030562);
b = md5_hh (b, C, D, A, x[i+14], 23,-35309556);
A = Md5_hh (A, B, C, D, x[i+ 1], 4,-1530992060);
D = md5_hh (d, a, B, C, x[i+ 4], 11, 1272893353);
c = md5_hh (c, D, a, B, x[i+ 7], 16,-155497632);
b = md5_hh (b, C, D, A, x[i+10], 23,-1094730640);
A = Md5_hh (A, B, C, D, X[i+13], 4, 681279174);
D = md5_hh (d, a, B, C, x[i+ 0], 11,-358537222);
c = md5_hh (c, D, a, B, x[i+ 3], 16,-722521979);
b = md5_hh (b, C, D, A, x[i+ 6], 23, 76029189);
A = Md5_hh (A, B, C, D, x[i+ 9], 4,-640364487);
D = md5_hh (d, a, B, C, x[i+12], 11,-421815835);
c = md5_hh (c, D, a, B, x[i+15], 16, 530742520);
b = md5_hh (b, C, D, A, x[i+ 2], 23,-995338651);
A = Md5_ii (A, B, C, D, x[i+ 0], 6,-198630844);
D = Md5_ii (d, a, B, C, x[i+ 7], 10, 1126891415);
c = Md5_ii (c, D, a, B, x[i+14], 15,-1416354905); b = Md5_ii (b, C,D, A, x[i+ 5], 21,-57434055);
A = Md5_ii (A, B, C, D, X[i+12], 6, 1700485571);
D = Md5_ii (d, a, B, C, x[i+ 3], 10,-1894986606);
c = Md5_ii (c, D, a, B, x[i+10], 15,-1051523);
b = Md5_ii (b, C, D, A, x[i+ 1], 21,-2054922799);
A = Md5_ii (A, B, C, D, x[i+ 8], 6, 1873313359);
D = Md5_ii (d, a, B, C, x[i+15], 10,-30611744);
c = Md5_ii (c, D, a, B, x[i+ 6], 15,-1560198380);
b = Md5_ii (b, C, D, A, x[i+13], 21, 1309151649);
A = Md5_ii (A, B, C, D, x[i+ 4], 6,-145523070);
D = Md5_ii (d, a, B, C, x[i+11], 10,-1120210379);
c = Md5_ii (c, D, a, B, x[i+ 2], 15, 718787259);
b = Md5_ii (b, C, D, A, x[i+ 9], 21,-343485551);
A = Safe_add (A, Olda);
b = Safe_add (b, oldb);
c = Safe_add (c, OLDC);
D = Safe_add (d, OLDD);
Return Array (A, B, C, D);
/* * These functions implement the four basic operations the algorithm uses. /function Md5_cmn (q, A, B, X, S, t) {return safe_add (Bit_rol (Safe_add (Safe_add (A, Q), Safe_add (x, T), s), b); functi
On Md5_ff (A, B, C, D, X, S, t) {Return Md5_cmn (b & c) |
((~b) & D), A, B, X, S, T); function Md5_gg (A, B, C, D, X, S, t) {return MD5_CMN (b & D) |
(C & (~d)), A, B, X, S, T);
function Md5_hh (A, B, C, D, X, S, t) {return md5_cmn (b ^ C ^ D, a, B, X, S, t);} function Md5_ii (A, B, C, D, X, S, t) {return md5_cmn (c ^) (b |
(~d)), A, B, X, S, T); } * * Calculate the HMAC-MD5, a key and some data/function core_hmac_md5 (key, data) {var bkey = Str2binl (key)
;
if (Bkey.length >) bkey = Core_md5 (Bkey, key.length * chrsz);
var ipad = array, Opad = Array (16);
for (var i = 0; i < i++) {Ipad[i] = bkey[i] ^ 0x36363636;
Opad[i] = bkey[i] ^ 0x5c5c5c5c;
var hash = core_md5 (str2binl (data), Ipad.concat + data.length * chrsz);
Return Core_md5 (Opad.concat (hash), 512 + 128); * * * Add integers, wrapping at 2^32.
This is uses 16-bit operations internally * To work around bugs in some JS interpreters. */function Safe_add (x, y) {var LSW = (x & 0xFFFF) + (y;
0xFFFF);
var MSW = (x >>) + (y >>) + (LSW >> 16); Return (MSW << 16) |
(LSW & 0xFFFF);
/* * Bitwise rotate a 32-bit number to the left. */function Bit_rol (num, cnt) {return (num << cnt) |
(Num >>> (32-cnt)); }/* Convert A string to an array of Little-endian words * If Chrsz is ASCII, characters >255 have their hi-byte
Silently ignored.
*/function Str2binl (str) {var bin = Array ();
var mask = (1 << chrsz)-1; for (var i = 0; i < str.length * Chrsz i + = Chrsz) bin[i>>5] |= (Str.charcodeat (I/chrsz) & mask) <<
(I%32);
return bin;
}/* Convert an array of Little-endian words to a string/function binl2str (BIN) {var str = "";
var mask = (1 << chrsz)-1; for (var i = 0; i < bin.length * i + = chrsz) str + string.fromcharcode ((bin[i>>5) >>> (i%)) &am P
mask);
return str;
/* * Convert an array of Little-endian words to a hex string.
*/function Binl2hex (BinArray) {var hex_tab = hexcase?
"0123456789ABCDEF": "0123456789abcdef";
var str = ""; for (var i = 0; i < binarray.length * 4; i++) {str + hex_tab.charat ((binarray[i>>2) >> ((i%4) *8+4)) ;
0xF) + Hex_tab.charat ((Binarray[i>>2] >> ((i%4) *8)) & 0xF);
return str; } * * Convert an array of Little-endian words to a base-64 string/function binl2b64 (binarray) {var tab = ABCDEF
ghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789+/";
var str = ""; for (var i = 0; i < binarray.length * 4; i + + 3) {var triplet = ((binarray[i >> 2] >> 8 * (i%4) & 0xFF) << 16) | (((binarray[i+1 >> 2] >> 8 * ((i+1)%4) & 0xFF) << 8) |
((binarray[i+2 >> 2] >> 8 * ((i+2)%4)) & 0xFF);
for (var j = 0; J < 4; J +) {if (I * 8 + J * 6 > Binarray.length *) str + = B64pad;
else str = Tab.charat ((triplet >> 6* (3-j)) & 0x3F);
} return str; }
Md5.html
add:JavaScript MD5 code Backup, with Java interoperability
var MD5 = function (string) {function Rotateleft (LValue, ishiftbits) {return (lvalue<<ishiftbits) |
(Lvalue>>> (32-ishiftbits));
function addunsigned (lx,ly) {var lx4,ly4,lx8,ly8,lresult;
LX8 = (LX & 0x80000000);
LY8 = (LY & 0x80000000);
LX4 = (LX & 0x40000000);
LY4 = (LY & 0x40000000);
LRESULT = (LX & 0x3fffffff) + (LY & 0X3FFFFFFF);
if (LX4 & lY4) {return (lresult ^ 0x80000000 ^ lX8 ^ lY8);
} if (LX4 | lY4) {if (Lresult & 0x40000000) {return (lresult ^ 0xc0000000 ^ lX8 ^ lY8);
else {return (lresult ^ 0x40000000 ^ lX8 ^ lY8);
} else {return (lresult ^ lX8 ^ lY8); } function F (x,y,z) {return (x & y) | ((~x) & Z); } function G (x,y,z) {return (X & Z) | (Y & (~z));
function H (x,y,z) {return (x ^ y ^ z);} function I (x,y,z) {return (y ^ (x | (~z))); } function FF (A,B,C,D,X,S,AC) {a = Addunsigned (A, addunsigned (AddunsigneD (F (b, C, D), x), AC);
Return addunsigned (Rotateleft (A, s), b);
};
function GG (A,B,C,D,X,S,AC) {a = Addunsigned (A, addunsigned (addunsigned (G (b, C, D), x), AC));
Return addunsigned (Rotateleft (A, s), b);
};
function HH (A,B,C,D,X,S,AC) {a = Addunsigned (A, addunsigned (addunsigned (H (b, C, D), x), AC));
Return addunsigned (Rotateleft (A, s), b);
};
function II (A,B,C,D,X,S,AC) {a = Addunsigned (A, addunsigned (addunsigned (I (b, C, D), x), AC));
Return addunsigned (Rotateleft (A, s), b);
};
function Converttowordarray (string) {var lwordcount;
var lmessagelength = string.length;
var lnumberofwords_temp1=lmessagelength + 8;
var lnumberofwords_temp2= (lnumberofwords_temp1-(lnumberofwords_temp1% 64))/64;
var lnumberofwords = (lnumberofwords_temp2+1) *16;
var lwordarray=array (lNumberOfWords-1);
var lbyteposition = 0;
var lbytecount = 0;
while (Lbytecount < lmessagelength) {Lwordcount = (lbytecount-(lbytecount% 4))/4;lbyteposition = (lbytecount% 4) *8; Lwordarray[lwordcount] = (Lwordarray[lwordcount] |
(String.charcodeat (Lbytecount) <<lbyteposition));
lbytecount++;
} Lwordcount = (lbytecount-(lbytecount% 4))/4;
lbyteposition = (lbytecount% 4) *8; Lwordarray[lwordcount] = Lwordarray[lwordcount] |
(0x80<<lbyteposition);
Lwordarray[lnumberofwords-2] = lmessagelength<<3;
LWORDARRAY[LNUMBEROFWORDS-1] = lmessagelength>>>29;
return lwordarray;
};
function Wordtohex (lValue) {var wordtohexvalue= "", wordtohexvalue_temp= "", Lbyte,lcount;
for (Lcount = 0;lcount<=3;lcount++) {Lbyte = (lvalue>>> (lcount*8)) & 255;
Wordtohexvalue_temp = "0" + lbyte.tostring (16);
Wordtohexvalue = Wordtohexvalue + wordtohexvalue_temp.substr (wordtohexvalue_temp.length-2,2);
return wordtohexvalue;
};
function Utf8encode (string) {string = String.Replace (/\r\n/g, "\ n");
var utftext = ""; for (var n = 0; n < string.lengtH
n++) {var c = string.charcodeat (n);
if (c < 128) {Utftext + = String.fromCharCode (c);
else if ((C > 127) && (C < 2048)) {Utftext + = String.fromCharCode ((c >> 6) | 192;
Utftext + + String.fromCharCode ((C & 63) | 128);
else {Utftext + = String.fromCharCode ((c >> 12) | 224);
Utftext + + String.fromCharCode ((c >> 6) & 63) | 128;
Utftext + + String.fromCharCode ((C & 63) | 128);
} return utftext;
};
var x=array ();
var k,aa,bb,cc,dd,a,b,c,d;
var s11=7, s12=12, s13=17, s14=22;
var s21=5, s22=9, s23=14, s24=20;
var s31=4, s32=11, s33=16, s34=23;
var s41=6, s42=10, s43=15, s44=21;
String = Utf8encode (string);
x = Converttowordarray (string); A = 0x67452301; b = 0xefcdab89; c = 0x98badcfe;
D = 0x10325476; for (k=0;k<x.length;k+=16) {aa=a; Bb=b; Cc=c;
Dd=d;
A=FF (a,b,c,d,x[k+0], s11,0xd76aa478);
D=FF (d,a,b,c,x[k+1], s12,0xe8c7b756); C=ff(C,d,a,b,x[k+2], s13,0x242070db);
B=FF (b,c,d,a,x[k+3], s14,0xc1bdceee);
A=FF (A,b,c,d,x[k+4], S11,0XF57C0FAF);
D=FF (d,a,b,c,x[k+5], s12,0x4787c62a);
C=FF (C,d,a,b,x[k+6], s13,0xa8304613);
B=FF (B,c,d,a,x[k+7], s14,0xfd469501);
A=FF (A,b,c,d,x[k+8], s11,0x698098d8);
D=FF (d,a,b,c,x[k+9], S12,0X8B44F7AF);
C=FF (C,D,A,B,X[K+10],S13,0XFFFF5BB1);
B=FF (B,C,D,A,X[K+11],S14,0X895CD7BE);
A=FF (a,b,c,d,x[k+12],s11,0x6b901122);
D=FF (d,a,b,c,x[k+13],s12,0xfd987193);
C=FF (c,d,a,b,x[k+14],s13,0xa679438e);
B=FF (b,c,d,a,x[k+15],s14,0x49b40821);
A=gg (a,b,c,d,x[k+1], s21,0xf61e2562);
D=gg (D,a,b,c,x[k+6], s22,0xc040b340);
C=gg (C,D,A,B,X[K+11],S23,0X265E5A51);
B=gg (b,c,d,a,x[k+0], S24,0XE9B6C7AA);
A=gg (a,b,c,d,x[k+5], s21,0xd62f105d);
D=gg (d,a,b,c,x[k+10],s22,0x2441453);
C=gg (c,d,a,b,x[k+15],s23,0xd8a1e681);
B=gg (B,c,d,a,x[k+4], S24,0XE7D3FBC8);
A=gg (a,b,c,d,x[k+9], s21,0x21e1cde6);
D=gg (D,A,B,C,X[K+14],S22,0XC33707D6); C=gg (C,d,a,b,x[k+3],S23,0XF4D50D87);
B=gg (B,c,d,a,x[k+8], s24,0x455a14ed);
A=gg (a,b,c,d,x[k+13],s21,0xa9e3e905);
D=gg (d,a,b,c,x[k+2], s22,0xfcefa3f8);
C=gg (C,d,a,b,x[k+7], s23,0x676f02d9);
B=gg (B,C,D,A,X[K+12],S24,0X8D2A4C8A);
A=HH (a,b,c,d,x[k+5], s31,0xfffa3942);
D=HH (D,a,b,c,x[k+8], s32,0x8771f681);
C=HH (c,d,a,b,x[k+11],s33,0x6d9d6122);
B=HH (b,c,d,a,x[k+14],s34,0xfde5380c);
A=HH (a,b,c,d,x[k+1], s31,0xa4beea44);
D=HH (D,a,b,c,x[k+4], s32,0x4bdecfa9);
C=HH (C,d,a,b,x[k+7], s33,0xf6bb4b60);
B=HH (B,C,D,A,X[K+10],S34,0XBEBFBC70);
A=HH (A,B,C,D,X[K+13],S31,0X289B7EC6);
D=HH (d,a,b,c,x[k+0], S32,0XEAA127FA);
C=HH (c,d,a,b,x[k+3], s33,0xd4ef3085);
B=HH (B,c,d,a,x[k+6], s34,0x4881d05);
A=HH (a,b,c,d,x[k+9], s31,0xd9d4d039);
D=HH (D,A,B,C,X[K+12],S32,0XE6DB99E5);
C=HH (C,D,A,B,X[K+15],S33,0X1FA27CF8);
B=HH (b,c,d,a,x[k+2], s34,0xc4ac5665);
A=ii (a,b,c,d,x[k+0], s41,0xf4292244);
D=ii (D,a,b,c,x[k+7], s42,0x432aff97); C=ii (C,D,A,B,X[K+14],S43,0XAB9423A7);
B=ii (b,c,d,a,x[k+5], s44,0xfc93a039);
A=ii (A,B,C,D,X[K+12],S41,0X655B59C3);
D=ii (d,a,b,c,x[k+3], s42,0x8f0ccc92);
C=ii (c,d,a,b,x[k+10],s43,0xffeff47d);
B=ii (b,c,d,a,x[k+1], S44,0X85845DD1);
A=ii (A,b,c,d,x[k+8], s41,0x6fa87e4f);
D=ii (D,A,B,C,X[K+15],S42,0XFE2CE6E0);
C=ii (C,d,a,b,x[k+6], s43,0xa3014314);
B=ii (B,C,D,A,X[K+13],S44,0X4E0811A1);
A=ii (A,b,c,d,x[k+4], s41,0xf7537e82);
D=ii (d,a,b,c,x[k+11],s42,0xbd3af235);
C=ii (c,d,a,b,x[k+2], S43,0X2AD7D2BB);
B=ii (b,c,d,a,x[k+9], s44,0xeb86d391);
A=addunsigned (A,AA);
B=addunsigned (B,BB);
C=addunsigned (C,CC);
D=addunsigned (D,DD);
var temp = Wordtohex (a) +wordtohex (b) +wordtohex (c) +wordtohex (d);
return Temp.tolowercase ();
}
The above is the entire content of this article, Java, JavaScript, MD5 the relationship between the three, use, hope for everyone's learning help.