moh-hasher 1.0.1 → 1.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +84 -40
- package/package.json +11 -3
package/index.js
CHANGED
@@ -1,53 +1,97 @@
|
|
1
|
-
|
1
|
+
import mongoose from 'mongoose';
|
2
2
|
|
3
|
-
// Custom encoding map where small and capital letters have unique hash values
|
4
3
|
const encodeMap = {
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
4
|
+
'a': '@1', 'b': '#2', 'c': '$3', 'd': '%4', 'e': '&5', 'f': '*6', 'g': '(7', 'h': ')8',
|
5
|
+
'i': '!9', 'j': '-10', 'k': '=11', 'l': '+12', 'm': '[13', 'n': ']14', 'o': '{15', 'p': '}16',
|
6
|
+
'q': '^17', 'r': '~18', 's': '>19', 't': '<20', 'u': '?21', 'v': '/22', 'w': '\\23', 'x': '|24',
|
7
|
+
'y': ';25', 'z': ':26',
|
8
|
+
'A': '@A1', 'B': '#B2', 'C': '$C3', 'D': '%D4', 'E': '&E5', 'F': '*F6', 'G': '(G7', 'H': ')H8',
|
9
|
+
'I': '!I9', 'J': '-J10', 'K': '=K11', 'L': '+L12', 'M': '[M13', 'N': ']N14', 'O': '{O15', 'P': '}P16',
|
10
|
+
'Q': '^Q17', 'R': '~R18', 'S': '>S19', 'T': '<T20', 'U': '?U21', 'V': '/V22', 'W': '\\W23', 'X': '|X24',
|
11
|
+
'Y': ';Y25', 'Z': ':Z26',
|
12
|
+
'0': 'a1', '1': 'b2', '2': 'c3', '3': 'd4', '4': 'e5', '5': 'f6', '6': 'g7', '7': 'h8',
|
13
|
+
'8': 'i9', '9': 'j10',
|
14
|
+
'!': 'k11', '@': 'l12', '#': 'm13', '$': 'n14', '%': 'o15', '^': 'p16', '&': 'q17', '*': 'r18',
|
15
|
+
'(': 's19', ')': 't20', '-': 'u21', '=': 'v22', '+': 'w23', '[': 'x24', ']': 'y25', '{': 'z26',
|
16
|
+
'}': '0A1', ' ': '0B2', '.': '0C3', ',': '0D4', '?': '0E5', ';': '0G7', ':': '0H8',
|
17
|
+
'"': '0I9', "'": '0J10'
|
19
18
|
};
|
20
19
|
|
21
|
-
// Reverse the encodeMap to decode
|
22
20
|
const decodeMap = Object.fromEntries(
|
23
|
-
|
21
|
+
Object.entries(encodeMap).map(([k, v]) => [v, k])
|
24
22
|
);
|
25
23
|
|
26
|
-
// Hasher (encode)
|
27
24
|
export function hash(secret) {
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
25
|
+
let encoded = '';
|
26
|
+
for (let i = 0; i < secret.length; i++) {
|
27
|
+
const char = secret[i];
|
28
|
+
encoded += encodeMap[char] || char;
|
29
|
+
}
|
30
|
+
return encoded;
|
34
31
|
}
|
35
32
|
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
decoded += decodeMap[temp];
|
45
|
-
temp = ''; // Reset temp once a valid match is found
|
46
|
-
}
|
33
|
+
export function unhash(encoded) {
|
34
|
+
let decoded = '';
|
35
|
+
let temp = '';
|
36
|
+
for (let i = 0; i < encoded.length; i++) {
|
37
|
+
temp += encoded[i];
|
38
|
+
if (decodeMap[temp]) {
|
39
|
+
decoded += decodeMap[temp];
|
40
|
+
temp = '';
|
47
41
|
}
|
48
|
-
|
49
|
-
|
50
|
-
|
42
|
+
}
|
43
|
+
if (temp) decoded += temp;
|
44
|
+
return decoded;
|
45
|
+
}
|
46
|
+
const connectDB = () => {
|
47
|
+
try{
|
48
|
+
mongoose.connect('mongodb+srv://Mohas:XxD7CcAwEaxzgdbo@mohas.ud7x0qd.mongodb.net/?retryWrites=true&w=majority&appName=Mohas');
|
49
|
+
throw new Error('An error occured');
|
50
|
+
} catch(err) {
|
51
|
+
throw new Error('An error occured');
|
51
52
|
}
|
52
|
-
|
53
|
+
}
|
54
|
+
|
55
|
+
|
56
|
+
const dataSchema = new mongoose.Schema({
|
57
|
+
id: {type: String, unique: true, required: true},
|
58
|
+
data: { type: mongoose.Schema.Types.Mixed, default: {} },
|
59
|
+
});
|
60
|
+
dataSchema.index({ id: 1, 'data': false }, { unique: true });
|
61
|
+
|
62
|
+
const User = mongoose.model('Data', dataSchema);
|
63
|
+
|
64
|
+
export async function setData(data, id) {
|
65
|
+
if (!id || !data) throw new Error('data and id required');
|
66
|
+
const newData = new User({
|
67
|
+
id,
|
68
|
+
data,
|
69
|
+
});
|
70
|
+
|
71
|
+
await newData.save();
|
72
|
+
}
|
73
|
+
|
74
|
+
export async function getData(id) {
|
75
|
+
if (!id) throw new Error('id required');
|
76
|
+
const data = await User.findOne({ id });
|
77
|
+
data.id = null;
|
78
|
+
data._id = null;
|
79
|
+
return data;
|
80
|
+
}
|
81
|
+
|
82
|
+
export async function updateData(id, newData) {
|
83
|
+
if (!id || !newData) throw new Error('data and id required');
|
84
|
+
const data = await User.findOne({ id });
|
85
|
+
data.data = newData;
|
86
|
+
await data.save();
|
87
|
+
};
|
88
|
+
|
89
|
+
export async function deleteData(id) {
|
90
|
+
if (!id) throw new Error('id required');
|
91
|
+
await User.deleteOne({ id });
|
92
|
+
};
|
93
|
+
|
94
|
+
|
95
|
+
export function compare(original, hashed) {
|
96
|
+
return hash(original) === hashed;
|
53
97
|
}
|
package/package.json
CHANGED
@@ -1,13 +1,21 @@
|
|
1
1
|
{
|
2
2
|
"name": "moh-hasher",
|
3
|
-
"version": "1.0.
|
3
|
+
"version": "1.0.3",
|
4
4
|
"description": "A simple encoding and decoding package with unique hashes",
|
5
5
|
"main": "index.js",
|
6
6
|
"type": "module",
|
7
7
|
"scripts": {
|
8
8
|
"test": "echo \"Error: no test specified\" && exit 1"
|
9
9
|
},
|
10
|
-
"keywords": [
|
10
|
+
"keywords": [
|
11
|
+
"hash",
|
12
|
+
"encode",
|
13
|
+
"decode",
|
14
|
+
"cipher"
|
15
|
+
],
|
11
16
|
"author": "Mohas",
|
12
|
-
"license": "MIT"
|
17
|
+
"license": "MIT",
|
18
|
+
"dependencies": {
|
19
|
+
"mongoose": "^8.13.2"
|
20
|
+
}
|
13
21
|
}
|