暗号化の設定
ENCRYPTION_KEY の設定
src/backend/config/settings/base.py
...(略)...
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = os.environ.get('DJANGO_SECRET_KEY')
ENCRYPTION_KEY = os.environ.get('ENCRYPTION_KEY') # <- 追加
...(略)...
暗号化、復号化、ハッシュ化の関数の作成
src/backend/api/online/utils.py
import hashlib
from cryptography.fernet import Fernet
from django.conf import settings
## Fernetオブジェクトを作成
cipher = Fernet(settings.ENCRYPTION_KEY.encode())
def encrypt(text):
"""textを暗号化"""
return cipher.encrypt(text.encode()).decode()
def decrypt(encrypted_text):
"""textを復号化"""
return cipher.decrypt(encrypted_text.encode()).decode()
def hash(text):
"""textのハッシュ値を生成 (SHA256)"""
return hashlib.sha256(text.encode()).hexdigest()