Files
fonrey/core/encryption.py
ishenwei 4aba6dfa77 Initialize Fonrey Django multi-tenant project skeleton
Set up the required directory layout, app scaffolding, core settings, templates, static assets, and Docker/Tailwind tooling to establish a standardized development baseline.
2026-04-26 17:12:09 +08:00

36 lines
905 B
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import base64
import hashlib
from cryptography.fernet import Fernet
from django.conf import settings
class PhoneEncryption:
"""
手机号 AES-256-GCM 加密存储 + SHA-256 哈希索引
存储字段phone_encrypted加密密文+ phone_hash哈希用于精确查询
显示:脱敏格式 138****1234
"""
@staticmethod
def encrypt(phone: str) -> str:
"""加密手机号,返回 base64 密文"""
...
@staticmethod
def decrypt(ciphertext: str) -> str:
"""解密返回明文"""
...
@staticmethod
def hash(phone: str) -> str:
"""返回 SHA-256 哈希(用于 DB 索引查询)"""
...
@staticmethod
def mask(phone: str) -> str:
"""返回脱敏格式138****1234"""
if not phone or len(phone) < 7:
return "***"
return phone[:3] + "****" + phone[-4:]