-
Notifications
You must be signed in to change notification settings - Fork 0
/
locker.js
58 lines (51 loc) · 2.07 KB
/
locker.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
(function ($, $$, $$$) {
"use strict";
var _dropPrivateKey = function (foreignProfile) {
delete(foreignProfile.privateKey);
}
, _decodePublicKey = function (foreignProfile) {
foreignProfile.publicKey = $.Crypt.Coder.decode(foreignProfile.publicKey);
}
, _decryptContactKeys = function (foreignProfile, profile) {
$.Iterator.each(foreignProfile.contacts, function (contactId, container) {
if (profile && profile.id && profile.id === contactId) {
container.keys =
$.Crypt.Asymmetric.Object.decrypt(
$.Crypt.Coder.decode(container.keys), profile.publicKey, profile.privateKey);
$.Iterator.each(container.keys, function (id, key) {
container.keys[id] = $.Crypt.Coder.decode(key);
});
} else {
delete(container.keys);
delete(container.sections);
}
});
}
, _decryptSections = function (foreignProfile, profile) {
if (profile && profile.id) {
var keys = foreignProfile.contacts[profile.id] ? foreignProfile.contacts[profile.id].keys : undefined;
$.Iterator.each(foreignProfile.sections, function (id, encryptedSection) {
if ($.Collection.include($.Sections.openIds, id)) {
foreignProfile.sections[id] = encryptedSection;
} else {
var key = keys ? keys[id] : undefined;
if (key) {
foreignProfile.sections[id] = $.Crypt.Symmetric.Object.decrypt($.Crypt.Coder.decode(encryptedSection), key);
} else {
delete(foreignProfile.sections[id]);
}
}
});
}
};
$$$.unlock = function (foreignProfile, profile) {
var result = $.Object.clone(foreignProfile);
_dropPrivateKey(result);
_decodePublicKey(result);
_decryptContactKeys(result, profile);
_decryptSections(result, profile);
return result;
};
})(epdRoot,
epdRoot.Foreign = epdRoot.Foreign || { },
epdRoot.Foreign.Locker = epdRoot.Foreign.Locker || { });