-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
69 lines (55 loc) · 1.72 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
const Cryptr = require("cryptr");
const encode = require("./functions/encode");
const decode = require("./functions/decode");
var encypSep = ["#", "@", "$", "^", "&", "*", "?"];
function Randomizer(secretKey) {
const ts = new Date().toUTCString();
if (!secretKey || typeof secretKey !== "string") {
throw new Error("Error: secret must be a non-0-length string");
}
const cryptr = new Cryptr(secretKey);
const encryptedTs = cryptr.encrypt(ts);
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;
};
this.showKey = function showKey() {
return secretKey;
};
this.getTs = function getTs() {
return ts;
};
}
module.exports = { Randomizer, Validator };