-
Notifications
You must be signed in to change notification settings - Fork 0
/
Testudo.js
85 lines (69 loc) · 2.19 KB
/
Testudo.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/*
* testudo
* https://github.com/hameddanesh/testudo
*
* Copyright 2024, Hamed Danesh
*
* Licensed under the MIT license:
* https://opensource.org/license/mit/
*
*/
let testudo = function () {
if (this.__proto__.constructor !== testudo) {
return new testudo();
}
this.form = function (value, valueHash) {
let seed = this.getSeed(valueHash, value)
return this.xor(this.textToBinary(value), seed)
}
this.unform = function (secret, seed) {
return this.binaryToText(this.xor(secret, seed))
}
this.textToBinary = function (text) {
let length = text.length,
output = [];
for (let i = 0; i < length; i++) {
let binary = text[i].charCodeAt().toString(2);
output.push(Array(8 - binary.length + 1).join("0") + binary);
}
return output.join("");
}
this.binaryToText = function (binaryString) {
let _binaryString = "",
counter = 0
for (let i = 0; i < binaryString.length; i++) {
counter++
_binaryString += binaryString[i]
if (counter >= 8) {
_binaryString += " "
counter = 0
}
}
_binaryString = _binaryString.substring(0, (_binaryString.length - 1))
return _binaryString.split(" ").map(letter => String.fromCharCode(parseInt(letter, 2))).join('')
}
this.getSeed = function (hashBinary, valueBinary) {
let rep = parseInt(valueBinary.length / hashBinary.length) + 1,
temp = ''
for (let i = 0; i < rep; i++)
temp += hashBinary
hashBinary = temp
return this.textToBinary(hashBinary).substring(0, this.textToBinary(valueBinary).length)
}
this.xor = function (binaryString, seed) {
let xString = '',
counter = 0
for (let i = 0; i < binaryString.length; i++) {
counter++
if (binaryString[i] != seed[i])
xString += '1'
else
xString += '0'
if (counter >= 8) {
xString += ""
counter = 0
}
}
return xString
}
}