Skip to content

Commit

Permalink
Swap out hash function for obfuscated IDs
Browse files Browse the repository at this point in the history
  • Loading branch information
sfan5 committed Jul 20, 2024
1 parent b75e12c commit 192c13d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
9 changes: 5 additions & 4 deletions secretlounge_ng/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ def defaults(self):
"hideKarma", "debugEnabled", "tripcode"
)

ID_ALPHA = "0123456789abcdefghijklmnopqrstuv"

class User():
__slots__ = USER_PROPS
id: int
Expand Down Expand Up @@ -66,10 +68,9 @@ def isBlacklisted(self):
return self.rank < 0
def getObfuscatedId(self):
salt = date.today().toordinal()
if salt & 0xff == 0: salt >>= 8 # zero bits are bad for hashing
value = (self.id * salt) & 0xffffff
alpha = "0123456789abcdefghijklmnopqrstuv"
return ''.join(alpha[n%32] for n in (value, value>>5, value>>10, value>>15))
value = fnv32a([self.id, salt], [])
# stringify 20 bits
return ''.join(ID_ALPHA[n%32] for n in (value, value>>5, value>>10, value>>15))
def getObfuscatedKarma(self):
for cutoff in (100, 50, 10):
if abs(self.karma) >= cutoff:
Expand Down
15 changes: 15 additions & 0 deletions secretlounge_ng/globals.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,21 @@ def format_timedelta(d):
return "%d%c" % (d // cmp, char)
return "%ds" % d.total_seconds()

# 32-bit FNV-1a
def fnv32a(int_parts, byte_parts) -> int:
h = 0x811c9dc5
p = 0x01000193
for i in int_parts:
i = abs(i)
# trivial little endian encoding
while i != 0:
h = ((h ^ (i & 0xff)) * p) & 0xffffffff
i >>= 8
for bs in byte_parts:
for b in bs:
h = ((h ^ b) * p) & 0xffffffff
return h

## for debugging ##
def dump(obj, name=None, r=False):
name = (name + ".") if name else ""
Expand Down

0 comments on commit 192c13d

Please sign in to comment.