python - Argon2 库,可以在没有 secret 的情况下对密码进行哈希处理,并且使用看起来不可解析的随机盐

标签 python argon2-ffi

我正在寻找 Python 应用程序中散列密码的不同替代方案。首先,我决定使用 Flask-bcrypt (https://github.com/maxcountryman/flask-bcrypt),但后来决定使用 Argon2。 Python 最流行的 Argon2 绑定(bind)是 argon2-cffi (https://github.com/hynek/argon2-cffi)。

根据它的文档( https://argon2-cffi.readthedocs.io/en/stable/api.html ),我需要做的就是使用 3 种方法:

  • hash散列密码
  • verify将密码与哈希值进行比较
  • check_needs_rehash查看更改散列参数后是否应重新散列密码

  • 有两件事让我困惑。

    1) 盐是随机的,使用 os.urandom .因此我想知道 verify方法能够以某种方式从哈希中提取盐吗?或者换句话说,既然我对盐是什么没有发言权并且无法保存它,那么verify方法实际上是否曾经将任何密码与使用随机盐散列的密码进行比较?我应该以某种方式从 hash 的返回值中解析盐吗?我自己,并将其与散列值分开存储?还是散列应该按原样存储在文档中,原封不动,并且 Argon2 能够以某种方式验证密码?如果 Argon2 确实可以从哈希中提取盐,那么在这种情况下使用盐如何更安全,因为获得哈希密码的敌对实体也应该能够提取盐?

    2) 默认情况下,我不向 hash 提供任何 secret 方法,而密码本身似乎被用作 secret 。这安全吗?我不提供散列方法的 secret 有什么缺点?

    最佳答案

    1) The salt is random, using os.urandom. I thus wonder if the verify method is somehow able to extract the salt from the hash?



    hash方法返回一个对盐、参数和密码哈希本身进行编码的字符串,如文档中所示:
    >>> from argon2 import PasswordHasher
    >>> ph = PasswordHasher()
    >>> hash = ph.hash("s3kr3tp4ssw0rd")
    >>> hash  
    '$argon2id$v=19$m=102400,t=2,p=8$tSm+JOWigOgPZx/g44K5fQ$WDyus6py50bVFIPkjA28lQ'
    >>> ph.verify(hash, "s3kr3tp4ssw0rd")
    True
    

    格式汇总in the Argon2 reference implementation ;也许还有其他引用资料。在这种情况下:
  • $argon2id$...
    哈希是 Argon2id,这是每个人都应该使用的特定 Argon2 变体(结合了 Argon2i 的侧 channel 阻力和更难破解的 Argon2d)。
  • ...$v=19$...
    哈希版本为 0x13(十进制 19),表示 Argon2 v1.3,Password Hashing Competition 采用的版本.
  • ...$m=102400,t=2,p=8$...
    内存使用为100 MB(102400 KB),时间为2次迭代,并行度为8路。
  • ...$tSm+JOWigOgPZx/g44K5fQ$...
    盐是tSm+JOWigOgPZx/g44K5fQ (base64),或 b5 29 be 24 e5 a2 80 e8 0f 67 1f e0 e3 82 b9 7d (十六进制)。
  • ...$WDyus6py50bVFIPkjA28lQ
    密码哈希本身是 WDyus6py50bVFIPkjA28lQ (base64),或 58 3c ae b3 aa 72 e7 46 d5 14 83 e4 8c 0d bc 95 (十六进制)。
  • verify方法获取此字符串和候选密码,使用所有编码参数重新计算密码哈希,并将其与编码密码哈希进行比较。

    And if indeed Argon2 can extract the salt out of the hash, how is using a salt any safer in that case since a hostile entity who gets a hashed password should then also be able to extract the salt?



    salt 的目的是通过简单地对每个用户进行不同的处理来减轻多目标攻击的批量优势。
  • 如果每个人都使用相同的盐,那么攻击者试图找到给定哈希值的 $n$ 密码中的第一个只需要花费大约 1 美元/n$ 的成本,而攻击者试图在给定哈希值的情况下找到单个特定密码所必须花费的成本花费。或者,对手可以通过进行昂贵的预计算(彩虹表)来加速破解个人密码。
  • 但如果每个人都使用不同的盐,那么批量优势或预计算优势就会消失。

  • 在 32 字节的字符串中随机均匀地选择 salt 只是保证每个用户都有不同的 salt 的一种简单方法。原则上,可以想象一个权威机构向世界上的每个人分发一个连续的数字作为他们的 Argon2 盐,但该系统不能很好地扩展——我的意思不仅仅是你的应用程序可以使用计数权威,而是世界上的每个应用程序都必须使用相同的计数权限,我认为芝麻街的计数太忙了,无法承担这项工作。

    2) By default I do not supply any secret to the hash method and instead the password itself seems to be used as a secret. Is this secure? What are the downsides for me not supplying a secret to the hashing method?



    一般来说,密码就是 secret :如果有人知道密码,那么他们应该能够登录;如果他们不知道密码,他们应该被带到门口!

    也就是说,Argon2 还支持 key ,它与盐分开,与密码分开。

    如果您的密码数据库和您的应用程序之间存在有意义的安全边界,因此攻击者可能会破坏其中一个而不是另一个,那么应用程序可以选择一个统一的随机 32 字节字符串作为 key ,并将其与 Argon2 一起使用使得密码哈希是 secret 密码的 secret 函数。

    这样,转储密码数据库但没有转储应用程序 key 的对手甚至无法测试密码的猜测,因为他们不知道计算密码哈希所需的 key 。

    关于python - Argon2 库,可以在没有 secret 的情况下对密码进行哈希处理,并且使用看起来不可解析的随机盐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58431973/

    相关文章:

    cryptography - 使用 BouncyCaSTLe 库的 Argon2 示例

    python - Django 连接远程 Mysql OperationalError 2026

    python - 带空字符串的条件 f 字符串格式

    python - 在Python中读取和搜索文本文件?

    node.js - Argon2 与 Node docker 容器

    php - PHP7 中的 Argon2i - 选择合适的选项

    python - talib ADX 函数错误

    python - 如何将 4 个 while 三角形循环放在一起以生成 "diamond"?

    PHP 警告 : Use of undefined constant PASSWORD_ARGON2ID when using password_hash() in php 7. 3

    python - 无法安装Python argon2_cffi