简体   繁体   English

如何使用browserify运行节点加密模块

[英]How to run node crypto module with browserify

I need to use crypto.pbkdf2 on my browser. 我需要在浏览器上使用crypto.pbkdf2

I use browserify to create my javascript file. 我使用browserify来创建我的javascript文件。 The asynchronous PBKDF2 function freeze my browser entirely when iterations are larger than 1000. 当迭代次数大于1000时,异步PBKDF2函数会完全冻结我的浏览器。

pbkdf2冻结

RequireBin example RequireBin示例

var crypto = require('crypto');
var iterations = 10;
// var iterations = 8192; // uncomment to freeze the browser
crypto.pbkdf2('password', 'salt', iterations, 32, 'sha256', function (error, key) {
    console.log(key.toString('hex'));
});

How to run node crypto module with browserify ? 如何使用browserify运行节点加密模块?

Edit: 编辑:

here the code created by browserify to declare pbkdf2 这里是browserify创建的代码,用于声明pbkdf2

exports.pbkdf2 = pbkdf2
function pbkdf2 (password, salt, iterations, keylen, digest, callback) {
  if (typeof digest === 'function') {
    callback = digest
    digest = undefined
  }

  if (typeof callback !== 'function') {
    throw new Error('No callback provided to pbkdf2')
  }

  var result = pbkdf2Sync(password, salt, iterations, keylen, digest)
  setTimeout(function () {
    callback(undefined, result)
  })
}

To fix my problem I use the pbkdf2 module from npm: https://github.com/crypto-browserify/pbkdf2 为了解决我的问题,我使用npm中的pbkdf2模块: https//github.com/crypto-browserify/pbkdf2

npm install --save pbkdf2

This package implement a browser version and proxy the node version 此包实现浏览器版本并代理节点版本

Depending on the target you want to reach (browser or node) your bundler (browserify or webpack) will load the appropriate version. 根据您要访问的目标(浏览器或节点),您的bundler(browserify或webpack)将加载相应的版本。

var pbkdf2 = require('pbkdf2');
pbkdf2.pbkdf2(password, salt, iterations, keylen, digest, function(error, key) {
...
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM