如何从python生成Spring兼容密码?

我有一个spring应用程序,它在db中创建如下所示的scrypt密码:


  {scrypt} $ e0801 $ QAqC0fvhY6iJPysiQsFnrcUg205njHo / 6o + IDXDn33lxmZOCVBhb4NAqdafhuGmykCxQtMI5xP5zb7MYMUrU3Q == $ sBEXCHOm6zQuGdRQID + 3HU


我正在尝试使用python和passlib使用新密码直接更新数据库,但无法从$e0801$判断passlib的正确setting_kwds:

>>> from passlib.hash import scrypt
>>> scrypt.verify('wBkfoBsxj9u3wLOZ', '{scrypt}$e0801$QAqC0fvhY6iJPysiQsFnrcUg205njHo/6o+IDXDn33lxmZOCVBhb4NAqdafhuGmykCxQtMI5xP5zb7MYMUrU3Q==$sBeXCHOm6zQuGdSDKs+HeXnNQGg3bhRidmL+HU/ZTMM=')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/gregn/.pyenv/versions/v2.7.16/lib/python2.7/site-packages/passlib/utils/handlers.py", line 789, in verify
    self = cls.from_string(hash, **context)
  File "/home/gregn/.pyenv/versions/v2.7.16/lib/python2.7/site-packages/passlib/handlers/scrypt.py", line 177, in from_string
    return cls(**cls.parse(hash))
  File "/home/gregn/.pyenv/versions/v2.7.16/lib/python2.7/site-packages/passlib/handlers/scrypt.py", line 181, in parse
    ident, suffix = cls._parse_ident(hash)
  File "/home/gregn/.pyenv/versions/v2.7.16/lib/python2.7/site-packages/passlib/utils/handlers.py", line 1207, in _parse_ident
    raise exc.InvalidHashError(cls)
ValueError: not a valid scrypt hash


手动摆弄前缀格式不起作用:

>>> scrypt.verify('wBkfoBsxj9u3wLOZ', '$scrypt$ln=1,r=8,p=1$QAqC0fvhY6iJPysiQsFnrcUg205njHo/6o+IDXDn33lxmZOCVBhb4NAqdafhuGmykCxQtMI5xP5zb7MYMUrU3Q==$sBeXCHOm6zQuGdSDKs+HeXnNQGg3bhRidmL+HU/ZTMM=')
False


我认为(但不确定)Spring正在使用默认的SCryptPasswordEncoder设置,因为我发现:

public static PasswordEncoder getPasswordEncoder() {
    final String encodingId = "scrypt";
    final Map<String, PasswordEncoder> encoders = new HashMap<>();
    encoders.put("bcrypt", new BCryptPasswordEncoder());
    encoders.put(encodingId, new SCryptPasswordEncoder());
    return new DelegatingPasswordEncoder(encodingId, encoders);
}


春季版:

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::       (v1.5.18.RELEASE)


谢谢!

最佳答案

您提供的不是有效的scrypt哈希

{scrypt}$e0801$QAqC0fvhY....


这是正确的格式(只需将{scrypt}替换为$s0

$s0$e0801$QAqC0fvhY6iJPysiQsFnrcUg205njHo/6o+IDXDn33lxmZOCVBhb4NAqdafhuGmykCxQtMI5xP5zb7MYMUrU3Q==$sBeXCHOm6zQuGdSDKs+HeXnNQGg3bhRidmL+HU/ZTMM=


如果尝试使用Password4j获取配置:

SCryptFunction.getInstanceFromHash("$s0$e0801$QAqC0...");


您得到N=16384r=8p=1。在您的Python实现中使用此配置。

09-10 08:13