-
Notifications
You must be signed in to change notification settings - Fork 0
/
enc.browser.js
197 lines (197 loc) · 4.65 KB
/
enc.browser.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
// Copyright (C) 2024 Code Eleven (Mustafa E / @codeeleven0)
// Hashing secrets are not for decoding. It protects hash collision.
const chs = 32;
const isEven = (x) => {
return (x % 2) == 0;
}
const len = (x) => {
return x.length;
}
const sum = (x) => {
s = 0;
x.forEach(h => s+=h);
return s;
}
const div = (x, y) => {
return Math.round(x / y);
}
const ord = (x) => {
return x.charCodeAt(0);
}
const chr = (x) => {
return String.fromCharCode(x);
}
const max = (x) => {
maxx = Number.MIN_VALUE;
for(let i = 0; i < len(x); i++){
if (x[i] > maxx){
maxx = x[i];
}
}
return maxx;
}
const min = (x) => {
minn = Number.MAX_VALUE;
for(let i = 0; i < len(x); i++){
if (minn > x[i]){
minn = x[i];
}
}
return minn;
}
const findall = (pat, str) => {
a = new RegExp(pat, "g");
return str.match(a);
}
const strby = (s, by) => {
ns = [];
for(let i = 0; i < by; i++){
ns.push(s)
}
return ns.join("");
}
const ljust = (str, times, d) => {
ln = len(str);
while(ln < times){
str += d;
ln = len(str);
if (ln == times){
break;
}
}
return str;
}
const reversearr = (arr) => {
return arr.reverse();
}
const calcAvg = (x) => {
return div(sum(x), len(x));
}
const splitstring = (x) => {
even = ""
odd = ""
for(let i = 0; i < len(x); i++){
if (isEven(i)){
even += x[i];
}else{
odd += x[i];
}
}
return [even, odd];
}
const applysecret = (x, secret) => {
splitted = splitstring(secret);
encoded = [splitted[1] + x[0], splitted[0] + x[1]];
return encoded;
}
const calculateintegrity = (x) => {
s = x.join("");
arr = [];
for(let i = 0; i < len(s); i++){
arr.push(s.charCodeAt(i));
}
if (arr){
sum1 = max(arr);
sum2 = min(arr);
sum3 = calcAvg(arr);
chrs = "";
for(let j = 33; j < 126; j++){
chrs += chr(j);
}
if (chrs.includes(chr(sum3))){
return [sum1, sum2, sum3];
}else{
return [sum1, sum2, arr[0]];
}
}else{
return [65, 66, 67];
}
}
const makeintegrity = (x) => {
s = "";
for(let j = 0; j < len(x); j++){
s += chr(x[j]);
}
return s;
}
const makeend = (x) => {
return x[0] + x[1];
}
const hashit = (x, secret) => {
splitted = splitstring(x);
encoded = applysecret(splitted, secret);
integrity = calculateintegrity(encoded);
integritystr = makeintegrity(integrity);
hashstr = makeend(encoded);
lastsplit = splitstring(hashstr + integritystr);
hashstr2 = makeend(lastsplit);
nstr = hashstr2.replaceAll(" ","*");
n = "";
if (len(nstr) < chs-1){
n = nstr;
}else{
a = findall(strby(".",div(len(nstr), chs-1)), nstr);
for(let s = 0; s < len(a); s++){
ac = [];
for (let i = 0; i < len(a[s]); i++){
ac.push(ord(a[s][i]));
n += chr(div(sum(ac), len(ac)));
}
}
}
if (len(n) < chs-1){
try{
c = makeintegrity(calculateintegrity(splitstring(secret)))
n = ljust(n, chs-1, chr(c.charCodeAt(c.length-1)));
}catch(e){
n = ljust(n, div(chs, 2), integritystr[-1]),
n = ljust(n, div(chs, 2) + div(chs, 3), integritystr[0]),
n = ljust(n, chs-1, integritystr[1]);
}
}
if (len(n) > chs-1){
n = findall(strby(".",chs-1), n)[0];
}
try{
end = makeend(reversearr(splitstring(n))) + chr(secret.charCodeAt(secret.length-1));
}catch(x){
end = makeend(reversearr(splitstring(n))) + chr(nstr.charCodeAt(nstr.length-1));
}
return end;
}
const _passwhash = (x, secret) => {
try{
return hashit(x, secret);
}catch(e0){
try{
console.error(e0);
return hashit("", secret);
}catch(e1){
try{
console.error(e1);
return hashit(x, "");
}catch(e2){
console.error(e2);
return strby("0", chs);
}
}
}
}
const __passwhash = (x, secret) => {
hr = _passwhash(x, secret).replaceAll(" ","*").replaceAll("\n","").replaceAll("\r","").replaceAll("\0","");
if(len(hr) != chs){
hr = ljust(hr, chs, hr[hr.length-1]);
}
return hr;
}
const toHex = (s) => {
summ = "";
for(let i = 0; i < len(s); i++){
summ += div(s.charCodeAt(i), 1).toString(16);
}
return summ;
}
const passwhash = (data, secret) => {
hashs = toHex(__passwhash(data, secret));
return hashs;
}