Add user tripcodes

Users can separate their name and a password with ## to create the
illusion of consistent posting.

Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
This commit is contained in:
2022-07-20 00:28:02 -07:00
parent 8f4b6d7aea
commit c53111ea60
6 changed files with 54 additions and 7 deletions

View File

@@ -1,9 +1,12 @@
from typing import Optional, TYPE_CHECKING
from django.utils import timezone
from django.conf import settings
import base64
import hashlib
import ipaddress
import random
import string
from typing import Optional, TYPE_CHECKING
from django.utils import timezone
from django.conf import settings
from board.models import Ban, RangeBan
@@ -56,3 +59,11 @@ def generate_user_token() -> str:
User tokens need not be secure so this is a simple implementation.
"""
return "".join(random.choices(string.ascii_letters, k=settings.USER_TOKEN_LENGTH))
def generate_tripcode(value: str) -> str:
hasher = hashlib.sha256()
hasher.update(settings.TRIPCODE_SALT.encode())
hasher.update(value.encode())
trip = base64.b64encode(hasher.digest()).decode("ascii")
return trip[0:10]