[Django] secret_key 재생성

nikevapormax·2023년 8월 24일
0

TIL

목록 보기
109/116

django secret_key 재생성

  • 터미널에서 python manage.py shell 명령어 실행
  • 아래와 같이 명령어를 입력해 새로운 시크릿 키 생성
>>> from django.core.management.utils import get_random_secret_key
>>> print(get_random_secret_key())
  • 다음과 같은 결과가 나오는 것을 알 수 있다.
yz_77wxy@)(pd#e@d(6v@bgz22%8heq-pt(8#uu(u-x#gr3)u-
  • 위 시크릿 키를 settings.py에 있는 SECRET_KEY에 대입하면 된다.

참고

  • 위에서 사용한 함수는 아래와 같다.
def get_random_secret_key():
    """
    Return a 50 character random string usable as a SECRET_KEY setting value.
    """
    chars = "abcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*(-_=+)"
    return get_random_string(50, chars)
  • get_random_string은 아래 경로를 통해 참조된다.
from django.utils.crypto import get_random_string
def get_random_string(length, allowed_chars=RANDOM_STRING_CHARS):
    """
    Return a securely generated random string.

    The bit length of the returned value can be calculated with the formula:
        log_2(len(allowed_chars)^length)

    For example, with default `allowed_chars` (26+26+10), this gives:
      * length: 12, bit length =~ 71 bits
      * length: 22, bit length =~ 131 bits
    """
    return "".join(secrets.choice(allowed_chars) for i in range(length))
profile
https://github.com/nikevapormax

0개의 댓글