-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.js
More file actions
147 lines (126 loc) · 3.48 KB
/
utils.js
File metadata and controls
147 lines (126 loc) · 3.48 KB
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
import { default as Complex } from "./lib/complex.min.js";
import { default as aesjs } from "./lib/aes.js";
import { PRIV, PUBL } from "./input.js";
// Logger utilities.
const Log = {};
Log.DEBUG_MODE = false;
// info
Log.i = (...things) => {
if (Log.DEBUG_MODE) {
console.log(...things);
}
};
// errs
Log.e = (...things) => {
console.error(...things);
};
// Point is a vector in 2D space represented as a
// complex number for easier manipulation.
const Point = Complex;
class Polyline {
constructor(points) {
this.points = points;
}
static fromRawPoints = (rawPoints) => {
const points = [];
for (const p of rawPoints) {
points.push(new Point(p.x, p.y));
}
return new Polyline(points);
};
scale = (factor) => {
const points = [];
for (const p of this.points) {
points.push(p.mul(factor));
}
return new Polyline(points);
};
translate = (x, y) => {
const t = new Point(x, y);
const points = [];
for (const p of this.points) {
points.push(p.add(t));
}
return new Polyline(points);
};
// compute center.
avg = () => {
let [reMin, reMax, imMin, imMax] = [100000, 0, 100000, 0];
for (const p of this.points) {
reMin = Math.min(reMin, p.re);
imMin = Math.min(imMin, p.im);
reMax = Math.max(reMax, p.re);
imMax = Math.max(imMax, p.im);
}
return new Point((reMin + reMax) / 2, (imMin, imMax) / 2);
};
}
class Locker {
static lock = (dataStr, key) => {
const keyArr = new TextEncoder().encode(key);
var textBytes = aesjs.utils.utf8.toBytes(dataStr);
const cnt = keyArr.reduce((a, b) => a + b, 0);
var aesCtr = new aesjs.ModeOfOperation.ctr(keyArr, new aesjs.Counter(cnt));
var encryptedBytes = aesCtr.encrypt(textBytes);
return aesjs.utils.hex.fromBytes(encryptedBytes);
};
static unlock = (garbage, key) => {
const keyArr = new TextEncoder().encode(key);
const cnt = keyArr.reduce((a, b) => a + b, 0);
const bytes = aesjs.utils.hex.toBytes(garbage);
const aesCtr = new aesjs.ModeOfOperation.ctr(
keyArr,
new aesjs.Counter(cnt)
);
const decryptedBytes = aesCtr.decrypt(bytes);
return aesjs.utils.utf8.fromBytes(decryptedBytes);
};
static mk = (key) => {
let small = "";
while (small.length < 16) {
small += key;
}
return small.slice(0, 16);
};
}
// Cache within a session
class Storage {
static set = (k, v) => {
sessionStorage.setItem(`key:${k}`, v);
};
static get = (k, defaultVal = undefined) => {
return sessionStorage.getItem(`key:${k}`) || defaultVal;
};
static rm = (k) => {
sessionStorage.removeItem(`key:${k}`);
};
}
const err = () => {
window.location.href = "https://www.google.com/search?q=sorry";
};
const param = (k, defaultFn = () => err()) => {
const searchParams = new URLSearchParams(window.location.search);
return searchParams.has(k) ? searchParams.get(k) : defaultFn();
};
// Resolve a input JSON based on URL params
const getInputJSON = () => {
const kind = param("k", () => "three");
Log.i("kind", kind);
if (kind in PRIV) {
const key = Storage.get(kind) || prompt("Enter key");
try {
Storage.set(kind, key);
return JSON.parse(Locker.unlock(PRIV[kind], Locker.mk(key)));
} catch (e) {
Storage.rm(kind);
Log.e(e);
err();
}
} else if (kind in PUBL) {
return JSON.parse(PUBL[kind]);
} else {
// TODO show rose URL
err();
}
};
export { Point, Log, Polyline, Locker, getInputJSON, param };