-
Notifications
You must be signed in to change notification settings - Fork 1
/
validate.js
66 lines (56 loc) · 1.45 KB
/
validate.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
/**
* Check for valid key
*
* @param {number} key Shift key
*/
function ensureValidKey(key) {
if (!key) {
throw new Error("Key is required");
}
if (typeof key !== 'number') {
throw new Error("Key should be a number")
}
if (0 > key || key > 25) {
throw new Error("Key should be within the range of 0 - 25");
}
}
/**
* Validation for encryptString and decryptString
*
* @param {String} str String input value
* @param {number} key Shift key
*/
function ensureValidForString (str, key) {
if (!str) {
throw new Error("Str is required");
}
if (typeof str !== 'string' || str.length === 0) {
throw new Error("Str is invalid");
}
if (str.length > 1000) {
throw new Error("Input too large, use EncryptTransform / DecryptTransform instead");
}
ensureValidKey(key);
}
/**
* Validation for encrypt and decrypt functions
*
* @param {Buffer} buffer Buffer input value
* @param {number} key Shift key
*/
function ensureValidForBuffer (buffer, key) {
if (!buffer) {
throw new Error("Buffer is required");
}
if (!(buffer instanceof Buffer) || buffer.length === 0) {
throw new Error("Buffer is invalid");
}
if (buffer.length > 1000) {
throw new Error("Input too large, use EncryptTransform / DecryptTransform instead");
}
}
module.exports = {
ensureValidKey,
ensureValidForString,
ensureValidForBuffer
}