Java通过BCrypt加密

一、概述
在用户模块,对于用户密码的保护,通常都会进行加密。我们通常对密码进行加密,然后存放在数据库中,在用户进行登录的时候,将其输入的密码进行加密然后与数据库中存放的密文进行比较,以验证用户密码是否正确。
目前,MD5和BCrypt比较流行。相对来说,BCrypt比MD5更安全,但加密更慢。

二、使用BCrypt
首先,可以在官网中取得源代码 http://www.mindrot.org/projects/jBCrypt/ 然后通过 Ant 进行编译。编译之后得到 jbcrypt.jar 。也可以不需要进行编译,而直接使用源码中的 java 文件(本身仅一个文件)。
下面是官网的一个Demo。

public class BCryptDemo {
public static void main(String[] args) {
// Hash a password for the first time
String password = “testpassword”;
String hashed = BCrypt.hashpw(password, BCrypt.gensalt());
System.out.println(hashed);
// gensalt’s log_rounds parameter determines the complexity
// the work factor is 2**log_rounds, and the default is 10
String hashed2 = BCrypt.hashpw(password, BCrypt.gensalt(12));

    // Check that an unencrypted password matches one that has
    // previously been hashed
    String candidate = "testpassword";
    //String candidate = "wrongtestpassword";
    if (BCrypt.checkpw(candidate, hashed))
        System.out.println("It matches");
    else
        System.out.println("It does not match");
}

}
在这个例子中,

BCrypt.hashpw(password, BCrypt.gensalt())
是核心。通过调用 BCrypt 类的静态方法 hashpw 对password进行加密。第二个参数就是我们平时所说的加盐。

BCrypt.checkpw(candidate, hashed)
该方法就是对用户后来输入的密码进行比较。如果能够匹配,返回true。

三、加盐
如果两个人或多个人的密码相同,加密后保存会得到相同的结果。破一个就可以破一片的密码。如果名为A的用户可以查看数据库,那么他可以观察到自己的密码和别人的密码加密后的结果都是一样,那么,别人用的和自己就是同一个密码,这样,就可以利用别人的身份登录了。
其实只要稍微混淆一下就能防范住了,这在加密术语中称为“加盐”。具体来说就是在原有材料(用户自定义密码)中加入其它成分(一般是用户自有且不变的因素),以此来增加系统复杂度。当这种盐和用户密码相结合后,再通过摘要处理,就能得到隐蔽性更强的摘要值。

四、Go 实现
import (
“fmt”

"golang.org/x/crypto/bcrypt"

)

// go get golang.org/x/crypto/bcrypt

func main() {
password := []byte(“somepassword”) //

// Hashing the password with the default cost of 10
hashedPassword, err := bcrypt.GenerateFromPassword(password, bcrypt.DefaultCost)
if err != nil {
    panic(err)
}
fmt.Println(string(hashedPassword))

// Comparing the password with the hash
err = bcrypt.CompareHashAndPassword(hashedPassword, password)
fmt.Println(err) // nil means it is a match

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值