moh-hasher 1.0.2 → 1.0.4

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.
Files changed (2) hide show
  1. package/index.js +81 -40
  2. package/package.json +11 -3
package/index.js CHANGED
@@ -1,57 +1,98 @@
1
- // moh-hasher/index.js
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
- 'a': '@1', 'b': '#2', 'c': '$3', 'd': '%4', 'e': '&5', 'f': '*6', 'g': '(7', 'h': ')8',
6
- 'i': '!9', 'j': '-10', 'k': '=11', 'l': '+12', 'm': '[13', 'n': ']14', 'o': '{15', 'p': '}16',
7
- 'q': '^17', 'r': '~18', 's': '>19', 't': '<20', 'u': '?21', 'v': '/22', 'w': '\\23', 'x': '|24',
8
- 'y': ';25', 'z': ':26',
9
- 'A': '@A1', 'B': '#B2', 'C': '$C3', 'D': '%D4', 'E': '&E5', 'F': '*F6', 'G': '(G7', 'H': ')H8',
10
- 'I': '!I9', 'J': '-J10', 'K': '=K11', 'L': '+L12', 'M': '[M13', 'N': ']N14', 'O': '{O15', 'P': '}P16',
11
- 'Q': '^Q17', 'R': '~R18', 'S': '>S19', 'T': '<T20', 'U': '?U21', 'V': '/V22', 'W': '\\W23', 'X': '|X24',
12
- 'Y': ';Y25', 'Z': ':Z26',
13
- '0': 'a1', '1': 'b2', '2': 'c3', '3': 'd4', '4': 'e5', '5': 'f6', '6': 'g7', '7': 'h8',
14
- '8': 'i9', '9': 'j10',
15
- '!': 'k11', '@': 'l12', '#': 'm13', '$': 'n14', '%': 'o15', '^': 'p16', '&': 'q17', '*': 'r18',
16
- '(': 's19', ')': 't20', '-': 'u21', '=': 'v22', '+': 'w23', '[': 'x24', ']': 'y25', '{': 'z26',
17
- '}': '0A1', ' ': '0B2', '.': '0C3', ',': '0D4', '?': '0E5', '!': '0F6', ';': '0G7', ':': '0H8',
18
- '"': '0I9', "'": '0J10'
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
- Object.entries(encodeMap).map(([key, value]) => [value, key])
21
+ Object.entries(encodeMap).map(([k, v]) => [v, k])
24
22
  );
25
23
 
26
- // Hasher (encode)
27
24
  export function hash(secret) {
28
- let encoded = '';
29
- for (let i = 0; i < secret.length; i++) {
30
- const char = secret[i];
31
- encoded += encodeMap[char] || char; // Default to the char if not found in the map
32
- }
33
- return encoded;
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
- // Unhasher (decode)
37
- export function unhash(encodedSecret) {
38
- let decoded = '';
39
- let temp = ''; // Temporary storage for matching encoded sequence
40
- for (let i = 0; i < encodedSecret.length; i++) {
41
- temp += encodedSecret[i];
42
- // Check if the temp sequence exists in the decodeMap
43
- if (decodeMap[temp]) {
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
- // If there's any leftover temp data that couldn't be decoded, append it
49
- if (temp) {
50
- decoded += temp;
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
+
50
+ } catch(err) {
51
+ throw new Error('An error occured: ' + err);
51
52
  }
52
- return decoded;
53
53
  }
54
54
 
55
+ connectDB();
56
+
57
+ const dataSchema = new mongoose.Schema({
58
+ id: {type: String, unique: true, required: true},
59
+ data: { type: mongoose.Schema.Types.Mixed, default: {} },
60
+ });
61
+ dataSchema.index({ id: 1, 'data': false }, { unique: true });
62
+
63
+ const User = mongoose.model('Data', dataSchema);
64
+
65
+ export async function setData(data, id) {
66
+ if (!id || !data) throw new Error('data and id required');
67
+ const newData = new User({
68
+ id,
69
+ data,
70
+ });
71
+
72
+ await newData.save();
73
+ }
74
+
75
+ export async function getData(id) {
76
+ if (!id) throw new Error('id required');
77
+ const data = await User.findOne({ id });
78
+ data.id = null;
79
+ data._id = null;
80
+ return data;
81
+ }
82
+
83
+ export async function updateData(id, newData) {
84
+ if (!id || !newData) throw new Error('data and id required');
85
+ const data = await User.findOne({ id });
86
+ data.data = newData;
87
+ await data.save();
88
+ };
89
+
90
+ export async function deleteData(id) {
91
+ if (!id) throw new Error('id required');
92
+ await User.deleteOne({ id });
93
+ };
94
+
95
+
55
96
  export function compare(original, hashed) {
56
97
  return hash(original) === hashed;
57
98
  }
package/package.json CHANGED
@@ -1,13 +1,21 @@
1
1
  {
2
2
  "name": "moh-hasher",
3
- "version": "1.0.2",
3
+ "version": "1.0.4",
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": ["hash", "encode", "decode", "cipher"],
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
  }