/* md5.js - MD5 Message-Digest * Copyright (C) 1999,2002 Masanao Izumo * Version: 2.0.0 * LastModified: May 13 2002 * * This program is free software. You can redistribute it and/or modify * it without any warranty. This library calculates the MD5 based on RFC1321. * See RFC1321 for more information and algorism. */ /* Interface: * md5_128bits = MD5_hash(data); * md5_hexstr = MD5_hexhash(data); */ /* ChangeLog * 2002/05/13: Version 2.0.0 released * NOTICE: API is changed. * 2002/04/15: Bug fix about MD5 length. */ /* 2005-04-05 http://ma.la/lib/Digest/MD5.js */ //シンプルな使い方 //Digest.MD5("text").hexdigest; if(typeof Digest == "undefined"){Digest = {}} Digest.MD5 = {}; Digest.MD5.init = function(){ //関数として呼び出すための定義 Digest.MD5.md5 = function(str){return Digest.MD5._private_.MD5_hash(str)}; Digest.MD5.md5_hex = function(str){return Digest.MD5._private_.MD5_hexhash(str)}; Digest.MD5.EXPORT = []; Digest.MD5.EXPORT_OK = ["md5","md5_hex"]; var properties = {}; for(var i in Digest.MD5){properties[i]=Digest.MD5[i]} Digest.MD5 = function(o){ //引数がある場合 this.str = ""; if(typeof o == "string"){this.str = o} this.add = function(o){ (this.str) ? this.str += o : this.str = o; return this; }; var clone = this; this.reset = function(){this.str="";return this} this.digest = function(str){ if(typeof str == "string"){ return Digest.MD5._private_.MD5_hash(str); }else if(typeof this.str == "string"){ return Digest.MD5._private_.MD5_hash(this.str) } }; this.digest.toString = function(){return clone.digest()} this.hexdigest = function(str){ if(typeof str == "string"){ return Digest.MD5._private_.MD5_hexhash(str); }else if(typeof this.str == "string"){ return Digest.MD5._private_.MD5_hexhash(this.str) } }; this.hexdigest.toString = function(){return clone.hexdigest()}; return this; }; for(var i in properties){Digest.MD5[i] = properties[i]} Digest.MD5.digest = function(str){return Digest.MD5().digest(str)} Digest.MD5.hexdigest = function(str){return Digest.MD5().hexdigest(str)}; }; Digest.MD5._private_ = { // md5_T[i] = parseInt(Math.abs(Math.sin(i)) * 4294967296.0); MD5_T : [ 0x00000000, 0xd76aa478, 0xe8c7b756, 0x242070db, 0xc1bdceee, 0xf57c0faf, 0x4787c62a, 0xa8304613, 0xfd469501, 0x698098d8, 0x8b44f7af, 0xffff5bb1, 0x895cd7be, 0x6b901122, 0xfd987193, 0xa679438e, 0x49b40821, 0xf61e2562, 0xc040b340, 0x265e5a51, 0xe9b6c7aa, 0xd62f105d, 0x02441453, 0xd8a1e681, 0xe7d3fbc8, 0x21e1cde6, 0xc33707d6, 0xf4d50d87, 0x455a14ed, 0xa9e3e905, 0xfcefa3f8, 0x676f02d9, 0x8d2a4c8a, 0xfffa3942, 0x8771f681, 0x6d9d6122, 0xfde5380c, 0xa4beea44, 0x4bdecfa9, 0xf6bb4b60, 0xbebfbc70, 0x289b7ec6, 0xeaa127fa, 0xd4ef3085, 0x04881d05, 0xd9d4d039, 0xe6db99e5, 0x1fa27cf8, 0xc4ac5665, 0xf4292244, 0x432aff97, 0xab9423a7, 0xfc93a039, 0x655b59c3, 0x8f0ccc92, 0xffeff47d, 0x85845dd1, 0x6fa87e4f, 0xfe2ce6e0, 0xa3014314, 0x4e0811a1, 0xf7537e82, 0xbd3af235, 0x2ad7d2bb, 0xeb86d391 ], MD5_round : (function(){ var MD5_round1 = [ [ 0, 7, 1], [ 1,12, 2],[ 2,17, 3], [ 3,22, 4], [ 4, 7, 5], [ 5,12, 6],[ 6,17, 7], [ 7,22, 8], [ 8, 7, 9], [ 9,12,10],[10,17,11], [11,22,12], [12, 7,13], [13,12,14],[14,17,15], [15,22,16] ]; var MD5_round2 = [ [ 1, 5,17], [ 6, 9,18],[11,14,19], [ 0,20,20], [ 5, 5,21], [10, 9,22],[15,14,23], [ 4,20,24], [ 9, 5,25], [14, 9,26],[ 3,14,27], [ 8,20,28], [13, 5,29], [ 2, 9,30],[ 7,14,31], [12,20,32] ]; var MD5_round3 = [ [ 5, 4,33], [ 8,11,34],[11,16,35], [14,23,36], [ 1, 4,37], [ 4,11,38],[ 7,16,39], [10,23,40], [13, 4,41], [ 0,11,42],[ 3,16,43], [ 6,23,44], [ 9, 4,45], [12,11,46],[15,16,47], [ 2,23,48] ]; var MD5_round4 = [ [ 0, 6,49], [ 7,10,50],[14,15,51], [ 5,21,52], [12, 6,53], [ 3,10,54],[10,15,55], [ 1,21,56], [ 8, 6,57], [15,10,58],[ 6,15,59], [13,21,60], [ 4, 6,61], [11,10,62],[ 2,15,63], [ 9,21,64] ]; var MD5_F = function(x, y, z) { return (x & y) | (~x & z); }; var MD5_G = function(x, y, z) { return (x & z) | (y & ~z); }; var MD5_H = function(x, y, z) { return x ^ y ^ z; }; var MD5_I = function(x, y, z) { return y ^ (x | ~z); }; return [ [MD5_F, MD5_round1], [MD5_G, MD5_round2], [MD5_H, MD5_round3], [MD5_I, MD5_round4] ]; })(), MD5_pack : function (n32) { return String.fromCharCode(n32 & 0xff) + String.fromCharCode((n32 >>> 8) & 0xff) + String.fromCharCode((n32 >>> 16) & 0xff) + String.fromCharCode((n32 >>> 24) & 0xff); }, MD5_unpack : function (s4) { return s4.charCodeAt(0) | (s4.charCodeAt(1) << 8) | (s4.charCodeAt(2) << 16) | (s4.charCodeAt(3) << 24); }, MD5_number : function (n) { while (n < 0) n += 4294967296; while (n > 4294967295) n -= 4294967296; return n; }, MD5_apply_round : function (x, s, f, abcd, r) { var a, b, c, d; var kk, ss, ii; var t, u; a = abcd[0]; b = abcd[1]; c = abcd[2]; d = abcd[3]; kk = r[0]; ss = r[1]; ii = r[2]; u = f(s[b], s[c], s[d]); t = s[a] + u + x[kk] + this.MD5_T[ii]; t = this.MD5_number(t); t = ((t<>>(32-ss))); t += s[b]; s[a] = this.MD5_number(t); }, MD5_hash : function (data) { var abcd, x, state, s; var len, index, padLen, f, r; var i, j, k; var tmp; state = [0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476]; len = data.length; index = len & 0x3f; padLen = (index < 56) ? (56 - index) : (120 - index); if(padLen > 0) { data += "\x80"; for(i = 0; i < padLen - 1; i++) data += "\x00"; } data += this.MD5_pack(len * 8); data += this.MD5_pack(0); len += padLen + 8; abcd = [0, 1, 2, 3]; x = new Array(16); s = new Array(4); for(k = 0; k < len; k += 64) { for(i = 0, j = k; i < 16; i++, j += 4) { x[i] = data.charCodeAt(j) | (data.charCodeAt(j + 1) << 8) | (data.charCodeAt(j + 2) << 16) | (data.charCodeAt(j + 3) << 24); } for(i = 0; i < 4; i++) s[i] = state[i]; for(i = 0; i < 4; i++) { f = this.MD5_round[i][0]; r = this.MD5_round[i][1]; for(j = 0; j < 16; j++) { this.MD5_apply_round(x, s, f, abcd, r[j]); tmp = abcd[0]; abcd[0] = abcd[3]; abcd[3] = abcd[2]; abcd[2] = abcd[1]; abcd[1] = tmp; } } for(i = 0; i < 4; i++) { state[i] += s[i]; state[i] = this.MD5_number(state[i]); } } return this.MD5_pack(state[0]) + this.MD5_pack(state[1]) + this.MD5_pack(state[2]) + this.MD5_pack(state[3]); }, MD5_hexhash : function (data) { var i, out, c; var bit128; bit128 = this.MD5_hash(data); out = ""; for(i = 0; i < 16; i++) { c = bit128.charCodeAt(i); out += "0123456789abcdef".charAt((c>>4) & 0xf); out += "0123456789abcdef".charAt(c & 0xf); } return out; } }; Digest.MD5.init();