Skip to content

Commit f9f81cb

Browse files
Update jellyfish.c
1 parent adf8082 commit f9f81cb

File tree

1 file changed

+17
-92
lines changed

1 file changed

+17
-92
lines changed

code/logic/jellyfish.c

Lines changed: 17 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -127,107 +127,32 @@ uint64_t get_time_microseconds(void) {
127127
}
128128
#endif
129129

130-
#if defined(_WIN32)
131-
#include <windows.h>
132-
#elif defined(__unix__) || defined(__APPLE__)
133-
#include <unistd.h>
134-
#include <pwd.h>
135-
#endif
136-
137-
static uint64_t fnv1a_hash(const unsigned char *data, size_t len) {
130+
static uint64_t get_device_salt(void) {
138131
uint64_t hash = 0xcbf29ce484222325ULL;
139-
for (size_t i = 0; i < len; ++i) {
140-
hash ^= data[i];
141-
hash *= 0x100000001b3ULL;
142-
}
143-
return hash;
144-
}
145-
146-
// Try reading file contents as salt (Linux/macOS)
147-
static uint64_t try_read_file_salt(const char *path) {
148-
FILE *fp = fopen(path, "rb");
149-
if (!fp) return 0;
150-
char buffer[256];
151-
size_t len = fread(buffer, 1, sizeof(buffer), fp);
152-
fclose(fp);
153-
if (len == 0) return 0;
154-
return fnv1a_hash((unsigned char *)buffer, len);
155-
}
156-
157-
// Try volume serial number (Windows)
158-
static uint64_t try_windows_volume_serial_salt(void) {
159-
#if defined(_WIN32)
160-
DWORD serial = 0;
161-
if (GetVolumeInformationA("C:\\", NULL, 0, &serial, NULL, NULL, NULL, 0)) {
162-
return fnv1a_hash((unsigned char *)&serial, sizeof(serial));
163-
}
164-
#endif
165-
return 0;
166-
}
167-
168-
// Try hostname + username combo (portable)
169-
static uint64_t try_username_hostname_salt(void) {
170-
char buffer[512];
171-
buffer[0] = '\0';
172-
173-
#if defined(_WIN32)
174-
DWORD size = 256;
175-
char username[256], hostname[256];
176-
GetUserNameA(username, &size);
177-
size = 256;
178-
GetComputerNameA(hostname, &size);
179-
snprintf(buffer, sizeof(buffer), "%s@%s", username, hostname);
180-
181-
#elif defined(__unix__) || defined(__APPLE__)
182-
const char *username = getlogin();
132+
const char *user = getenv("USER");
133+
const char *host = getenv("HOSTNAME");
183134
char hostname[256];
184-
gethostname(hostname, sizeof(hostname));
185-
snprintf(buffer, sizeof(buffer), "%s@%s", username ? username : "?", hostname);
186-
#endif
187-
188-
return fnv1a_hash((unsigned char *)buffer, strlen(buffer));
189-
}
190135

191-
// Final fallback: generate + store UUID
192-
static uint64_t try_random_uuid_salt(void) {
193-
FILE *fp = fopen(".device_salt", "rb");
194-
uint64_t salt;
195-
if (fp && fread(&salt, sizeof(salt), 1, fp) == 1) {
196-
fclose(fp);
197-
return salt;
136+
if (!host) {
137+
gethostname(hostname, sizeof(hostname));
138+
host = hostname;
198139
}
199140

200-
// Generate new one
201-
salt = ((uint64_t)rand() << 32) | rand();
202-
fp = fopen(".device_salt", "wb");
203-
if (fp) {
204-
fwrite(&salt, sizeof(salt), 1, fp);
205-
fclose(fp);
141+
if (user) {
142+
for (size_t i = 0; i < strlen(user); ++i) {
143+
hash ^= user[i];
144+
hash *= 0x100000001b3ULL;
145+
}
206146
}
207-
return salt;
208-
}
209-
210-
uint64_t get_device_salt(void) {
211-
uint64_t salt = 0;
212147

213-
#if defined(__linux__) || defined(__APPLE__)
214-
const char *paths[] = {
215-
"/etc/machine-id",
216-
"/var/lib/dbus/machine-id",
217-
"/sys/class/dmi/id/product_uuid"
218-
};
219-
for (int i = 0; i < 3 && !salt; ++i) {
220-
salt = try_read_file_salt(paths[i]);
148+
if (host) {
149+
for (size_t i = 0; i < strlen(host); ++i) {
150+
hash ^= host[i];
151+
hash *= 0x100000001b3ULL;
152+
}
221153
}
222-
#endif
223154

224-
#if defined(_WIN32)
225-
if (!salt) salt = try_windows_volume_serial_salt();
226-
#endif
227-
228-
if (!salt) salt = try_username_hostname_salt();
229-
if (!salt) salt = try_random_uuid_salt();
230-
return salt;
155+
return hash;
231156
}
232157

233158
void fossil_jellyfish_hash(const char *input, const char *output, uint8_t *hash_out) {

0 commit comments

Comments
 (0)