[转]php hash_pbkdf2 和 node.js crypto.pbkdf2
http://php.net/manual/en/function.hash-pbkdf2.php
http://php.net/manual/en/function.hash-pbkdf2.php
hash_pbkdf2
(PHP 5 >= 5.5.0, PHP 7)
hash_pbkdf2 — Generate a PBKDF2 key derivation of a supplied password
Description
$algo , string $password , string $salt , int $iterations [, int $length = 0 [, bool$raw_output = FALSE ]] )Parameters
algo-
Name of selected hashing algorithm (i.e. md5, sha256, haval160,4, etc..) See hash_algos() for a list of supported algorithms.
password-
The password to use for the derivation.
salt-
The salt to use for the derivation. This value should be generated randomly.
iterations-
The number of internal iterations to perform for the derivation.
length-
The length of the output string. If
raw_outputisTRUEthis corresponds to the byte-length of the derived key, ifraw_outputisFALSEthis corresponds to twice the byte-length of the derived key (as every byte of the key is returned as two hexits).If 0 is passed, the entire output of the supplied algorithm is used.
raw_output-
When set to
TRUE, outputs raw binary data.FALSEoutputs lowercase hexits.
Return Values
Returns a string containing the derived key as lowercase hexits unless raw_output is set to TRUE in which case the raw binary representation of the derived key is returned.
Errors/Exceptions
An E_WARNING will be raised if the algorithm is unknown, the iterations parameter is less than or equal to 0, the length is less than 0 or the salt is too long (greater than INT_MAX - 4).
Changelog
| Version | Description |
|---|---|
| 7.2.0 | Usage of non-cryptographic hash functions (adler32, crc32, crc32b, fnv132, fnv1a32, fnv164, fnv1a64, joaat) was disabled. |
Examples
Example #1 hash_pbkdf2() example, basic usage
<?php
$password = "password";
$iterations = 1000;
// Generate a random IV using openssl_random_pseudo_bytes()
// random_bytes() or another suitable source of randomness
$salt = openssl_random_pseudo_bytes(16);
$hash = hash_pbkdf2("sha256", $password, $salt, $iterations, 20);
echo $hash;
?>
The above example will output something similar to:
120fb6cffcf8b32c43e7
Notes
The PBKDF2 method can be used for hashing passwords for storage. However, it should be noted that password_hash() or crypt()with CRYPT_BLOWFISH are better suited for password storage.
crypto.pbkdf2(password, salt, iterations, keylen, digest, callback)#
password<string> | <Buffer> | <TypedArray> | <DataView>salt<string> | <Buffer> | <TypedArray> | <DataView>iterations<number>keylen<number>digest<string>callback<Function>
Provides an asynchronous Password-Based Key Derivation Function 2 (PBKDF2) implementation. A selected HMAC digest algorithm specified by digest is applied to derive a key of the requested byte length (keylen) from thepassword, salt and iterations.
The supplied callback function is called with two arguments: err and derivedKey. If an error occurs while deriving the key, err will be set; otherwise err will be null. By default, the successfully generated derivedKey will be passed to the callback as a Buffer. An error will be thrown if any of the input arguments specify invalid values or types.
If digest is null, 'sha1' will be used. This behavior is deprecated, please specify a digest explicitely.
The iterations argument must be a number set as high as possible. The higher the number of iterations, the more secure the derived key will be, but will take a longer amount of time to complete.
The salt should be as unique as possible. It is recommended that a salt is random and at least 16 bytes long. See NIST SP 800-132 for details.
const crypto = require('crypto');
crypto.pbkdf2('secret', 'salt', 100000, 64, 'sha512', (err, derivedKey) => {
if (err) throw err;
console.log(derivedKey.toString('hex')); // '3745e48...08d59ae'
});
The crypto.DEFAULT_ENCODING property can be used to change the way the derivedKey is passed to the callback. This property, however, has been deprecated and use should be avoided.
const crypto = require('crypto');
crypto.DEFAULT_ENCODING = 'hex';
crypto.pbkdf2('secret', 'salt', 100000, 512, 'sha512', (err, derivedKey) => {
if (err) throw err;
console.log(derivedKey); // '3745e48...aa39b34'
});
An array of supported digest functions can be retrieved using crypto.getHashes().
Note that this API uses libuv's threadpool, which can have surprising and negative performance implications for some applications, see the UV_THREADPOOL_SIZE documentation for more information.
[转]php hash_pbkdf2 和 node.js crypto.pbkdf2的更多相关文章
- Node.js crypto加密模块汇总
第一篇文章:MD5 和 SHA家族 概述:使用Node实现较为简单的Hash加密算法,本篇实际上重不在Hash加密,主要的还是为了引出crypto加密的三种方式 第二篇文章:HMAC 概述:密钥相关的 ...
- Node.js Crypto 加密算法库
Crypto库是随Nodejs内核一起打包发布的,主要提供了加密.解密.签名.验证等功能.Crypto利用OpenSSL库来实现它的加密技术,它提供OpenSSL中的一系列哈希方法,包括hmac.ci ...
- Node.js 内置模块crypto加密模块(4) Diffie Hellman
Diffie-Hellman( DH ):密钥交换协议/算法 ( Diffie-Hellman Key Exchange/Agreement Algorithm ) 百科摘录: Diffie-Hell ...
- 88.NODE.JS加密模块CRYPTO常用方法介绍
转自:https://www.jb51.net/article/50668.htm 使用require('crypto')调用加密模块. 加密模块需要底层系统提供OpenSSL的支持.它提供了一种安全 ...
- 记一次在node.js中使用crypto的createCipheriv方法进行加密时所遇到的坑
Node.js的crypto模块提供了一组包括对OpenSSL的哈希.HMAC.加密.解密.签名,以及验证等一整套功能的封装.具体的使用方法可以参考这篇文章中的描述:node.js_crypto模块. ...
- 转:Node.js软肋之CPU密集型任务
文章来自于:http://www.infoq.com/cn/articles/nodejs-weakness-cpu-intensive-tasks Node.js在官网上是这样定义的:“一个搭建在C ...
- Node.js 加密
稳定性: 2 - 不稳定; 正在讨论未来版本的 API 改进,会尽量减少重大变化.详见后文. 使用 require('crypto') 来访问这个模块. 加密模块提供了 HTTP 或 HTTPS 连接 ...
- Node.js:理解stream
Stream在node.js中是一个抽象的接口,基于EventEmitter,也是一种Buffer的高级封装,用来处理流数据.流模块便是提供各种API让我们可以很简单的使用Stream. 流分为四种类 ...
- Node.js API 初解读(二)
四. Cluster 1.简介 在介绍 Cluster 之前.我们需要知道 node的 一些基本特性,比如说 都知道的 nodejs最大的特点就是单进程.无阻塞运行,并且是异步事件驱动的. 那么随之而 ...
随机推荐
- 基于UML的时空建模
一.基本信息 标题:基于UML的时空建模 时间:2018 出版源:东北大学学报(自然科学版) 领域分类:UML模型:RCC-8空间拓扑:Allen-13时态拓扑:时空数据:建模 二.研究背景 问题定义 ...
- P层
package net.goeasyway.uploadimage.presenter; import net.goeasyway.uploadimage.model.Photo;import net ...
- spring-data-redis和jedis版本对应收集总结
基于spring主版本为4.3.13.RELEASE的项目,测试以下对应版本可用. spring-data-redis版本 jedis版本 备注 1.5.2.RELEASE 2.7.3 1.6.0 ...
- Android开发 - 掌握ConstraintLayout(九)分组(Group)
使用ConstraintLayout后我们的布局是没有层级关系的,各个View之间都是平级关系,但是如果根据某个业务条件来控制多个View的显示与否,我们需要分别对每个View进行控制,需要调用多次s ...
- C 线性表的链式存储实现及插入、删除等操作示例
一.链式存储的优势 线性表的存储可以通过顺序存储或链式存储实现,其中顺序存储基于数组实现(见本人上一篇博客),在进行插入删除等操作时,需对表内某一部分元素逐个移动,效率较低.而链式结构不依赖于地址连续 ...
- php过滤 字符
今天在抓取页面中得到字符串:"卡牌 ",使用str_replace . preg_replace 和 strip_tags过滤都无解. 最后google到2种方式,如下: str_ ...
- jsp进阶
Jsp,前段的数据读取到后端 获取前段输入框数据 request.getParameter(前段输入框的name值) Request.代表转发,(代码在服务器内部执行的一次性请求,url地址不会发生改 ...
- Target优化
优化目标主要包括以下几方面: 1 优化平面文件 如果目标平面文件在某机器的共享目录下,则该机器最好是专门用于文件存储的,如果还应用于其他非文件存储任务,则会降低加载效率 如果Integration s ...
- 【hadoop】1、MapReduce进行日志分析,并排序统计结果
1.网上很多关于搭建Hadoop集群的知识,这里不多做叙述,并且本机运行Hadoop程序是不需要hdfs集群的,我们本机运行只做个demo样式,当真的需要运行大数据的时候,才需要真正的集群 2.还有就 ...
- web容器 web服务器 servlet/jsp容器 之间的区别和关系
今天学习tomcat时发现一篇写的比较好的文章,故分享给大家 Web服务器(软件): Apache http server, 这个它的网址,http://httpd.apache.org/downlo ...