2016-07-04 69 views
0

我将Spring Security的Bcrypt密码编码器集成到一个新的应用程序中,并且在测试过程中我注意到当使用两个不同的编码器匹配密码时,工作负载似乎没有影响工作因素。举例如下:Spring Security BCrypt密码编码器 - 工作负载因子

public static void main(String[] args) { 
    PasswordEncoder strongEncoder = new BCryptPasswordEncoder(12); 
    PasswordEncoder weakEncoder = new BCryptPasswordEncoder(6); 

    String password = "[email protected]@"; 

    String strongEncodedPass = strongEncoder.encode(password); 
    String weakEncodedPass = weakEncoder.encode(password); 

    //Prints true 
    System.out.println(weakEncoder.matches(password, strongEncodedPass)); 
    //Prints true 
    System.out.println(strongEncoder.matches(password, weakEncodedPass)); 
} 

由于编码器使用不同的工作负载,不应该同时打印语句结果为false?

将上述样品中的Java 8

回答

0

读取源代码使用弹簧安全核心4.1.0.RELEASE.jar测试,生成所述盐时强度时才使用。加密算法本身所使用的回合数被硬编码为16.

所以你看到的是预期的。不知道为什么Spring不允许为加密部分选择多轮。在bug /功能请求中可能值得指出,因为文档确实令人困惑。

2

如果你看一下在BCrypt(https://en.wikipedia.org/wiki/Bcrypt)维基百科的文章,你会发现,哈希的格式包含轮

例如数量,影子密码记录$2a$10$N9qo8uLOickgx2ZMRZoMyeIjZAgcfl7p92ldGxad68LJZdL17lhWy指定一个成本参数10个,表明210个关键扩张轮。该盐是N9qo8uLOickgx2ZMRZoMye并且得到的散列是IjZAgcfl7p92ldGxad68LJZdL17lhWy。

因此,如果密码与散列相匹配,则散列的次数与原始散列相似。

换句话说:在matches()是独立设置的,而且很可能是静态的......