O2Sound_ver2_final/backend/app/shared/crypto.py

33 lines
1.2 KiB
Python

from passlib.context import CryptContext
from cryptography.fernet import Fernet
from app.core.env_setting import EnvSetting
settings = EnvSetting()
env_setting = settings
# 비밀번호 해싱을 위한 설정
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
# 대칭키 암호화를 위한 키
CRYPTO_SECRET_KEY = env_setting.CRYPTO_SECRET_KEY
fernet = Fernet(CRYPTO_SECRET_KEY)
class Crypto:
@staticmethod
def hash_password(password: str) -> str:
"""비밀번호를 bcrypt로 해싱합니다."""
return pwd_context.hash(password)
@staticmethod
def verify_password(plain_password: str, hashed_password: str) -> bool:
"""입력된 비밀번호가 해시와 일치하는지 확인합니다."""
return pwd_context.verify(plain_password, hashed_password)
@staticmethod
def encrypt_token(token: str) -> str:
"""토큰을 대칭키 암호화로 암호화합니다."""
return fernet.encrypt(token.encode()).decode()
@staticmethod
def decrypt_token(encrypted_token: str) -> str:
"""암호화된 토큰을 복호화합니다."""
return fernet.decrypt(encrypted_token.encode()).decode()