Untitled
unknown
html
4 years ago
3.3 kB
14
Indexable
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Hash</title>
</head>
<body>
<p style="margin-bottom: 5px;">Llave</p>
<input id="key"><br>
<p style="margin-bottom: 5px;">Texto a convertir (Solo caracteres del 33 al 126 respecto a la <a href="https://www.rapidtables.com/code/text/ascii-table.html" target="blank">Tabla ASCII</a> )</p>
<input id="text"><br>
<button id="encrypt" style="margin-top: 15px;">Encriptar</button>
<button id="decrypt" >Desencriptar</button><br>
<p style="margin-bottom: 5px;">Resultado: </p><input id="result">
<button id="load">Cargar</button>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
const ascii_first = 33;
const ascii_last = 126;
const total_chars = ascii_last-ascii_first;
function hashText(key, text)
{
var new_text = "";
var diff = 0;
for (var i = 0; i < key.length; i++) {
const ascii = key[i].charCodeAt(0);
if (isValidByASCII(ascii)) {
diff = diff+ascii;
}
else {
alert("Ocurrio un error | Char: [" + key[i] + "] CharCode: [" + ascii + "] Index: [" + i + "] | (hash | key)");
return false;
}
}
for (var i = 0; i < text.length; i++) {
const ascii = text[i].charCodeAt(0);
if (isValidByASCII(ascii)) {
const new_ascii = getNumberLimit(ascii+(diff*(i+1)), total_chars)+ascii_first;
const new_char = String.fromCharCode(new_ascii);
new_text = new_text + new_char;
}
else {
alert("Ocurrio un error | Char: [" + text[i] + "] CharCode: [" + ascii + "] Index: [" + i + "] | (hash | text)");
return false;
}
}
return new_text;
}
function unhashText(key, text)
{
var new_text = "";
var diff = 0;
for (var i = 0; i < key.length; i++) {
const ascii = key[i].charCodeAt(0);
if (isValidByASCII(ascii)) {
diff = diff+ascii;
}
else {
alert("Ocurrio un error | Char: [" + key[i] + "] ASCII: [" + ascii + "] Index: [" + i + "] | (hash | key)");
return false;
}
}
for (var i = 0; i < text.length; i++) {
const ascii = text[i].charCodeAt(0);
if (isValidByASCII(ascii)) {
const new_ascii = getNumberLimit(-(diff*(i+1))+(ascii-(ascii_first*2)), total_chars)+ascii_first;
const new_char = String.fromCharCode(new_ascii);
new_text = new_text + new_char;
}
else {
alert("Ocurrio un error | Char: [" + text[i] + "] ASCII: [" + ascii + "] Index: [" + i + "] | (unhash | text)");
return false;
}
}
return new_text;
}
function isValidByASCII(index){
return index >= ascii_first && index <= ascii_last;
}
function getNumberLimit(index, max)
{
return index-(Math.floor(index/(max+1))*(max+1));
}
$("#encrypt").click(function() {
const key = $("#key").val();
const text = $("#text").val();
$("#result").val(hashText(key, text));
});
$("#decrypt").click(function() {
const key = $("#key").val();
const text = $("#text").val();
$("#result").val(unhashText(key, text));
});
$("#load").click(function() {
const text = $("#result").val();
$("#text").val(text);
});
</script>
</body>
</html>Editor is loading...