password hashing

今天科技助盲遇到Unicode-objects must be encoded before hashing的问题,debug的时候了解了一下password hashing,在这里记录一下。

服务器在数据库一般不存储用户密码,只存储密码哈希之后的值,以保证如果网站被黑,用户的信息可以不被窃取。

目前的哈希方法主要有SHA-1,SHA-256,MD5。用户在登录的时候对数值进行再哈希后比对就可以了。

目前还是有其他攻击方法破解用户密码,主要是暴力破解,暴力破解的难度随着密码复杂度的提高而增加。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Sure! In Java, you can use a salt-based encryption technique to secure passwords. Here's an example of how you can achieve this using the Java Cryptography Architecture (JCA): ```java import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.security.SecureRandom; import java.util.Base64; public class PasswordEncryption { public static void main(String[] args) { String password = "password123"; // Replace with the actual // Generate a random salt byte[] salt = generateSalt(); // Hash the password with the salt String hashedPassword = hashPassword(password, salt); System.out.println("Salt: " + Base64.getEncoder().encodeToString(salt)); System.out.println("Hashed Password: " + hashedPassword); } private static byte[] generateSalt() { SecureRandom random = new SecureRandom(); byte[] salt = new byte[16]; random.nextBytes(salt); return salt; } private static String hashPassword(String password, byte[] salt) { try { MessageDigest md = MessageDigest.getInstance("SHA-256"); md.update(salt); byte[] hashedPassword = md.digest(password.getBytes()); return Base64.getEncoder().encodeToString(hashedPassword); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); return null; } } } ``` In this example, we use the SHA-256 hashing algorithm to hash the password. The `generateSalt()` method generates a random salt, and the `hashPassword()` method hashes the password using the salt. The salt and hashed password are then printed to the console for demonstration purposes. Remember to replace `"password123"` with the actual password you want to encrypt. Also, ensure that you store the generated salt securely along with the hashed password for future verification.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值