php中写salt,php – 使用sha1和salt存储密码

我有一个简单的注册脚本在PHP完成,我只是好奇,如果我这样做的方式足够安全,以存储用户密码.我正在生成一个32位随机盐并将其附加到sha1哈希密码.

//create new validator object

$validator = new data_validation();

//validate user input

$firstName = $validator->validate_fname($firstName); //is the first name a string?

$lastName = $validator->validate_lname($lastName); // is the last name a string?

$username = $validator->validate_username($username); // is the username a string?

$email = $validator->validate_email($email); //is the email in valid format?

//make sure there isn't duplicate emails

$valQuery = $link->query("SELECT email FROM users WHERE email = '" .$email. "'");

if ($valQuery->num_rows == 1) {

echo "An email is already registered with that address";

return false;

}

// generate a random salt for converting passwords into sha1

$salt = $link->real_escape_string(bin2hex(mcrypt_create_iv(32, MCRYPT_DEV_URANDOM)));

$saltedPW = $password . $salt;

$hashedPW = sha1($saltedPW);

mysqli_connect($db_host, $db_user, $db_pass) OR DIE (mysqli_error());

// select the db

mysqli_select_db ($link, $db_name) OR DIE ("Unable to select db".mysqli_error($db_name));

// our sql query

$sql = "INSERT INTO users (first_name, last_name, username, email, password, salt) VALUES ('$firstName', '$lastName', '$username', '$email', '$hashedPW', '$salt');";

//save the updated information to the database

$result = mysqli_query($link, $sql) or die("Error in Query: " . mysqli_error($link));

if (!mysqli_error($link))

{

$row = mysqli_fetch_assoc($result);

$_SESSION['user_id'] = $row['user_id'];

$_SESSION['loggedin'] = TRUE;

header("Location: ../home");

}

此外,我正在使用程序和oop php的组合.其中大部分都是在过程中完成的,但是有一些oop类,比如你在上面的脚本中看到的验证类.这是否会导致使用这两种样式的性能问题?

解决方法:

最重要的是:

Use scrypt when you can; bcrypt if you cannot.

Use PBKDF2 if you cannot use either bcrypt or scrypt.

有关PBKDF2,bcrypt和scrypt的比较,请参阅this answer.

[MD5, SHA1, SHA256, SHA512, SHA-3, etc] are all general purpose hash functions, designed to calculate a digest of huge amounts of data in as short a time as possible. This means that they are fantastic for ensuring the integrity of data and utterly rubbish for storing passwords.

PHPass可能是在PHP中进行bcrypt散列的最简单方法.如果你愿意,也可以使用crypt function和CRYPT_BLOWFISH进行do it the hard way,但要注意有很多方法可以解决它,并且界面相当神秘(就像你指定盐值一样).

标签:php,hash,mysqli,salt,sha1

来源: https://codeday.me/bug/20190725/1536907.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个Java实现的密码生成工具类,使用了随机生成的salt和1024次SHA-1哈希函数加密: ```java import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.security.SecureRandom; public class PasswordGenerator { private static final int SALT_LENGTH = 16; // salt的长度 private static final int HASH_ITERATIONS = 1024; // 哈希迭代次数 /** * 生成随机的salt * @return salt的字节数组 */ public static byte[] generateSalt() { SecureRandom random = new SecureRandom(); byte[] salt = new byte[SALT_LENGTH]; random.nextBytes(salt); return salt; } /** * 生成经过1024次SHA-1加密的密码 * @param password 原始密码 * @param salt salt的字节数组 * @return 经过加密的密码的字节数组 * @throws NoSuchAlgorithmException */ public static byte[] generateHashedPassword(String password, byte[] salt) throws NoSuchAlgorithmException { MessageDigest digest = MessageDigest.getInstance("SHA-1"); digest.reset(); digest.update(salt); byte[] hashedPassword = digest.digest(password.getBytes()); for (int i = 0; i < HASH_ITERATIONS - 1; i++) { digest.reset(); hashedPassword = digest.digest(hashedPassword); } return hashedPassword; } /** * 将字节数组转换为十六进制的字符串 * @param bytes 待转换的字节数组 * @return 转换后的十六进制字符串 */ public static String bytesToHex(byte[] bytes) { StringBuilder hexString = new StringBuilder(); for (byte b : bytes) { hexString.append(String.format("%02x", b)); } return hexString.toString(); } } ``` 使用示例: ```java try { // 生成随机的salt byte[] salt = PasswordGenerator.generateSalt(); System.out.println("Salt: " + PasswordGenerator.bytesToHex(salt)); // 生成经过1024次SHA-1加密的密码 String password = "123456"; byte[] hashedPassword = PasswordGenerator.generateHashedPassword(password, salt); System.out.println("Hashed password: " + PasswordGenerator.bytesToHex(hashedPassword)); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } ``` 输出结果: ``` Salt: d4c5a0c1a6d7e38d46d6b98187b0c5fd Hashed password: 9b7f3e15b2e8ce3a3dc7f1b3d56aa6e9f231a1b5 ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值