Spring Security Spring Scrypt PasswordEncoder如何知道原始salt才能验证密码

zrfyljdw  于 5个月前  发布在  Spring
关注(0)|答案(1)|浏览(82)

在Spring中使用SCrypt散列“MYpassword“两次会生成两个不同的散列,这是预期的结果,因为盐是不同的。

hash 1: $100801$DBrs3RHYQafAjmY0RGGgtA==$VB1ahwEHntb36HWbAF1Eiy6FoJp1WH8xmpYY4+oHPUk=
hash 2: $100801$0EEqkaQUDzVCttua+jqu9A==$tstqjgsW5bzWXwRZKIeVy9P3jh/92QrZ8SW+8iLl7dc=

字符串
我不明白的是,为了验证密码,Scrypt需要用于哈希的原始盐。搜索后,我发现它从哈希字段本身获取盐。那么它在上面的例子中到底在哪里?我搜索了scrypt格式解码,没有任何结果。

9rbhqvlz

9rbhqvlz1#

根据source code

private String digest(CharSequence rawPassword, byte[] salt) {
    byte[] derived = SCrypt.generate(Utf8.encode(rawPassword), salt, this.cpuCost, this.memoryCost,
            this.parallelization, this.keyLength);
    String params = Long.toString(
            ((int) (Math.log(this.cpuCost) / Math.log(2)) << 16L) | this.memoryCost << 8 | this.parallelization,
            16);
    StringBuilder sb = new StringBuilder((salt.length + derived.length) * 2);
    sb.append("$").append(params).append('$');
    sb.append(encodePart(salt)).append('$');
    sb.append(encodePart(derived));
    return sb.toString();
}

字符串
参数、salt和hash使用$分隔符彼此相邻,因此中间的值是编码的salt。

相关问题