使用 Spring Boot 实现 MD5 加盐算法:提升密码保

在Spring Boot中实现MD5加盐算法,可以通过编写一个工具类来生成加盐的MD5哈希值。下面是一个简单的示例代码,演示如何实现这一点:

import org.springframework.stereotype.Component;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.Base64;
@Component
public class HashUtil {
// 生成随机盐值
public static String generateSalt() {
SecureRandom random = new SecureRandom();
byte[] salt = new byte[16];
random.nextBytes(salt);
return Base64.getEncoder().encodeToString(salt);
}
// 使用MD5生成加盐哈希
public static String hashWithSalt(String input, String salt) {
try {
MessageDigest md = MessageDigest.getInstance("MD5");
String saltedInput = input + salt;
byte[] hashedBytes = md.digest(saltedInput.getBytes());
// 将字节数组转换为十六进制字符串
StringBuilder sb = new StringBuilder();
for (byte b : hashedBytes) {
sb.append(String.format("%02x", b));
}
return sb.toString();
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException("MD5 algorithm not found", e);
}
}
public static void main(String[] args) {
String originalPassword = "myPassword";
// 生成盐值
String salt = generateSalt();
System.out.println("Generated Salt: " + salt);
// 生成加盐的哈希值
String hashedPassword = hashWithSalt(originalPassword, salt);
System.out.println("Hashed Password with Salt: " + hashedPassword);
}
}
import org.springframework.stereotype.Component;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.Base64;

@Component
public class HashUtil {

    // 生成随机盐值
    public static String generateSalt() {
        SecureRandom random = new SecureRandom();
        byte[] salt = new byte[16];
        random.nextBytes(salt);
        return Base64.getEncoder().encodeToString(salt);
    }

    // 使用MD5生成加盐哈希
    public static String hashWithSalt(String input, String salt) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            String saltedInput = input + salt;
            byte[] hashedBytes = md.digest(saltedInput.getBytes());
            
            // 将字节数组转换为十六进制字符串
            StringBuilder sb = new StringBuilder();
            for (byte b : hashedBytes) {
                sb.append(String.format("%02x", b));
            }
            return sb.toString();
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException("MD5 algorithm not found", e);
        }
    }

    public static void main(String[] args) {
        String originalPassword = "myPassword";
        
        // 生成盐值
        String salt = generateSalt();
        System.out.println("Generated Salt: " + salt);
        
        // 生成加盐的哈希值
        String hashedPassword = hashWithSalt(originalPassword, salt);
        System.out.println("Hashed Password with Salt: " + hashedPassword);
    }
}
import org.springframework.stereotype.Component; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.security.SecureRandom; import java.util.Base64; @Component public class HashUtil { // 生成随机盐值 public static String generateSalt() { SecureRandom random = new SecureRandom(); byte[] salt = new byte[16]; random.nextBytes(salt); return Base64.getEncoder().encodeToString(salt); } // 使用MD5生成加盐哈希 public static String hashWithSalt(String input, String salt) { try { MessageDigest md = MessageDigest.getInstance("MD5"); String saltedInput = input + salt; byte[] hashedBytes = md.digest(saltedInput.getBytes()); // 将字节数组转换为十六进制字符串 StringBuilder sb = new StringBuilder(); for (byte b : hashedBytes) { sb.append(String.format("%02x", b)); } return sb.toString(); } catch (NoSuchAlgorithmException e) { throw new RuntimeException("MD5 algorithm not found", e); } } public static void main(String[] args) { String originalPassword = "myPassword"; // 生成盐值 String salt = generateSalt(); System.out.println("Generated Salt: " + salt); // 生成加盐的哈希值 String hashedPassword = hashWithSalt(originalPassword, salt); System.out.println("Hashed Password with Salt: " + hashedPassword); } }

代码说明:

  1. 生成盐值
    • 使用SecureRandom生成一个16字节的随机盐值,并通过Base64编码为字符串。
  2. 加盐哈希
    • 将输入字符串与盐值连接。
    • 使用MessageDigest的MD5算法对连接后的字符串进行哈希。
    • 将生成的字节数组转换为十六进制字符串表示。
  3. 主方法
    • 演示如何生成盐值和加盐的MD5哈希值。
图片[1]_使用 Spring Boot 实现 MD5 加盐算法:提升密码保_知途无界

注意事项:

  • 安全性:MD5虽然常用,但已被证明不够安全,容易受到碰撞攻击。在生产环境中,建议使用更安全的哈希算法,如SHA-256或结合HMAC。
  • 盐值存储:在实际应用中,盐值需要与用户数据一起存储(例如,存储在数据库中),以便在验证密码时可以使用相同的盐值。
  • 密码存储:永远不要存储明文密码,只存储加盐哈希后的密码。

通过这种方式,你可以有效地增加密码存储的安全性,防止彩虹表攻击。

© 版权声明
THE END
喜欢就点个赞,支持一下吧!
点赞34 分享
The sacrifices you make today will pay dividends in the future.
今天的牺牲和努力未来都会有回报
评论 抢沙发
头像
欢迎您留下评论!
提交
头像

昵称

取消
昵称表情代码图片

    暂无评论内容