forked from timotejroiko/zlib-benchmark
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.js
More file actions
84 lines (69 loc) · 1.83 KB
/
functions.js
File metadata and controls
84 lines (69 loc) · 1.83 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
"use strict";
const chars = "1234567890 abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ";
function dev(avg, array) {
return Math.sqrt(array.map(t => (t - avg) ** 2).reduce((a, t) => a + t, 0) / array.length);
}
function randomChar() {
return chars[Math.floor(Math.random() * chars.length)];
}
function r(obj) {
const keys = Object.keys(obj);
for(const key of keys) {
switch(typeof obj[key]) {
case "object":
r(obj[key]);
break;
case "number":
obj[key] = Math.random();
break;
case "string":
obj[key] = new Array(Math.round(obj[key].length * (Math.random() * 3))).fill().map(randomChar).join("");
break;
}
}
}
function randomize(data) {
try {
const o = JSON.parse(data);
r(o);
return JSON.stringify(o);
} catch(e) {
return data;
}
}
function randomizeBuffer(data) {
return Buffer.from(randomize(data));
}
function randomizeCrypto(data, needUint8 = false) {
const MIN_SIZE = 64 // 32 bytes key + 32 bytes iv
data = randomize(data)
const padSize = 16 - (data.length % 16);
let buf = Buffer.allocUnsafe(Math.max(data.length + padSize, MIN_SIZE));
buf.set(Buffer.from(data));
if (needUint8) {
buf = new Uint8Array(buf.buffer, buf.byteOffset, buf.byteLength)
}
const key = buf.slice(0, 32);
const iv = buf.slice(32, 64);
return [buf, key, iv];
}
function randomizeCryptoCtr(data, needUint8 = false) {
const MIN_SIZE = 48 // 32 bytes key + 32 bytes iv
data = randomize(data)
const padSize = 16 - (data.length % 16);
let buf = Buffer.allocUnsafe(Math.max(data.length + padSize, MIN_SIZE));
buf.set(Buffer.from(data));
if (needUint8) {
buf = new Uint8Array(buf.buffer, buf.byteOffset, buf.byteLength)
}
const key = buf.slice(0, 32);
const iv = buf.slice(32, 48);
return [buf, key, iv];
}
module.exports = {
dev,
randomize,
randomizeBuffer,
randomizeCrypto,
randomizeCryptoCtr,
};