-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvalidate.js
More file actions
72 lines (61 loc) · 1.46 KB
/
validate.js
File metadata and controls
72 lines (61 loc) · 1.46 KB
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
/**
* Check for valid key
*
* @param {number} key Shift key
*/
function ensureValidKey(key) {
if (key === undefined || key === null) {
throw new Error("Key is required");
}
if (typeof key !== "number" || !Number.isInteger(key)) {
throw new Error("Key should be an integer");
}
if (key < 0 || 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.isBuffer(buffer) || buffer.length === 0) {
throw new Error("Buffer is invalid");
}
if (buffer.length > 1000) {
throw new Error(
"Input too large, use EncryptTransform / DecryptTransform instead"
);
}
ensureValidKey(key);
}
module.exports = {
ensureValidKey,
ensureValidForString,
ensureValidForBuffer,
};