加密
BCryptPasswordEncoder encode = new BCryptPasswordEncoder(); encode.encode(password);
比較
matches(CharSequence rawPassword, String encodedPassword)
需要通過自帶的方法 matches 將未經過加密的密碼和已經過加密的密碼傳進去進行判斷,返回布爾值。
舉例
public class BCryptPasswordEncoderTest { public static void main(String[] args) { String pass = "admin"; BCryptPasswordEncoder bcryptPasswordEncoder = new BCryptPasswordEncoder(); String hashPass = bcryptPasswordEncoder.encode(pass); System.out.println(hashPass); boolean flag = bcryptPasswordEncoder.matches("admin",hashPass); System.out.println(flag); }
}
可以看到,每次輸出的hashPass 都不一樣,但是最終的flag都為 true,即匹配成功。
查看代碼,可以看到,其實每次的隨機鹽,都保存在hashPass中。在進行matchs進行比較時,調用BCrypt 的String hashpw(String password, String salt)方法。兩個參數即”admin“和 hashPass。
//******BCrypt.java******salt即取出要比較的DB中的密碼******* real_salt = salt.substring(off + 3, off + 25); try { // *************************************************** passwordb = (password + (minor >= 'a' ? "\000" : "")).getBytes("UTF-8"); } catch (UnsupportedEncodingException uee) {} saltb = decode_base64(real_salt, BCRYPT_SALT_LEN); B = new BCrypt(); hashed = B.crypt_raw(passwordb, saltb, rounds);
假定一次hashPass為:$2a$10$AxafsyVqK51p.s9WAEYWYeIY9TKEoG83LTEOSB3KUkoLtGsBKhCwe
隨機鹽即為 AxafsyVqK51p.s9WAEYWYe(salt = BCrypt.gensalt();中有描述),可見,隨機鹽(AxafsyVqK51p.s9WAEYWYe),會在比較的時候,重新被取出。
即,加密的hashPass中,前部分已經包含了鹽信息。