-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprotect.h
More file actions
126 lines (104 loc) · 3.26 KB
/
protect.h
File metadata and controls
126 lines (104 loc) · 3.26 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
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
126
#ifndef PROTECT_H
#define PROTECT_H
#include <WiFi.h>
#include <EEPROM.h>
#include <MD5Builder.h>
// Define the EEPROM size
#define EEPROM_SIZE 512
#define HASH_ADDRESS 0 // EEPROM address where hash is stored
// Define the length of the MD5 hash (32 characters)
#define HASH_LENGTH 32
// SALT
const String SALT = "SALTYFISH";
// Function to get the MAC address as a string without delimiters
String getMACAddress() {
uint8_t mac[6];
WiFi.macAddress(mac);
String macStr;
for (int i = 0; i < 6; i++) {
if (mac[i] < 16) macStr += "0"; // Pad with zero
macStr += String(mac[i], HEX);
}
macStr.toUpperCase();
return macStr;
}
// Function to perform double MD5 hashing
String doubleMD5(String data) {
// First MD5 hash
MD5Builder md5First;
md5First.begin();
md5First.add(data);
md5First.calculate();
String firstHash = md5First.toString();
// Second MD5 hash
MD5Builder md5Second;
md5Second.begin();
md5Second.add(firstHash);
md5Second.calculate();
return md5Second.toString();
}
// Function to read the hash from EEPROM
String readHashFromEEPROM() {
char hashString[HASH_LENGTH + 1];
EEPROM.get(HASH_ADDRESS, hashString);
hashString[HASH_LENGTH] = '\0'; // Ensure null-termination
return String(hashString);
}
// Function to write the hash to EEPROM
void writeHashToEEPROM(String hash) {
char hashBuffer[HASH_LENGTH + 1];
hash.toCharArray(hashBuffer, HASH_LENGTH + 1);
EEPROM.put(HASH_ADDRESS, hashBuffer);
EEPROM.commit(); // Ensure changes are written to EEPROM
}
// Function to prompt for hash input
void promptForHash() {
String macAddress = getMACAddress();
String concatenatedMAC = SALT + macAddress;
String expectedHash = doubleMD5(concatenatedMAC); // expectedHash = md5(md5(SALT + macAddress))
while (true) { // Loop until a valid hash is entered
Serial.println("Device Not Registered");
Serial.println("Please Enter Registration Code:");
while (!Serial.available()) {
// Wait for user input
}
String userInput = Serial.readString();
userInput.trim();
// Validate hash length
if (userInput.length() == HASH_LENGTH) {
// Compare the input hash with the expected hash
if (userInput == expectedHash) {
// Write the hash to EEPROM
writeHashToEEPROM(userInput);
Serial.println("Hash registered successfully. Restarting...");
delay(1000);
ESP.restart();
} else {
Serial.println("Invalid registration code. Please try again.");
}
} else {
Serial.println("Invalid hash length. Please try again.");
}
}
}
// Function to initialize and check hash
void ProtectSetup() {
Serial.begin(115200);
EEPROM.begin(EEPROM_SIZE);
String macAddress = getMACAddress();
String concatenatedMAC = SALT + macAddress; // Concatenate MAC address three times
String expectedHash = doubleMD5(concatenatedMAC);
String storedHash = readHashFromEEPROM();
// Print the MAC address
Serial.print("MAC Address: ");
Serial.println(macAddress);
// Check if the stored hash matches the expected hash
if (storedHash == expectedHash) {
Serial.println("Device is Registered");
// Initialize your application here
} else {
// Serial.println("Device is not Registered");
promptForHash();
}
}
#endif // PROTECT_H