-
Notifications
You must be signed in to change notification settings - Fork 6
/
facade-high-level.js
81 lines (78 loc) · 2.45 KB
/
facade-high-level.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
(function () {
function hashGuard(M, r) {
return (Array.prototype.slice.call(arguments).length === 2
&& Array.isArray(M)
&& M.length > 0
&& typeof r === 'number'
&& r > 0);
}
function encryptGuard(K, M) {
return (Array.prototype.slice.call(arguments).length === 2
&& Array.isArray(K)
&& K.length > 0
&& Array.isArray(M)
&& M.length > 0);
}
function decryptGuard(K, C) {
return (Array.prototype.slice.call(arguments).length === 2
&& Array.isArray(K)
&& K.length > 0
&& Array.isArray(C)
&& C.length > 0);
}
function encryptWithIVGuard(K, IV, M) {
return (Array.prototype.slice.call(arguments).length === 3
&& Array.isArray(K)
&& K.length > 0
&& Array.isArray(IV)
&& IV.length > 0
&& Array.isArray(M)
&& M.length > 0);
}
function decryptWithIVGuard(K, IV, C) {
return (Array.prototype.slice.call(arguments).length === 3
&& Array.isArray(K)
&& K.length > 0
&& Array.isArray(IV)
&& IV.length > 0
&& Array.isArray(C)
&& C.length > 0);
}
function domHashGuard(J, M, r) {
return (Array.prototype.slice.call(arguments).length === 3
&& J.length > 0
&& Array.isArray(J)
&& M.length > 0
&& typeof r === 'number'
&& r > 0);
}
/* TODO: whilst domHash domain is possibly less stringent we may wish to
assert K length for mac */
function macGuard(K, M, r) {
return (Array.prototype.slice.call(arguments).length === 3
&& K.length > 0
&& Array.isArray(K)
&& M.length > 0
&& typeof r === 'number'
&& r > 0);
}
var facadeHighLevel = {
hash: hashGuard
, encrypt: encryptGuard
, decrypt: decryptGuard
, encryptWithIV: encryptWithIVGuard
, decryptWithIV: decryptWithIVGuard
, domHash: domHashGuard
, mac: macGuard
};
if(typeof module !== 'undefined' && module.exports){
/* export in CommonJS style */
module.exports = facadeHighLevel;
} else if (typeof window !== 'undefined' && !window.facadeHighLevel) {
/* augment window object */
window.facadeHighLevel = facadeHighLevel;
} else {
/* ...requires consumer-thought */
if (typeof console !== 'undefined') console.error("unable to export facadeHighLevel!");
}
}());