dimanche 4 juillet 2021

Encrypt a string so that even the admin can't figure out

I'm trying to make a Discord confessions bot that maintains partial anonymity of the user. I plan to achieve this by making an embed colour be constant for a user, so people "know" who it is.

I was thinking of using a one way hashing algorithm that will give me the same output for a particular user. Then I can simply extract parts of the hash itself to use for the colour associated with the user.

The code is open source and the username of all server members are all publicly visible, so I used an environment variable to ensure that nobody can trace the user associated with the colour. However, since I know the value of the variable, I can technically still figure out which colour belongs to a particular user.

I feel like it's not possible, since I want a deterministic algorithm with known inputs and known outputs and I know the algorithm itself. Like having a list of passwords and the hashes associated with the passwords as well as the hashing algorithm used.

So I was hoping for something pseudo-random-like, but on restarting the program the same "random" number must be generated. This is still weak and can be figured out by outsiders too.

Coming back to my original question. Is it possible to somewhat randomise the algorithm so that even I can't backtrace it to find the colour associated with a user?

Code below:

import discord
import os
from hashlib import sha256

def embed_text(parsed, user):
  encrypted = sha256((str(user) + os.environ['ENCRYPTION_KEY']).encode()).hexdigest()
  color = int(encrypted[int(encrypted[0],16):int(encrypted[0],16)+6],16)
  return discord.Embed(description=parsed, color=color)



Aucun commentaire:

Enregistrer un commentaire