本文最后更新于:2025年2月18日 下午
由于MD5
加密存在撞库的漏洞(尽管那概率很小),所以现在并不适合用来加密用户的密码。
目前的用处一般在文件完整性检查
和数据去重
上。
替代方案
Bcrypt
加密,这也是一种单向加密算法。也是SpringSecurity
中的加密算法之一。
使用方法
依赖
1 2 3 4 5 6
| <dependency> <groupId>org.mindrot</groupId> <artifactId>jbcrypt</artifactId> <version>0.4</version> </dependency>
|
工具类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
| import org.mindrot.jbcrypt.BCrypt;
public class PasswordUtil {
public static String hashPassword(String password) { return BCrypt.hashpw(password, BCrypt.gensalt()); }
public static boolean checkPassword(String password, String hashed) { return BCrypt.checkpw(password, hashed); }
public static void main(String[] args) { String password = "mySecretPassword"; String hashedPassword = hashPassword(password); System.out.println("Hashed Password: " + hashedPassword); boolean isMatch = checkPassword(password, hashedPassword); System.out.println("Password matches: " + isMatch); } }
|