我试图找出如何使用crypto模块在nodejs中加密和散列密码.我可以创建哈希密码来做到这一点:
UserSchema.pre('save', function(next) {
var user = this;
var salt = crypto.randomBytes(128).toString('base64');
crypto.pbkdf2(user.password, salt, 10000, 512, function(err, derivedKey) {
user.password = derivedKey;
next();
});
});
Run Code Online (Sandbox Code Playgroud)
但是我对如何验证密码感到困惑.
UserSchema.methods.validPassword = function(password) {
// need to salt and hash this password I think to compare
// how to I get the salt?
}
Run Code Online (Sandbox Code Playgroud) 我试图在NodeJS中散列变量,如下所示:
var crypto = require('crypto');
var hash = crypto.createHash('sha256');
var code = 'bacon';
code = hash.update(code);
code = hash.digest(code);
console.log(code);
Run Code Online (Sandbox Code Playgroud)
但看起来我误解了文档作为console.log没有记录一个哈希版本的培根,而只是一些关于SlowBuffer的信息.
这样做的正确方法是什么?
我在我的共享主机上部署的ASP.NET MVC应用程序上遇到自定义错误问题.我创建了一个ErrorController并将以下代码添加到Global.asax以捕获未处理的异常,记录它们,然后将控制转移到ErrorController以显示自定义错误.此代码取自此处:
protected void Application_Error(object sender, EventArgs e)
{
Exception ex = Server.GetLastError();
Response.Clear();
HttpException httpEx = ex as HttpException;
RouteData routeData = new RouteData();
routeData.Values.Add("controller", "Error");
if (httpEx == null)
{
routeData.Values.Add("action", "Index");
}
else
{
switch (httpEx.GetHttpCode())
{
case 404:
routeData.Values.Add("action", "HttpError404");
break;
case 500:
routeData.Values.Add("action", "HttpError500");
break;
case 503:
routeData.Values.Add("action", "HttpError503");
break;
default:
routeData.Values.Add("action", "Index");
break;
}
}
ExceptionLogger.LogException(ex); // <- This is working. Errors get logged
routeData.Values.Add("error", ex);
Server.ClearError();
IController controller = new ErrorController(); …Run Code Online (Sandbox Code Playgroud) 我在我的应用程序中使用加密模块.似乎在nodejs http://nodejs.org/api/crypto.html中分布了加密模块, 所以我需要做npm install crypto吗?有什么区别来自https://npmjs.org/package/crypto和nodejs api加密模块?
问题
我有node.js模块crypto.createHash用于生成md5哈希.
最近我注意到crypto模块生成的哈希在新版本中有所不同:
码
require('crypto').createHash('md5').update('¥').digest('hex')
Run Code Online (Sandbox Code Playgroud)
Node.js v0.10.0
输出: ab3af8566ddd20d7efc9b314abe90755
Node.js v6.1.0
输出: 07625e142e4ac5961de57472657a88c1
题
我想知道新版本的原因是什么,我该如何解决?
更新
关于GitHub的类似问题:
有没有一种很好的方法来使用公钥验证Node.JS(v0.4 +)中的签名?
当前的加密模块允许使用证书但不允许使用公钥.例如:
var crypto = require("crypto");
verifier = crypto.createVerifier("sha1");
verifier.update("signed data");
verifier.verify(CERT, signature);
Run Code Online (Sandbox Code Playgroud)
变量CERT需要签名证书(我猜公钥是从中提取的)但我所拥有的只是公钥而不是证书.
只有实现这一点的可靠方法似乎是将数据,公钥和签名的内容转储到文件中并执行 openssl dgst
fs.writeFileSync("public.key", pubkey);
fs.writeFileSync("sig.sha1", signature);
fs.writeFileSync("data.txt", data);
exec("openssl dgst -sha1 -verify public.key -signature sig.sha1 data.txt", ...)
Run Code Online (Sandbox Code Playgroud)
但是每次我需要验证签名时创建(和删除)文件似乎是完全浪费.
任何好的想法如何做得更好?
更新2011-08-03
Node.js v0.5中的加密模块允许使用证书和公钥 (RSA或X.509)进行验证
我在使用Node的加密库时遇到了奇怪的问题.我写了这个简单的AES测试脚本:
var cipher = crypto.createCipher('aes-256-cbc','InmbuvP6Z8')
var text = "123|123123123123123";
cipher.update(text,'utf8','hex')
var crypted = cipher.final('hex')
var decipher = crypto.createDecipher('aes-256-cbc','InmbuvP6Z8')
decipher.update(crypted,'hex','utf8')
var dec = decipher.final('utf8')
Run Code Online (Sandbox Code Playgroud)
当我执行console.log(dec)时,它为null.出于某种原因,如果我将测试设置为"123 | 123123",它可以工作.那么为什么"123 | 123123"工作但"123 | 123123123123123"不工作?
如果我决定为我的服务器使用http模块,我需要执行以下哪些模块/方法?
谢谢.
我想使用node.js crypto lib创建一个salt-hash,而不必解析任何硬编码数据.
硬编码我的意思是什么?
var salt, hardcodedString = "8397dhdjhjh";
crypto.createHmac('sha512', hardcodedString).update(salt).digest("base64");
Run Code Online (Sandbox Code Playgroud)
如果不使用原始javascript,随机函数或硬编码,我怎么能创建一个随机字符串?
问候
UPDATE
var Crypto = require('crypto')
, mongoose = require('mongoose');
module.exports = mongoose.model('User', new mongoose.Schema({
username: {
type: String
, required: true
, index: { unique: true, sparse: true }
, set: toLower
},
email: {
type: String
, required: true
, index: { unique: true, sparse: true }
, set: toLower
},
salt: {
type: String
, set: generateSalt
},
password: {
type: String
, set: encodePassword …Run Code Online (Sandbox Code Playgroud) 我正在编写一个简单的计算器应用程序(使用带有3个编辑框和一些按钮的绝对布局),它有两个输入文本框和一个输出框.
input1 = (EditText) findViewById(R.id.input1);
input2 = (EditText) findViewById(R.id.input2);
Run Code Online (Sandbox Code Playgroud)
现在,一旦用户输入一些数字到input1并按'+',现在我想将焦点从input1转移到input2.我怎样才能做到这一点?
我在'+'的按键上尝试了下面的代码
onClick(View arg0){
operator.setText("+");
//Move focus from input1 to input2
input1.clearFocus();
input2.setNextFocusDownId(input2.getId());
}
Run Code Online (Sandbox Code Playgroud)
但这不起作用..你可以帮我这个吗?
node.js ×8
node-crypto ×7
cryptography ×2
javascript ×2
aes ×1
android ×1
asp.net-mvc ×1
encryption ×1
focus ×1
global-asax ×1
hash ×1
npm ×1
openssl ×1