将密码从web2py迁移到Django

将密码从web2py迁移到Django,django,passwords,web2py,sha512,Django,Passwords,Web2py,Sha512,我使用SHA 512算法将密码存储在web2py中。我现在正在将模型迁移到django,因此需要一种使用SHA 512在django中散列密码的方法,就像web2py一样,这样我就可以用相同的密码对老用户进行身份验证。请给出一些建议。我认为最好的解决方案是编写一个身份验证后端,该后端将根据web2py库对用户进行身份验证,然后要求他更改或确认密码,并建立一个新的Django auth密码 加密散列密码的漏洞在于,如果你有权访问数据库,你或任何黑客都看不到它们 以下是关于的Django文档。根据这

我使用SHA 512算法将密码存储在web2py中。我现在正在将模型迁移到django,因此需要一种使用SHA 512在django中散列密码的方法,就像web2py一样,这样我就可以用相同的密码对老用户进行身份验证。请给出一些建议。

我认为最好的解决方案是编写一个身份验证后端,该后端将根据web2py库对用户进行身份验证,然后要求他更改或确认密码,并建立一个新的Django auth密码

加密散列密码的漏洞在于,如果你有权访问数据库,你或任何黑客都看不到它们

以下是关于的Django文档。

根据这一点,用于重新创建web2py中使用的约定的Python代码片段如下所示:

from hashlib import md5
import hmac
hmac_key = '<your secret key>'
password = 'insecure'
thehash = hmac.new(hmac_key, password).hexdigest()
print thehash
更新:我从web2py源代码中提取了以下代码,但您需要做的是使用为auth.settings.hmac_key设置的值更新变量hmac_key。希望在运行(更新hmac_key变量后)时,哈希值应该匹配

import hashlib
import hmac
from hashlib import sha512

h="sha512$b850ed44943b861b$c90901439983bce7fd512592b20d83f8e654632dee51de515773e70eabe609f62cebec64fed4df03acd54e6a627c9291e70fdf3a89996ffa796897c159e95c11"

algo,salt,hash = h.split("$")
print "crypted hash: %s"%hash

pwd = "pawan123"
##get this value from auth.settings.hmac_key
hmac_key = "" 

def get_digest(value):
    """
    Returns a hashlib digest algorithm from a string
    """
    if not isinstance(value, str):
        return value
    value = value.lower()
    if value == "md5":
        return md5
    elif value == "sha1":
        return sha1
    elif value == "sha224":
        return sha224
    elif value == "sha256":
        return sha256
    elif value == "sha384":
        return sha384
    elif value == "sha512":
        return sha512
    else:
        raise ValueError("Invalid digest algorithm: %s" % value)

#hashed = simple_hash(self.password, key, salt, digest_alg)
def simple_hash(text, key='', salt='', digest_alg='md5'):
    """
    Generates hash with the given text using the specified
    digest hashing algorithm
    """
    if not digest_alg:
        raise RuntimeError("simple_hash with digest_alg=None")
    elif not isinstance(digest_alg, str):  # manual approach
        h = digest_alg(text + key + salt)
    elif digest_alg.startswith('pbkdf2'):  # latest and coolest!
        iterations, keylen, alg = digest_alg[7:-1].split(',')
        return pbkdf2_hex(text, salt, int(iterations),
                          int(keylen), get_digest(alg))
    elif key:  # use hmac
        digest_alg = get_digest(digest_alg)
        h = hmac.new(key + salt, text, digest_alg)
    else:  # compatible with third party systems
        h = get_digest(digest_alg)()
        h.update(text + salt)
    return h.hexdigest()




print "result hash:  %s"%simple_hash(pwd, hmac_key, salt, "sha512")

但每个密码都有盐。我们将如何解决这个问题?在这种情况下,“salt”是hmac_密钥+纯文本用户密码。其中一个由您控制(秘密hmac_密钥),另一个由用户提供。这使得salt更安全,因为它不存储在任何地方,因此密码破解将花费更长的时间。如果你给我完整的密码哈希(algo$salt$hash),我会用更多关于web2py salt的信息更新我的答案从web2py和明文中,我可以帮助您编写python明文是pawan123,完整密码哈希是:sha512$b850ed44943b861b$C901439983BCE7FD512592B20D83F8E654632 EE515773E70EABE609F62CEBEC64FED4DF03ACD54E6A627C92E70FDF389996FFA796897C159E95C11
algo, salt, hash = password_hash.split("$")
import hashlib
import hmac
from hashlib import sha512

h="sha512$b850ed44943b861b$c90901439983bce7fd512592b20d83f8e654632dee51de515773e70eabe609f62cebec64fed4df03acd54e6a627c9291e70fdf3a89996ffa796897c159e95c11"

algo,salt,hash = h.split("$")
print "crypted hash: %s"%hash

pwd = "pawan123"
##get this value from auth.settings.hmac_key
hmac_key = "" 

def get_digest(value):
    """
    Returns a hashlib digest algorithm from a string
    """
    if not isinstance(value, str):
        return value
    value = value.lower()
    if value == "md5":
        return md5
    elif value == "sha1":
        return sha1
    elif value == "sha224":
        return sha224
    elif value == "sha256":
        return sha256
    elif value == "sha384":
        return sha384
    elif value == "sha512":
        return sha512
    else:
        raise ValueError("Invalid digest algorithm: %s" % value)

#hashed = simple_hash(self.password, key, salt, digest_alg)
def simple_hash(text, key='', salt='', digest_alg='md5'):
    """
    Generates hash with the given text using the specified
    digest hashing algorithm
    """
    if not digest_alg:
        raise RuntimeError("simple_hash with digest_alg=None")
    elif not isinstance(digest_alg, str):  # manual approach
        h = digest_alg(text + key + salt)
    elif digest_alg.startswith('pbkdf2'):  # latest and coolest!
        iterations, keylen, alg = digest_alg[7:-1].split(',')
        return pbkdf2_hex(text, salt, int(iterations),
                          int(keylen), get_digest(alg))
    elif key:  # use hmac
        digest_alg = get_digest(digest_alg)
        h = hmac.new(key + salt, text, digest_alg)
    else:  # compatible with third party systems
        h = get_digest(digest_alg)()
        h.update(text + salt)
    return h.hexdigest()




print "result hash:  %s"%simple_hash(pwd, hmac_key, salt, "sha512")