tables sirve que propiedad para obtener ejemplo crear javascript hash md5 sha1

sirve - ¿Función hash simple(no segura) para JavaScript?



propiedad location javascript (7)

Consulte esta http://pajhome.org.uk/crypt/md5/ . Su licencia BSD y realmente fácil de usar. Ejemplo:

md5 = hex_md5("message to digest")

Posible duplicado:
Genera un Hash desde una cadena en Javascript / jQuery

¿Puede alguien sugerir una función hash simple (es decir, decenas de líneas de código, no de cientos) escrita en JavaScript (compatible con el navegador)? Idealmente, me gustaría algo que, al pasar una cadena como entrada, produzca algo similar a la cadena hexadecimal de 32 caracteres que es la salida típica de MD5, SHA1, etc. No tiene que ser criptográficamente segura, solo razonablemente resistente a las colisiones. . (Mi caso de uso inicial son las URL, pero probablemente quiera usarlo en otras cadenas en el futuro).



Hasher de objeto simple:

(function () { Number.prototype.toHex = function () { var ret = ((this<0?0x8:0)+((this >> 28) & 0x7)).toString(16) + (this & 0xfffffff).toString(16); while (ret.length < 8) ret = ''0''+ret; return ret; }; Object.hashCode = function hashCode(o, l) { l = l || 2; var i, c, r = []; for (i=0; i<l; i++) r.push(i*268803292); function stringify(o) { var i,r; if (o === null) return ''n''; if (o === true) return ''t''; if (o === false) return ''f''; if (o instanceof Date) return ''d:''+(0+o); i=typeof o; if (i === ''string'') return ''s:''+o.replace(/([////;])/g,''//$1''); if (i === ''number'') return ''n:''+o; if (o instanceof Function) return ''m:''+o.toString().replace(/([////;])/g,''//$1''); if (o instanceof Array) { r=[]; for (i=0; i<o.length; i++) r.push(stringify(o[i])); return ''a:''+r.join('';''); } r=[]; for (i in o) { r.push(i+'':''+stringify(o[i])) } return ''o:''+r.join('';''); } o = stringify(o); for (i=0; i<o.length; i++) { for (c=0; c<r.length; c++) { r[c] = (r[c] << 13)-(r[c] >> 19); r[c] += o.charCodeAt(i) << (r[c] % 24); r[c] = r[c] & r[c]; } } for (i=0; i<r.length; i++) { r[i] = r[i].toHex(); } return r.join(''''); } }());

Aquí la carne es el agente de unión, que simplemente convierte cualquier objeto en una cadena única. hashCode luego se ejecuta sobre el objeto, mezclando los caracteres del objeto codificado.

Para obtener puntos adicionales, exporta el secuenciador y crea un analizador.


Hay muchas realizaciones de funciones hash escritas en JS. Por ejemplo:

Si no necesita seguridad, también puede usar base64, que no es función hash, no tiene salida fija y el usuario podría simplemente decodificarla, pero parece más liviana y podría usarse para ocultar valores: http://www.webtoolkit.info/javascript-base64.html


No lo verifiqué yo mismo, pero puedes ver esta implementación de JavaScript del método String.hashCode () de Java . Parece razonablemente corto.

Con este prototipo, simplemente puede llamar a .hashCode() en cualquier cadena, por ejemplo, "some string".hashCode() , y recibir un código numérico hash (más específicamente, un equivalente Java) como 1395333309.

String.prototype.hashCode = function() { var hash = 0; if (this.length == 0) { return hash; } for (var i = 0; i < this.length; i++) { var char = this.charCodeAt(i); hash = ((hash<<5)-hash)+char; hash = hash & hash; // Convert to 32bit integer } return hash; }



// Simple but unreliable function to create string hash by Sergey.Shuchkin [t] gmail.com // alert( strhash(''http://www.w3schools.com/js/default.asp'') ); // 6mn6tf7st333r2q4o134o58888888888 function strhash( str ) { if (str.length % 32 > 0) str += Array(33 - str.length % 32).join("z"); var hash = '''', bytes = [], i = j = k = a = 0, dict = [''a'',''b'',''c'',''d'',''e'',''f'',''g'',''h'',''i'',''j'',''k'',''l'',''m'',''n'',''o'',''p'',''q'',''r'',''s'',''t'',''u'',''v'',''w'',''x'',''y'',''1'',''2'',''3'',''4'',''5'',''6'',''7'',''8'',''9'']; for (i = 0; i < str.length; i++ ) { ch = str.charCodeAt(i); bytes[j++] = (ch < 127) ? ch & 0xFF : 127; } var chunk_len = Math.ceil(bytes.length / 32); for (i=0; i<bytes.length; i++) { j += bytes[i]; k++; if ((k == chunk_len) || (i == bytes.length-1)) { a = Math.floor( j / k ); if (a < 32) hash += ''0''; else if (a > 126) hash += ''z''; else hash += dict[ Math.floor( (a-32) / 2.76) ]; j = k = 0; } } return hash; }