Foros del Web » Programando para Internet » Javascript »

Ayuda con encriptación Hash

Estas en el tema de Ayuda con encriptación Hash en el foro de Javascript en Foros del Web. Tengo un script para hacer ejercicios de enseñanza con autocorrección. Las soluciones van introducidas encriptadas en Hash y necesito saber cómo colocar las nuevas soluciones ...
  #1 (permalink)  
Antiguo 05/04/2007, 10:52
 
Fecha de Ingreso: octubre-2004
Mensajes: 289
Antigüedad: 19 años, 6 meses
Puntos: 10
Ayuda con encriptación Hash

Tengo un script para hacer ejercicios de enseñanza con autocorrección. Las soluciones van introducidas encriptadas en Hash y necesito saber cómo colocar las nuevas soluciones en código Hash, o bien como modificar el script para que acepte lenguaje común.

Este es el script completo:

p= new Array();

p['p1'] = 5;
p['p2'] = 5;
p['p3'] = 5;
p['p4'] = 5;
p['p5'] = 5;
p['p6'] = 5;
p['p7'] = 5;
p['p8'] = 5;
p['p9'] = 5;
p['p10'] = 5;
p['p11'] = 5;
p['p12'] = 5;
p['p13'] = 5;
p['p14'] = 5;
p['p15'] = 5;
p['p16'] = 5;


q = new Array();


q['q1'] = new Array('a20cfaeafa28ab7c163001c12e11a66205ac2c02');
q['q2'] = new Array('3d7b051f88a58a400d5d829db0a3ad2bf9c108a4');
q['q3'] = new Array('470d801dd3798a196412d9f817d33de2fb1e2b3a', '51b5b155101171d0c8cbc2e538fbfbe5ef5085c1', 'fe204596d4a472ef4b276c50d388cc3983a9bd18', '94b5272936671f2e8d38359422895bf023287f54');
q['q4'] = new Array('6a6723056db5d159a9158299d93ee107ffa478b0');
q['q5'] = new Array('162a958aa2917ca98e2e19400c1b762bae2f7e07');
q['q6'] = new Array('d4ccd66b137ffc3f0e0645dc98b1a783a87607af');
q['q7'] = new Array('4233c1bfb5472b70bbff003c086e35c71459d7d0', '9432391374de0724ac5300838887c1cfa3e2488c', '1460da7243dc779d49e8d91abee7c6ace79b6233');
q['q8'] = new Array('e10d5a2cd713be36d6707d8723bb36a050c72588', 'a9ba521207c289bcdd8fa1e8cc9e49600fc5b972', 'd50c78c00a0c230df21f8e8ab2933204e0b8f972');
q['q9'] = new Array('529e62c90faedc83881e7c78a66c6b68515cfd5e', '4aeffbd9afcbd8283e5fb2ecaef02bed86ce57eb', '4d8ae669d2e780c1b77f067184f59e9f9a519bfb', '95724532da8fd37abfe3260825234f86fce4d9ea');
q['q10'] = new Array('ef716b87c7a61e6e6885a1da076c70c3d5435f3d', 'f0af6b7bd9b594da8d762343cab464bea7deb099', '4d7f3545dffd7b6f889e4817ef13a8b20c0c9263', '7aeb128967f6a355e3c27619f01ab8f9a3113077');
q['q11'] = new Array('a83f1a8beae8d59064803c6144ee8f4c3037a333', '8b140f8b0b711dcc9e3d9c00bfeff726a27afa26');
q['q12'] = new Array('3c83168c6440acfbc711e9cf81d3e7f5e0b8a0a3', '8b03963d5f33fa3531298d1c8f1ce4dd0e36600c');
q['q13'] = new Array('8fd3ea14677986ddd0d6fbd8ce23f2a1bde7d177', '0a1b5877b22b5e62c3706fd029885f1bde90604f', '96e88ebb6f9e793ffd5381fa24892a20ad334d1f');
q['q14'] = new Array('c917813400efef6d74f6f0c253b8f7141b2cca42');
q['q15'] = new Array('776f06176beae0c6a76b1762af1b0c98904ab970', 'f6dc8ca3cb5a4510c26944d8fe067ab3459cdf60');
q['q16'] = new Array('a0a016fb4e51e39ff1fbaba552f901d04274995d', 'f110654c1c5b2b5030241497270ba01d60f7db94');



function simplifica(msg) {

var str=msg.toUpperCase();

str=str.replace(/ /g,'');
str=str.replace(/\,/g,'');
str=str.replace(/\./g,'');
str=str.replace(/\-/g,'');
str=str.replace(/\_/g,'');
str=str.replace(/\'/g,'');
str=str.replace(/\:/g,'');
str=str.replace(/\(/g,'');
str=str.replace(/\)/g,'');
str=str.replace(/\&/g,'');
str=str.replace(/\?/g,'');
str=str.replace(/\¿/g,'');
str=str.replace(/\!/g,'');
str=str.replace(/\¡/g,'');
str=str.replace(/Á/g,'A');
str=str.replace(/É/g,'E');
str=str.replace(/Í/g,'I');
str=str.replace(/Ó/g,'O');
str=str.replace(/Ú/g,'U');

return str;

}


function mirar(str) {

var ok=0;
for (x in q[str])
{
if(sha1Hash(simplifica(document.getElementById(str ).value)) == (q[str][x]) )
{

ok=1;
}
}


if(ok)
{
validar1(str);
}
else
{
validar2(str);
}

}

function carga() {

for (var i=1; i<=25; i++) {
str='q'+i;
document.getElementById(str).style.border = '1px solid #A5ACB2';
document.getElementById(str).disabled = false;

/*str='q'+i+'b';
document.getElementById(str).style.border = '1px solid #A5ACB2';
document.getElementById(str).disabled = false;*/

}
}


function validar1(qstr) {
if(document.getElementById(qstr).disabled == false) {
document.getElementById(qstr).disabled = true;
document.getElementById(qstr).style.border = '2px solid #00FF00';
/* document.getElementById('correct').innerHTML = document.getElementById('correct').innerHTML*1 + 1; */
document.getElementById('correct').innerHTML = '';
}
}

function validar2(qstr) {
if(document.getElementById(qstr).value=='') {
document.getElementById(qstr).style.border = '1px solid #A5ACB2';
}
else
{
document.getElementById(qstr).style.border = '2px solid #FF0000';
}
}


function sha1Hash(msg)
{
// constants [4.2.1]
var K = [0x5a827999, 0x6ed9eba1, 0x8f1bbcdc, 0xca62c1d6];

// PREPROCESSING

msg += String.fromCharCode(0x80); // add trailing '1' bit to string [5.1.1]

// convert string msg into 512-bit/16-integer blocks arrays of ints [5.2.1]
var l = Math.ceil(msg.length/4) + 2; // long enough to contain msg plus 2-word length
var N = Math.ceil(l/16); // in N 16-int blocks
var M = new Array(N);

for (var i=0; i<N; i++) {
M[i] = new Array(16);
for (var j=0; j<16; j++) { // encode 4 chars per integer, big-endian encoding
M[i][j] = (msg.charCodeAt(i*64+j*4)<<24) | (msg.charCodeAt(i*64+j*4+1)<<16) |
(msg.charCodeAt(i*64+j*4+2)<<8) | (msg.charCodeAt(i*64+j*4+3));
} // note running off the end of msg is ok 'cos bitwise ops on NaN return 0
}
// add length (in bits) into final pair of 32-bit integers (big-endian) [5.1.1]
M[N-1][14] = ((msg.length-1) >>> 30) * 8;
M[N-1][15] = ((msg.length-1)*8) & 0xffffffff;

// set initial hash value [5.3.1]
var H0 = 0x67452301;
var H1 = 0xefcdab89;
var H2 = 0x98badcfe;
var H3 = 0x10325476;
var H4 = 0xc3d2e1f0;

// HASH COMPUTATION [6.1.2]

var W = new Array(80); var a, b, c, d, e;
for (var i=0; i<N; i++) {

// 1 - prepare message schedule 'W'
for (var t=0; t<16; t++) W[t] = M[i][t];
for (var t=16; t<80; t++) W[t] = ROTL(W[t-3] ^ W[t-8] ^ W[t-14] ^ W[t-16], 1);

// 2 - initialise five working variables a, b, c, d, e with previous hash value
a = H0; b = H1; c = H2; d = H3; e = H4;

// 3 - main loop
for (var t=0; t<80; t++) {
var s = Math.floor(t/20); // seq for blocks of 'f' functions and 'K' constants
var T = (ROTL(a,5) + f(s,b,c,d) + e + K[s] + W[t]) & 0xffffffff;
e = d;
d = c;
c = ROTL(b, 30);
b = a;
a = T;
}

// 4 - compute the new intermediate hash value
H0 = (H0+a) & 0xffffffff; // note 'addition modulo 2^32'
H1 = (H1+b) & 0xffffffff;
H2 = (H2+c) & 0xffffffff;
H3 = (H3+d) & 0xffffffff;
H4 = (H4+e) & 0xffffffff;
}

return H0.toHexStr() + H1.toHexStr() + H2.toHexStr() + H3.toHexStr() + H4.toHexStr();
}

//
// function 'f' [4.1.1]
//
function f(s, x, y, z)
{
switch (s) {
case 0: return (x & y) ^ (~x & z);
case 1: return x ^ y ^ z;
case 2: return (x & y) ^ (x & z) ^ (y & z);
case 3: return x ^ y ^ z;
}
}

//
// rotate left (circular left shift) value x by n positions [3.2.5]
//
function ROTL(x, n)
{
return (x<<n) | (x>>>(32-n));
}

//
// extend Number class with a tailored hex-string method
// (note toString(16) is implementation-dependant, and
// in IE returns signed numbers when used on full words)
//
Number.prototype.toHexStr = function()
{
var s="", v;
for (var i=7; i>=0; i--) { v = (this>>>(i*4)) & 0xf; s += v.toString(16); }
return s;
}
function resetForm() {

for(i=1;i<=25;i++) {
document.getElementById('q'+i).disabled = false;

/*document.getElementById('q'+i+'b').disabled = false;*/
}
for(i=1;i<=25;i++) {
p['p'+i] = 5;

/*document.getElementById('q'+i+'b').disabled = false;*/
}

document.forms[0].reset();
}

function cuentaCorrectos() {
var j=0;
for(i=1;i<=25;i++) {

if ((document.getElementById('q'+i).disabled == true))
{
j=j + p['p'+i];
}

/*document.getElementById('q'+i+'b').disabled = false;*/
}
document.getElementById('correct2').innerHTML = '<b><u>' + j + ' puntos de 125 posibles. ' + '</u></b>';
}

function FP_changeProp() {//v1.0
var args=arguments,d=document,i,j,id=args[0],o=FP_getObjectByID(id),s,ao,v,x;
d.$cpe=new Array(); if(o) for(i=2; i<args.length; i+=2) { v=args[i+1]; s="o";
ao=args[i].split("."); for(j=0; j<ao.length; j++) { s+="."+ao[j]; if(null==eval(s)) {
s=null; break; } } x=new Object; x.o=o; x.n=new Array(); x.v=new Array();
x.n[x.n.length]=s; eval("x.v[x.v.length]="+s); d.$cpe[d.$cpe.length]=x;
if(s) eval(s+"=v"); }
}

function FP_getObjectByID(id,o) {//v1.0
var c,el,els,f,m,n; if(!o)o=document; if(o.getElementById) el=o.getElementById(id);
else if(o.layers) c=o.layers; else if(o.all) el=o.all[id]; if(el) return el;
if(o.id==id || o.name==id) return o; if(o.childNodes) c=o.childNodes; if(c)
for(n=0; n<c.length; n++) { el=FP_getObjectByID(id,c[n]); if(el) return el; }
f=o.forms; if(f) for(n=0; n<f.length; n++) { els=f[n].elements;
for(m=0; m<els.length; m++){ el=FP_getObjectByID(id,els[n]); if(el) return el; } }
return null;
}

FIN DEL CODIGO

En este ejemplo la primera pregunta viene en esta línea:

q['q1'] = new Array('a20cfaeafa28ab7c163001c12e11a66205ac2c02');

dónde este código hash

a20cfaeafa28ab7c163001c12e11a66205ac2c02

es la solución (En este caso es "Woody Allen").

Muchas gracias por su atención.

Saludos
  #2 (permalink)  
Antiguo 05/04/2007, 11:16
Avatar de Shiryu_Libra
Colaborador
 
Fecha de Ingreso: febrero-2007
Ubicación: Cantando "Screenager" en "Kirafa Kaput"
Mensajes: 3.614
Antigüedad: 17 años, 2 meses
Puntos: 88
Re: Ayuda con encriptación Hash

if(ok) <<<---------- igual a algo?????
{
validar1(str);
}
else
{
validar2(str);
}
}


no se supone que en esta parte deberia de haber alguna validacion?

if(ok)=0
o
if (ok)=1
__________________
"Eres parte del problema, parte de la solucion o parte del paisaje"
Un Saludo desde Desierto de Altar, Sonora, MX.
Shiryu_libra
  #3 (permalink)  
Antiguo 05/04/2007, 16:08
Avatar de derkenuke
Colaborador
 
Fecha de Ingreso: octubre-2003
Ubicación: self.location.href
Mensajes: 2.665
Antigüedad: 20 años, 6 meses
Puntos: 45
Re: Ayuda con encriptación Hash

No entiendo muy bien la pregunta ssclamp. Según tu código sha1Hash("hola") codificaría la palabra "hola" no? ¿Qué es lo que intentas hacer?

Shiryu_Libra ahí no tiene porqué haber nada, ya que ok puede tomar los valores 1 ó 0, entonces
if(ok==1) equivale a if(ok)
y
if(ok==0) equivale a if(!ok) (o en este caso el else del IF anterior)
__________________
- Haz preguntas inteligentes, y obtendrás más y mejores respuestas.
- Antes de postearlo Inténtalo y Búscalo.
- Escribe correctamente tus mensajes.
  #4 (permalink)  
Antiguo 05/04/2007, 19:18
Avatar de Shiryu_Libra
Colaborador
 
Fecha de Ingreso: febrero-2007
Ubicación: Cantando "Screenager" en "Kirafa Kaput"
Mensajes: 3.614
Antigüedad: 17 años, 2 meses
Puntos: 88
Re: Ayuda con encriptación Hash

Cita:
Iniciado por derkenuke Ver Mensaje
...
Shiryu_Libra ahí no tiene porqué haber nada, ya que ok puede tomar los valores 1 ó 0, entonces
if(ok==1) equivale a if(ok)
y
if(ok==0) equivale a if(!ok) (o en este caso el else del IF anterior)
amigo, gracias por la asesoria constante, tan acertivo como siempre derkenuke
__________________
"Eres parte del problema, parte de la solucion o parte del paisaje"
Un Saludo desde Desierto de Altar, Sonora, MX.
Shiryu_libra
  #5 (permalink)  
Antiguo 05/04/2007, 19:46
Avatar de derkenuke
Colaborador
 
Fecha de Ingreso: octubre-2003
Ubicación: self.location.href
Mensajes: 2.665
Antigüedad: 20 años, 6 meses
Puntos: 45
Re: Ayuda con encriptación Hash

De nada
__________________
- Haz preguntas inteligentes, y obtendrás más y mejores respuestas.
- Antes de postearlo Inténtalo y Búscalo.
- Escribe correctamente tus mensajes.
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 23:34.