Md5

  • July 2020
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Md5 as PDF for free.

More details

  • Words: 1,542
  • Pages: 5
/* * Implementação do algoritmo de encriptação com seus devidos direitos autorais: * * Algoritmo traduzido e comentado por mim: STORM [email protected] * * A JavaScript implementation of the RSA Data Security, Inc. MD5 Message * Digest Algorithm, as defined in RFC 1321. * Version 2.1 Copyright (C) Paul Johnston 1999 - 2002. * Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet * Distributed under the BSD License * See http://pajhome.org.uk/crypt/md5 for more info. */ /* * Configuração das variáveis. Você pode necessitar destas para ser compatível com o usuário, * mas o default (padrão) trabalha em todos os casos. */ 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 */ /* * Estas são as funções que você geralmente requererá se chamar. * Faz o argumento das strings e retorna a string em hex ou base-64. */ function hex_md5(s){ return binl2hex(core_md5(str2binl(s), s.length * chrsz));} function 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)); } /* * Um simples teste para ver se o VM [algoritmo md5] está trabalhando. */ function md5_vm_test() { return hex_md5("abc") == "900150983cd24fb0d6963f7d28e17f72"; } /* * Calcular o MD5 de um array de palavras pequenas. */ function core_md5(x, len) { /* append padding */ x[len >> 5] |= 0x80 << ((len) % 32); x[(((len + 64) >>> 9) << 4) + 14] = len; var var var var

a b c d

= 1732584193; = -271733879; = -1732584194; = 271733878;

for(var i = 0; i < x.length; i += 16)

{

var var var var

olda oldb oldc oldd

= = = =

a; b; c; d;

a d c b a d c b a d c b a d c b

= = = = = = = = = = = = = = = =

md5_ff(a, md5_ff(d, md5_ff(c, md5_ff(b, md5_ff(a, md5_ff(d, md5_ff(c, md5_ff(b, md5_ff(a, md5_ff(d, md5_ff(c, md5_ff(b, md5_ff(a, md5_ff(d, md5_ff(c, md5_ff(b,

b, a, d, c, b, a, d, c, b, a, d, c, b, a, d, c,

c, b, a, d, c, b, a, d, c, b, a, d, c, b, a, d,

d, c, b, a, d, c, b, a, d, c, b, a, d, c, b, a,

x[i+ 0], x[i+ 1], x[i+ 2], x[i+ 3], x[i+ 4], x[i+ 5], x[i+ 6], x[i+ 7], x[i+ 8], x[i+ 9], x[i+10], x[i+11], x[i+12], x[i+13], x[i+14], x[i+15],

7 , 12, 17, 22, 7 , 12, 17, 22, 7 , 12, 17, 22, 7 , 12, 17, 22,

-680876936); -389564586); 606105819); -1044525330); -176418897); 1200080426); -1473231341); -45705983); 1770035416); -1958414417); -42063); -1990404162); 1804603682); -40341101); -1502002290); 1236535329);

a d c b a d c b a d c b a d c b

= = = = = = = = = = = = = = = =

md5_gg(a, md5_gg(d, md5_gg(c, md5_gg(b, md5_gg(a, md5_gg(d, md5_gg(c, md5_gg(b, md5_gg(a, md5_gg(d, md5_gg(c, md5_gg(b, md5_gg(a, md5_gg(d, md5_gg(c, md5_gg(b,

b, a, d, c, b, a, d, c, b, a, d, c, b, a, d, c,

c, b, a, d, c, b, a, d, c, b, a, d, c, b, a, d,

d, c, b, a, d, c, b, a, d, c, b, a, d, c, b, a,

x[i+ 1], x[i+ 6], x[i+11], x[i+ 0], x[i+ 5], x[i+10], x[i+15], x[i+ 4], x[i+ 9], x[i+14], x[i+ 3], x[i+ 8], x[i+13], x[i+ 2], x[i+ 7], x[i+12],

5 , 9 , 14, 20, 5 , 9 , 14, 20, 5 , 9 , 14, 20, 5 , 9 , 14, 20,

-165796510); -1069501632); 643717713); -373897302); -701558691); 38016083); -660478335); -405537848); 568446438); -1019803690); -187363961); 1163531501); -1444681467); -51403784); 1735328473); -1926607734);

a d c b a d c b a d c b a d c b

= = = = = = = = = = = = = = = =

md5_hh(a, md5_hh(d, md5_hh(c, md5_hh(b, md5_hh(a, md5_hh(d, md5_hh(c, md5_hh(b, md5_hh(a, md5_hh(d, md5_hh(c, md5_hh(b, md5_hh(a, md5_hh(d, md5_hh(c, md5_hh(b,

b, a, d, c, b, a, d, c, b, a, d, c, b, a, d, c,

c, b, a, d, c, b, a, d, c, b, a, d, c, b, a, d,

d, c, b, a, d, c, b, a, d, c, b, a, d, c, b, a,

x[i+ 5], x[i+ 8], x[i+11], x[i+14], x[i+ 1], x[i+ 4], x[i+ 7], x[i+10], x[i+13], x[i+ 0], x[i+ 3], x[i+ 6], x[i+ 9], x[i+12], x[i+15], x[i+ 2],

4 , 11, 16, 23, 4 , 11, 16, 23, 4 , 11, 16, 23, 4 , 11, 16, 23,

-378558); -2022574463); 1839030562); -35309556); -1530992060); 1272893353); -155497632); -1094730640); 681279174); -358537222); -722521979); 76029189); -640364487); -421815835); 530742520); -995338651);

a d c b a d c b a d c

= = = = = = = = = = =

md5_ii(a, md5_ii(d, md5_ii(c, md5_ii(b, md5_ii(a, md5_ii(d, md5_ii(c, md5_ii(b, md5_ii(a, md5_ii(d, md5_ii(c,

b, a, d, c, b, a, d, c, b, a, d,

c, b, a, d, c, b, a, d, c, b, a,

d, c, b, a, d, c, b, a, d, c, b,

x[i+ 0], x[i+ 7], x[i+14], x[i+ 5], x[i+12], x[i+ 3], x[i+10], x[i+ 1], x[i+ 8], x[i+15], x[i+ 6],

6 , 10, 15, 21, 6 , 10, 15, 21, 6 , 10, 15,

-198630844); 1126891415); -1416354905); -57434055); 1700485571); -1894986606); -1051523); -2054922799); 1873313359); -30611744); -1560198380);

b a d c b

= = = = =

md5_ii(b, md5_ii(a, md5_ii(d, md5_ii(c, md5_ii(b,

c, b, a, d, c,

a b c d

= = = =

safe_add(a, safe_add(b, safe_add(c, safe_add(d,

d, c, b, a, d,

a, d, c, b, a,

x[i+13], x[i+ 4], x[i+11], x[i+ 2], x[i+ 9],

21, 1309151649); 6 , -145523070); 10, -1120210379); 15, 718787259); 21, -343485551);

olda); oldb); oldc); oldd);

} return Array(a, b, c, d); } /* * Estas funções executam as quatro operações básicas que o algoritmo usa. */ 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); } function 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); } /* * Calcular o HMAC-MD5, de uma chave e de alguns dados */ function core_hmac_md5(key, data) { var bkey = str2binl(key); if(bkey.length > 16) bkey = core_md5(bkey, key.length * chrsz); var ipad = Array(16), opad = Array(16); for(var i = 0; i < 16; i++) { ipad[i] = bkey[i] ^ 0x36363636; opad[i] = bkey[i] ^ 0x5C5C5C5C; } var hash = core_md5(ipad.concat(str2binl(data)), 512 + data.length * chrsz); return core_md5(opad.concat(hash), 512 + 128); } /* * Adicionar os inteiros, envolvendo em 2^32. Isto usa 16-bit internamente * para trabalhar em torno dos erros em intérpretes de algum JS. */ function safe_add(x, y) {

}

var lsw = (x & 0xFFFF) + (y & 0xFFFF); var msw = (x >> 16) + (y >> 16) + (lsw >> 16); return (msw << 16) | (lsw & 0xFFFF);

/* * Rodar um numero de 32-bit para à esquerda. */ function bit_rol(num, cnt) { return (num << cnt) | (num >>> (32 - cnt)); } /* * Converter uma string de um array para uma palavra. * Se o chrsz for ASCII, os carácteres (maior)>255 têm seu hi-byte ignorado. */ 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; } /* * Converte um array de palavras para uma string. */ function binl2str(bin) { var str = ""; var mask = (1 << chrsz) - 1; for(var i = 0; i < bin.length * 32; i += chrsz) str += String.fromCharCode((bin[i>>5] >>> (i % 32)) & mask); return str; } /* * Converte um array de palavras para uma string em hex. */ 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; } /* * Converte um array de palavras para uma string em base-64. */ function binl2b64(binarray) { var tab = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; 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 * 32) str += b64pad; else str += tab.charAt((triplet >> 6*(3-j)) & 0x3F); } } return str;

Related Documents

Md5
July 2020 4
Md5.txt
June 2020 11
Algoritmo Md5
November 2019 15
Base64 Md5
October 2019 13
Md5-512.pdf
November 2019 11