-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsjclWorkerInclude.js
184 lines (156 loc) · 4.04 KB
/
sjclWorkerInclude.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
define(["cryptoWorker/generalWorkerInclude", "cryptoWorker/minimalHelper"], function (WorkerManager, chelper) {
"use strict";
var entropyAvailable = true;
function getEntropy() {
try {
var ab;
// get cryptographically strong entropy depending on runtime environment
if (window && Uint32Array) {
ab = new Uint32Array(32);
if (window.crypto && window.crypto.getRandomValues) {
window.crypto.getRandomValues(ab);
} else if (window.msCrypto && window.msCrypto.getRandomValues) {
window.msCrypto.getRandomValues(ab);
} else {
return false;
}
// get cryptographically strong entropy in Webkit
return ab;
}
} catch (e) {}
return false;
}
var addEntropy = {
setup: function (theWorker, callback) {
var entropy = getEntropy();
if (entropy) {
theWorker.postMessage({randomNumber: entropy, entropy: 1024}, callback);
} else {
entropyAvailable = false;
}
}
};
var workers = new WorkerManager("cryptoWorker/sjclWorker", 4, addEntropy);
var sjclWorker = {
hash: function (toHash, callback) {
workers.getFreeWorker(function (err, worker) {
var data = {
isHash: true,
toHash: toHash
};
worker.postMessage(data, callback);
});
},
asym: {
generateCryptKey: function (curve, callback) {
if (typeof curve === "function") {
callback = curve;
curve = undefined;
}
workers.getFreeWorker(function (err, worker) {
var data = {
asym: true,
generate: true,
crypt: true
};
if (curve) {
data.curve = curve;
}
worker.postMessage(data, callback);
});
},
generateSignKey: function (callback, curve) {
workers.getFreeWorker(function (err, worker) {
var data = {
asym: true,
generate: true,
crypt: false,
curve: curve
};
worker.postMessage(data, callback);
});
},
kem: function (publicKey, callback) {
workers.getFreeWorker(function (err, worker) {
var data = {
asym: true,
generate: false,
action: "kem",
curve: chelper.getCurveName(publicKey._curve),
x: publicKey._point.x.toString(),
y: publicKey._point.y.toString()
};
worker.postMessage(data, callback);
});
},
unkem: function (privateKey, tag, callback) {
workers.getFreeWorker(function (err, worker) {
var data = {
asym: true,
generate: false,
action: "unkem",
curve: chelper.getCurveName(privateKey._curve),
exponent: privateKey._exponent.toString(),
tag: chelper.bits2hex(tag)
};
worker.postMessage(data, callback);
});
},
sign: function (privateKey, toSign, callback) {
workers.getFreeWorker(function (err, worker) {
var data = {
asym: true,
generate: false,
action: "sign",
curve: chelper.getCurveName(privateKey._curve),
exponent: privateKey._exponent.toString(),
toSign: chelper.bits2hex(toSign)
};
worker.postMessage(data, callback);
});
},
verify: function (publicKey, signature, hash, callback) {
workers.getFreeWorker(function (err, worker) {
var data = {
asym: true,
generate: false,
action: "verify",
curve: chelper.getCurveName(publicKey._curve),
point: {
x: publicKey._point.x.toString(),
y: publicKey._point.y.toString()
},
signature: chelper.bits2hex(signature),
hash: chelper.bits2hex(hash)
};
worker.postMessage(data, callback);
});
}
},
sym: {
encrypt: function (key, message, callback) {
workers.getFreeWorker(function (err, worker) {
var data = {
"key": key,
"message": message,
"asym": false,
"encrypt": true
};
worker.postMessage(data, callback);
});
},
decrypt: function (key, message, callback) {
workers.getFreeWorker(function (err, worker) {
var data = {
"key": key,
"message": message,
"asym": false,
"encrypt": false
};
worker.postMessage(data, callback);
});
}
}
};
return sjclWorker;
});