【问题标题】:Django unknown password hashing algorithm - writing a custom hashing alghorithmDjango 未知密码散列算法 - 编写自定义散列算法
【发布时间】:2013-01-09 06:46:15
【问题描述】:

我将用户从其他站点迁移到 django 站点。旧网页有带盐的 md5 密码,我正在编写一个自定义散列算法,以便用户可以使用他们的旧密码进行身份验证。

当我尝试以旧用户身份登录时:

Unknown password hashing algorithm 'my_hashed_password'. Did you specify it in the PASSWORD_HASHERS setting?

这是我的密码哈希

class FallbackMD5PasswordHasher(BasePasswordHasher):

    algorithm = "fallback_md5"

    def salt(self):
        return 'my_salt'

    def encode(self, password):
        return hashlib.md5(self.salt() + password).hexdigest()

    def verify(self, password, encoded):
        encoded_2 = self.encode(password, '')
        return constant_time_compare(encoded, encoded_2)

    def safe_summary(self, encoded):
        return SortedDict([
            (_('algorithm'), self.algorithm),
            (_('hash'), mask_hash(encoded, show=3)),
        ])

那是我的 settings.py

PASSWORD_HASHERS = (
    'lfs_custom.hasher.FallbackMD5PasswordHasher',
    'django.contrib.auth.hashers.PBKDF2PasswordHasher',
    'django.contrib.auth.hashers.PBKDF2SHA1PasswordHasher',
    'django.contrib.auth.hashers.BCryptPasswordHasher',
    'django.contrib.auth.hashers.SHA1PasswordHasher',
    'django.contrib.auth.hashers.MD5PasswordHasher',
    'django.contrib.auth.hashers.CryptPasswordHasher',
    )

请提供一些信息来解决这个问题 :) 我不知道为什么它不起作用。

【问题讨论】:

  • 字符串“my_hashed_pa​​ssword”从何而来?你能查一下项目文件吗?
  • 该字符串是我数据库中的哈希密码,登录时返回错误。

标签: django authentication hash passwords


【解决方案1】:

如果你需要这个 {

类 FallbackMD5PasswordHasher(BasePasswordHasher): 算法 = “fallback_md5” 迭代次数 = 100000 def盐(自我): 返回''

def encode(self, password, salt):
    assert salt == ''
    hash= hashlib.md5(password).hexdigest()
    return "%s$%d$%s$%s" % (self.algorithm, self.iterations, '', hash)

def verify(self, password, encoded):
    algorithm, iterations, salt, hash = encoded.split('$', 3)
    assert algorithm == self.algorithm

    encoded_2 = self.encode(password, '')
    return constant_time_compare(encoded, encoded_2)

def safe_summary(self, encoded):
    algorithm, iterations, salt, hash = encoded.split('$', 3)
    return SortedDict([
        (_('algorithm'), algorithm),
        (_('iterations'), iterations),
        (_('salt'), mask_hash(salt)),
        (_('hash'), mask_hash(hash)),
    ])

def must_update(self, encoded):
    return True

}

【讨论】:

    【解决方案2】:

    Django 尝试使用 format 解析数据库中的哈希:

    <algorithm>$<iterations>$<salt>$<hash>
    

    您可能在数据库中有普通的哈希值。尝试在数据库上执行 SQL 以使用正确的格式进行更新,例如:

    update auth_user set password=concat("md5$1$", password);
    

    【讨论】:

      猜你喜欢
      • 2013-02-28
      • 1970-01-01
      • 1970-01-01
      • 2011-10-05
      • 1970-01-01
      • 1970-01-01
      • 2013-08-21
      • 2016-11-03
      • 2016-10-21
      相关资源
      最近更新 更多