在脚本中更改 Django 密钥
Change Django secret key within script
我有一个 python 脚本,需要在整个执行过程中多次更改 django SECRET_KEY
。这是我正在尝试做的一个例子:
from django.core import signing
# SECRET_KEY = "abc"
print signing.dumps("value")
# SECRET_KEY = "123"
print signing.dumps("value")
# SECRET_KEY = "test"
print signing.dumps("value")
signing.dumps
方法要求 SECRET_KEY
已经设置,但它似乎没有在脚本中更改它的参数。
我该如何在脚本中多次更改 SECRET_KEY
?
如果您仔细查看 dumps
method in signing.py
,您会发现它接受一个密钥作为签名密钥:
If key is None, settings.SECRET_KEY is used instead.
def dumps(obj, key=None, salt='django.core.signing', serializer=JSONSerializer, compress=False):
"""
Returns URL-safe, sha1 signed base64 compressed JSON string. If key is
None, settings.SECRET_KEY is used instead.
If compress is True (not the default) checks if compressing using zlib can
save some space. Prepends a '.' to signify compression. This is included
in the signature, to protect against zip bombs.
Salt can be used to namespace the hash, so that a signed string is
only valid for a given namespace. Leaving this at the default
value or re-using a salt value across different parts of your
application without good cause is a security risk.
The serializer is expected to return a bytestring.
"""
所以你需要做的就是每次传递不同的密钥:
SECRET_KEY = "abc"
print signing.dumps("value", key=SECRET_KEY)
SECRET_KEY = "123"
print signing.dumps("value", key=SECRET_KEY)
也就是说,这对我来说似乎不是个好主意,因为您没有使用默认密钥进行签名。如果您真的需要签署文本,请使用 Signer class
来创建对象,并在其中使用新的 key
实例化对象,然后像 Signer(key="NEW KEY")
.
一样使用它
两件事:
- 您不需要更改 SECRET_KEY。事实上,如果您这样做,您将无法取消签署已签署的内容!
- signing.dumps() 函数适用于复杂数据类型,例如字典和元组。
如果您有一个要签名的值,您可以执行以下操作:
from django.core.signing import Signer
signer = Signer()
value = signer.sign('My string')
...然后在发布表单后您可以验证签名...
try:
original = signer.unsign(value)
except signing.BadSignature:
print("Tampering detected!")
我有一个 python 脚本,需要在整个执行过程中多次更改 django SECRET_KEY
。这是我正在尝试做的一个例子:
from django.core import signing
# SECRET_KEY = "abc"
print signing.dumps("value")
# SECRET_KEY = "123"
print signing.dumps("value")
# SECRET_KEY = "test"
print signing.dumps("value")
signing.dumps
方法要求 SECRET_KEY
已经设置,但它似乎没有在脚本中更改它的参数。
我该如何在脚本中多次更改 SECRET_KEY
?
如果您仔细查看 dumps
method in signing.py
,您会发现它接受一个密钥作为签名密钥:
If key is None, settings.SECRET_KEY is used instead.
def dumps(obj, key=None, salt='django.core.signing', serializer=JSONSerializer, compress=False):
"""
Returns URL-safe, sha1 signed base64 compressed JSON string. If key is
None, settings.SECRET_KEY is used instead.
If compress is True (not the default) checks if compressing using zlib can
save some space. Prepends a '.' to signify compression. This is included
in the signature, to protect against zip bombs.
Salt can be used to namespace the hash, so that a signed string is
only valid for a given namespace. Leaving this at the default
value or re-using a salt value across different parts of your
application without good cause is a security risk.
The serializer is expected to return a bytestring.
"""
所以你需要做的就是每次传递不同的密钥:
SECRET_KEY = "abc"
print signing.dumps("value", key=SECRET_KEY)
SECRET_KEY = "123"
print signing.dumps("value", key=SECRET_KEY)
也就是说,这对我来说似乎不是个好主意,因为您没有使用默认密钥进行签名。如果您真的需要签署文本,请使用 Signer class
来创建对象,并在其中使用新的 key
实例化对象,然后像 Signer(key="NEW KEY")
.
两件事:
- 您不需要更改 SECRET_KEY。事实上,如果您这样做,您将无法取消签署已签署的内容!
- signing.dumps() 函数适用于复杂数据类型,例如字典和元组。
如果您有一个要签名的值,您可以执行以下操作:
from django.core.signing import Signer
signer = Signer()
value = signer.sign('My string')
...然后在发布表单后您可以验证签名...
try:
original = signer.unsign(value)
except signing.BadSignature:
print("Tampering detected!")