我是nodejs,sails的新手,需要在将数据存储到数据库之前对其进行加密。
我找到了模块simplecrypt
并制作了此代码演示。
var simplecrypt = require("simplecrypt");
var sc = simplecrypt();
var digest = sc.encrypt("my secret");
console.log(digest); // "66cea6eb1c18b8862485cf0604fa6062"
var message = sc.decrypt(digest);
console.log(message); // "my secret"
它会对值进行加密,但是当我尝试解密时,我收到错误:
crypto.js:202
var ret = this._handle.final();
^
Error: error:06065064:digital envelope routines:EVP_DecryptFinal_ex:bad decrypt
at Error (native)
at Decipher.Cipher.final (crypto.js:202:26)
有人可以建议我正确解密我的加密值吗?
答案 0 :(得分:1)
花了一整天的时间来检查后,我把结果搞清楚了。 我必须定义
func mergeChanges(notification: NSNotification) {
NSLog("mergeChanges notif:\(notification)")
if let moc = managedObjectContext {
moc.performBlock {
moc.mergeChangesFromContextDidSaveNotification(notification)
self.postRefetchDatabaseNotification()
}
}
let vc = CollectionViewController()
let view = self.window?.rootViewController
vc.title = "Syncing"
view?.title = "Syncing"
}
func persistentStoreDidImportUbiquitousContentChanges(notification: NSNotification) {
self.mergeChanges(notification);
}
func storesWillChange(notification: NSNotification) {
NSLog("storesWillChange notif:\(notification)");
if let moc = self.managedObjectContext {
moc.performBlockAndWait {
var error: NSError? = nil;
if moc.hasChanges && !moc.save(&error) {
NSLog("Save error: \(error)");
} else {
// drop any managed objects
}
moc.reset();
}
let vc = CollectionViewController()
vc.title = "Syncing"
// reset UI to be prepared for a totally different
// don't load any new data yet.
}
}
func storesDidChange(notification: NSNotification) {
// here is when you can refresh your UI and
// load new data from the new store
let vc = CollectionViewController()
// vc.collectionView.reloadData()
NSLog("storesDidChange posting notif");
self.postRefetchDatabaseNotification();
}
并将值从mydabase赋值给user1并声明
var user1 ={
method:methodinput,
password:passwordinput,
salt:saltinput
};
然后将user.password与passEncrypt进行比较。
答案 1 :(得分:0)
根据你的评论@angel,我想我明白你的问题在哪里。 decrypt()接受String的散列,而不是String(user.password)本身。
var simplecrypt = require("simplecrypt");
var sc = simplecrypt();
var user = {
password: "password123"
};
var myHash = sc.encrypt(user.password);
// myHash returns a hash of user.password
console.log(myHash); // "66cea6eb1c18b8862485cf0604fa6062"
// This next state
// You will need to pass the created hash into decrypt
var message = sc.decrypt("66cea6eb1c18b8862485cf0604fa6062");
console.log(message); // "password123"
如果您尝试验证用户已登录(假设来自user.password),您可能希望将密码与数据库中存储的哈希值进行比较。
// Here is a simple example for a login screen
// (Not for use in production)
var simplecrypt = require("simplecrypt");
var sc = simplecrypt();
var user = {
password: "password123"
};
// This would be stored in the DB
var databaseHash = sc.encrypt(user.password);
// Here is your login function
//
// Now when the user logs in, you will compare the two
// and see if the credentials match
var userInput = sc.encrypt(user.password);
var usersPasswordFromDatabase = databaseHash;
if (userInput === usersPasswordFromDatabase){
// Logged In!
console.log('Logged In!');
}else {
console.log('Invalid Username/Password');
}
该示例将返回'已登录'