Randomizer/index.js

70 lines
1.7 KiB
JavaScript
Raw Permalink Normal View History

2021-07-13 14:25:20 +00:00
const Cryptr = require("cryptr");
2021-07-14 20:42:42 +00:00
const encode = require("./functions/encode");
const decode = require("./functions/decode");
2021-07-13 14:25:20 +00:00
2021-07-14 20:42:42 +00:00
var encypSep = ["#", "@", "$", "^", "&", "*", "?"];
function Randomizer(secretKey) {
2021-07-13 14:25:20 +00:00
const ts = new Date().toUTCString();
2021-07-14 20:42:42 +00:00
if (!secretKey || typeof secretKey !== "string") {
throw new Error("Error: secret must be a non-0-length string");
2021-07-13 14:25:20 +00:00
}
2021-07-14 20:42:42 +00:00
const cryptr = new Cryptr(secretKey);
2021-07-13 14:25:20 +00:00
const encryptedTs = cryptr.encrypt(ts);
2021-07-14 20:42:42 +00:00
this.showKey = function showKey() {
return secretKey;
};
this.getTs = function getTs() {
return ts;
};
this.encTs = function encTs() {
return encryptedTs;
};
this.sendHeader = function sendHeader() {
let setEncypSep = encypSep[Math.floor(Math.random() * encypSep.length)];
var encryptHeader =
encryptedTs + setEncypSep + Buffer.from(encryptedTs).toString("base64");
const finalHeader = encode(encryptHeader, secretKey);
return finalHeader;
};
}
function Validator(secretKey, encryptedTs) {
const cryptr = new Cryptr(secretKey);
const ts = cryptr.decrypt(encryptedTs);
this.verifyState = function verify(headers) {
const decodedHeaders = decode(headers, secretKey);
const encodedDecryptedTs = [
decodedHeaders.slice(0, 250),
decodedHeaders.slice(251),
];
const decodeDecryptedTs = Buffer.from(
encodedDecryptedTs[1],
"base64"
).toString("utf8");
if (decodeDecryptedTs != encodedDecryptedTs[0]) {
return false;
} else if (cryptr.decrypt(encodedDecryptedTs[0]) != ts) {
return false;
} else return true;
};
2021-07-13 14:25:20 +00:00
this.showKey = function showKey() {
2021-07-14 20:42:42 +00:00
return secretKey;
2021-07-13 14:25:20 +00:00
};
2021-07-14 20:42:42 +00:00
2021-07-13 14:25:20 +00:00
this.getTs = function getTs() {
return ts;
};
}
2021-07-14 20:42:42 +00:00
module.exports = { Randomizer, Validator };