-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
BuildTools
committed
Nov 12, 2023
1 parent
d29c351
commit f58e88a
Showing
3 changed files
with
136 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
aiohttp~=3.8.4 | ||
colorama~=0.4.6 | ||
cryptography~=41.0.5 | ||
discord~=2.3.2 | ||
dpytest~=0.6.4 | ||
heckbot | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import os | ||
from datetime import datetime, timedelta | ||
from typing import Optional | ||
|
||
from cryptography.hazmat.backends import default_backend | ||
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes | ||
|
||
SECRET_KEY = os.getenv('HECKBOT_SECRET_KEY') | ||
|
||
|
||
def encrypt(username: str, expiry: str) -> tuple[bytes, bytes]: | ||
message = f"{username}:{expiry}".encode() | ||
iv = os.urandom(16) | ||
cipher = Cipher( | ||
algorithms.AES( | ||
SECRET_KEY.encode() | ||
), modes.CFB(iv), backend=default_backend() | ||
) | ||
encryptor = cipher.encryptor() | ||
ciphertext = encryptor.update(message) + encryptor.finalize() | ||
return ciphertext, iv | ||
|
||
|
||
def decrypt(ciphertext: str, iv: str) -> Optional[str]: | ||
cipher = Cipher( | ||
algorithms.AES( | ||
SECRET_KEY.encode() | ||
), modes.CFB(iv.encode()), backend=default_backend() | ||
) | ||
decryptor = cipher.decryptor() | ||
decrypted_data = decryptor.update( | ||
ciphertext.encode() | ||
) + decryptor.finalize() | ||
decoded_data = decrypted_data.decode() | ||
username, timestamp = decoded_data.split(':') | ||
timestamp = datetime.fromisoformat(timestamp) | ||
if datetime.utcnow() < timestamp + timedelta(seconds=300): | ||
return username | ||
return None |