当前位置: 首页 > 工具软件 > Bcrypt > 使用案例 >

BCrypt加密使用方法

何玺
2023-12-01

只是为了了解一下除了MD5以外,其他的加密方式,以后还可能会写其他加密的使用方法

MD5和BCrypt比较流行。相对来说,BCrypt比MD5更安全,BCrypt算法的优点是计算速度慢,你没看错,我说的就是计算速度慢,它还可以通过参数调节速度,要多慢有多慢

官方源码:http://www.mindrot.org/projects/jBCrypt/

源码中的几个重要的方法如下:

1. 对password加盐值
BCrypt.hashpw(String password, String salt);

2. 比对password(如果相同返回true)
BCrypt.checkpw(String plaintext, String hashed);

3. 获取盐值
BCrypt.gensalt();

实例如下:

public static void main(String[] args) {
	//加密(参数1:待加密的密码,参数2:调用获取盐值的方法)
    String hashpw = BCrypt.hashpw("123qwe", BCrypt.gensalt());
    System.out.println(hashpw);

	//判断密码是否相同
    boolean checkpw1 = BCrypt.checkpw("123qwe", hashpw);
    System.out.println(checkpw1);//true

    boolean checkpw2 = BCrypt.checkpw("123456", hashpw);
    System.out.println(checkpw2);//false
}
 类似资料: