node-red-contrib-services-mentor
Version:
Mentor Services
86 lines (72 loc) • 2.72 kB
JavaScript
module.exports = function(RED) {
function EncriptaMentor(config) {
RED.nodes.createNode(this,config);
var node = this;
node.on('input', function(msg) {
msg.payload = Codificar(msg.payload);
node.send(msg);
});
}
RED.nodes.registerType("encripta-mentor",EncriptaMentor);
var limite;
var texto_codificado;
function Reiniciar_Index()
{
indx1 = 1;
limite = 3;
}
function Codificar(texto)
{
code = Buffer.from(texto, 'utf-8');
texto_codificado = new Buffer.from("");
Reiniciar_Index();
for (indx = 0; indx < code.length; indx += 1)
{
if (indx1 == limite)
{
indx1 = 1;
limite += 1;
}
if (indx1 % 2 == 0) {
tem_cod = code.readUInt8(indx) + 1 ;
} else {
tem_cod = code.readUInt8(indx) - 1 ;
}
buffer1 = Buffer.from(texto_codificado);
buffer2 = Buffer.from([tem_cod]);
if (indx % 2 == 0) {
texto_codificado = Buffer.concat([buffer1,buffer2]);
} else {
texto_codificado = Buffer.concat([buffer2,buffer1]);
}
indx1 += 1;
}
code = texto_codificado;
texto_codificado = Buffer.from("");
for (indx = 0; indx < code.length; indx += 1)
{
tem_cod = code.readUInt8(indx);
buffer1 = Buffer.from(texto_codificado);
buffer2 = Buffer.from([tem_cod]);
if (indx % 2 == 0) {
texto_codificado = Buffer.concat([buffer2,buffer1]);
} else {
texto_codificado = Buffer.concat([buffer1,buffer2]);
}
}
code = texto_codificado;
texto_codificado = Buffer.from("");
for (indx = 0; indx < code.length; indx += 1)
{
tem_cod = code.readUInt8(indx);
buffer1 = Buffer.from(texto_codificado);
buffer2 = Buffer.from([tem_cod]);
if (indx % 2 == 0) {
texto_codificado = Buffer.concat([buffer1,buffer2]);
} else {
texto_codificado = Buffer.concat([buffer2,buffer1]);
}
}
return texto_codificado.toString("ascii");
}
}