forked from smogon/pokemon-showdown
-
Notifications
You must be signed in to change notification settings - Fork 6
/
verifier.js
49 lines (38 loc) · 1.23 KB
/
verifier.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
// Because I don't want two files, we're going to fork ourselves.
// Node.js's crypto functions are synchronous, strangely, unlike the rest of Node
// here's an asynchronous implementation.
if (!process.send) {
// This is the parent
var guid = 1;
var callbacks = {};
var callbackData = {};
var child = require('child_process').fork('verifier.js');
exports.verify = function(data, signature, callback) {
var localGuid = guid++;
callbacks[localGuid] = callback;
callbackData[localGuid] = data;
child.send({data: data, sig: signature, guid: localGuid});
}
child.on('message', function(response) {
if (callbacks[response.guid]) {
callbacks[response.guid](response.success, callbackData[response.guid]);
delete callbacks[response.guid];
delete callbackData[response.guid];
}
});
} else {
// This is the child
var config = require('./config/config.js');
var crypto = require('crypto');
var keyalgo = config.loginserverkeyalgo;
var pkey = config.loginserverpublickey;
process.on('message', function(message) {
var verifier = crypto.createVerify(keyalgo);
verifier.update(message.data);
var success = verifier.verify(pkey, message.sig, 'hex');
process.send({
success: success,
guid: message.guid
});
});
}