如何生成用于散列的随机长盐? [英] How to generate a random, long salt for use in hashing?

查看:91
本文介绍了如何生成用于散列的随机长盐?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

PHP用什么方法制作用于散列的随机可变长度的盐?假设我想制作16个字符的长盐-我该怎么做?

解决方案

edit:mcrypt扩展名已被弃用. 对于新项目,请查看
random_bytes(int $ length)
(自php 7.2核心开始)扩展. /p>


如果 mcrypt扩展可用,您可以简单地使用

What is a way in PHP to make a random, variable length salt for use in hashing? Let's say I want to make a 16-character long salt - how would I do it?

edit: the mcrypt extension has been deprecated. For new projects take a look at
random_bytes ( int $length )
and the sodium (as of php 7.2 core-)extension.


If the mcrypt extension is available you could simply use mcrypt_create_iv(size, source) to create a salt.

$iv = mcrypt_create_iv(16, MCRYPT_DEV_URANDOM);
var_dump($iv);

Since each byte of the "string" can be in the range between 0-255 you need a binary-safe function to save/retrieve it.

这篇关于如何生成用于散列的随机长盐?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆