-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
125 lines (122 loc) · 4.76 KB
/
index.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
const data = function () {
const characters = [...Array(26)].map((_, i) => String.fromCharCode(i + 65).toLowerCase());
const numbers = [...Array(10)].map((_, i) => i.toString());
const specialCharacters = '!@#$%^&*[]{}()₹?-+_';
const characterArray = characters.concat(characters.map(c => c.toUpperCase()), numbers, specialCharacters.split(''));
return characterArray;
}
const getnewString = function (string) { // for old to new
const Length = string.length;
let newString = '';
for (let i = 0; i < string.length; i++) {
let charCode = string.charCodeAt(i);
charCode += Length;
newString += String.fromCharCode(charCode);
}
return newString;
}
const concat = function (random, text, hashed) { // for a unique pass
const Length = random.length + text.length + hashed.length;
let s = '';
for (let i = 0; i < Length; i++) {
s = s + random.charAt(i);
s = s + text.charAt(i);
s = s + hashed.charAt(i);
}
return s;
}
const getRandomNumber = function (from, to) { // for random number
const randomNumber = Math.floor(Math.random() * (to - from + 1)) + from;
return randomNumber;
}
/**
* The function `getSalt` generates a random string of characters of length `number` using a
* character array.
* @param number - The `number` parameter is the length of the salt that you want to generate. It
* should be a positive integer.
* @returns The function `getSalt` returns a string `S` which is generated by randomly selecting
* characters from a character array.
*/
function getSalt(Number) { // for generate salt string
let S = '';
const Data = data();
if (typeof(Number) !== 'number'){
throw new Error(`Invalid data type: expected agrueement to be number, got ${typeof(Number)}`);
}
else if (0 >= Number) {
throw new Error('Invalid range: arguement must be more than 0');
}
for (let i = 0; i < Number; i++) {
const num = getRandomNumber(0, Data.length - 1);
S = S + Data[num];
}
return S;
}
/**
* The function `getString` generates a unique string by concatenating a random string, the original
* string, and a salt value.
* @param Text - The "Text" parameter is a string that represents the input text for generating a
* unique string.
* @param getSalt - The `getSalt` parameter is a salt that is used to generate a unique string. It is a
* string value that is concatenated with the generated random string and the original string to create
* a new string.
* @access - `string` should be greater than 0 and less than 13.
* @returns a new string that is generated by concatenating a random string, the old string, and a
* salt.
*/
function getString(Text, getSalt) { // for generate unique string
if (Text === undefined) {
throw new Error('getSalt should be a salt!');
}
else if (getSalt === undefined) {
throw new Error('getSalt should be a salt!');
}
else if (typeof(Text) !== 'string') {
throw new Error(`Invalid data type: expected agrueement to be string, got ${typeof(Text)}`);
}
else if(Text.length > 12 || Text.length <= 0){
throw new Error('Restricted: Error at .match() "string" should be greater than 0 and less than 13.' )
}
const length = Text.length;
const saltLength = getSalt.length;
let RandomString = '';
const Char = data();
for (let i = 0; i < Text.length; i++) {
RandomString = RandomString + Char[getRandomNumber(0, Char.length - 1)] // Random string baked!
}
const oldString = getnewString(Text);
const newString = concat(RandomString, oldString, getSalt);
return newString;
}
function inspect(string, hash,salt){
const newsalt = getSalt(salt);
const newStr = getString(string,newsalt);
for (let i = 1; i < string.length*3; i+=3) {
if(newStr.charAt(i) != hash.charAt(i)){
return false;
}
}
return true;
}
/**
* the function `match` used to compare two strings.
* @param string - The `string` parameter is the original string.
* @param salt - The number of salt used to encrypt text.
* @param hash - The `hash` parameter is the string to be compared with.
* @returns the function `match` returns the boolean expression `true` or `false`.
* If the both string are matched it will return `true` else `false`.
*/
function match(string, hash,salt) {
if(string.length > 12 || string.length <= 0){
throw new Error('Restricted: Error at .match() "string" should be greater than 0 and less than 13.' )
}
if (typeof string !== 'string' || typeof hash !== 'string') {
throw new Error('Invalid input: "string" and "hash" should be strings');
}
return inspect(string,hash,salt);
}
module.exports = {
getSalt:getSalt,
getString:getString,
match:match
};