gitlab就使用默认的bcrypt作为密码的加密算法
关于bcrypt相关信息:
通过salt和cost这两个值来减缓加密过程,ta的加密时间(百ms级)远远超过md5(大概1ms左右);
cost值我暂时把ta当作是迭代次数;RoR的devise组件默认的cost是10,但其实推荐的值是12;
bcrypt是单向的,而且经过salt和cost的处理,使其受rainbow攻击破解的概率大大降低,同时破解的难度也提升不少;
bcrypt加密后的字符串形如:$2a$10$asdjflkaydgigadfahgl.asdfaoygoqhgasldhf,其中:$是分割符,无意义;2a是bcrypt加密版本号;10是cost的值;而后的前22位是salt值;再然后的字符串就是密码的密文了;
java代码相关:
依赖
<!--加密-->
<dependency>
<groupId>org.mindrot</groupId>
<artifactId>jbcrypt</artifactId>
<version>0.4</version>
</dependency>
String password = "12345678";
/**
* 生成随机的盐
*/
System.out.println(BCrypt.gensalt());
/**
* 对密码password 进行加密
*/
String s = BCrypt.hashpw( password,BCrypt.gensalt() );
System.out.println(s);
//盐:$2a$10$9sM0WeC3WSDCx9HWwr7z7e
//加密后:$2a$10$9sM0WeC3WSDCx9HWwr7z7eV3h3Q/tPxP7G2kvCWxWqdJemid1lICa
/**
* 解密
*
*
* s1 : 是加密后的密码
* password : 是用户输入的密码
*/
Boolean b = BCrypt.checkpw(password, s );
false 说明两个密码不一样
true :说明密码匹配成功
System.out.println(b);