-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.js
81 lines (72 loc) · 3.04 KB
/
common.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
/****************************************************************************
* Copyright 2010,2011 Benjamin Kellermann *
* *
* This file is part of dudle. *
* *
* Dudle is free software: you can redistribute it and/or modify it under *
* the terms of the GNU Affero General Public License as published by *
* the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* Dudle is distributed in the hope that it will be useful, but WITHOUT ANY *
* WARRANTY; without even the implied warranty of MERCHANTABILITY or *
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public *
* License for more details. *
* *
* You should have received a copy of the GNU Affero General Public License *
* along with dudle. If not, see <http://www.gnu.org/licenses/>. *
***************************************************************************/
"use strict";
// Register Namespace
if (typeof(Asymcrypt) === "undefined") {
var Asymcrypt = {};
} else {
alert("Somebody captured the Namespace Asymcrypt!!!");
}
Asymcrypt.initiator = {};
Asymcrypt.setInitiator = function (publicKey) {
var ini = {}, to, i, niceFingerprint;
ini.pubkey = new openpgp.key.readArmored(publicKey);
ini.fingerprint = ini.pubkey.keys[0].primaryKey.fingerprint.toUpperCase();
to = ini.fingerprint.length;
niceFingerprint = "";
for (i = 0; i < to; i++) {
niceFingerprint += ini.fingerprint[i];
if ((i + 1) % 4 === 0 && (i + 1) !== to) {
niceFingerprint += ' ';
}
}
ini.fingerprint = niceFingerprint;
ini.user = ini.pubkey.keys[0].users[0].userId.userid;
ini.name = $('<div/>').text(ini.user.replace(/ <.*>/g, '')).html();
ini.mail = ini.user.replace(/^[^<]*</, '').replace(/>/, "");
Asymcrypt.initiator = ini;
};
Asymcrypt.encrypt = function (plain, readyfunc) {
openpgp.encrypt({
publicKeys: Asymcrypt.initiator.pubkey.keys,
data: plain,
armor: true
}).then(function(enc){
readyfunc(enc.data);
});
};
Asymcrypt.init = function () {
var successfunc = {}, options = arguments[0] || {};
if (options.success) {
successfunc = options.success;
}
options.success = function (publicKey) {
Asymcrypt.setInitiator(JSON.parse(publicKey));
successfunc();
};
Poll.load("Asymcrypt", "initiator", options);
};
Asymcrypt.pwgen = function (num) {
var i, ret = "", keyspace = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
for (i = 0; i < num; i++) {
ret += keyspace[openpgp.crypto.random.getSecureRandom(0,keyspace.length-1)];
}
return ret;
}