View on GitHub

暗号化の設定

Home

暗号化の設定

ENCRYPTION_KEYの設定

backend/config/settings/base.py

from pathlib import Path
import os  # <- 追加
......
LOGGING = {
......
}

ENCRYPTION_KEY = os.environ.get('ENCRYPTION_KEY')  # <- 追加

暗号化、復号化、ハッシュ化の関数の作成

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()