Skip to content

Instantly share code, notes, and snippets.

@eiabea
Created August 26, 2015 14:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eiabea/05b51e6f4f1bc4c4197d to your computer and use it in GitHub Desktop.
Save eiabea/05b51e6f4f1bc4c4197d to your computer and use it in GitHub Desktop.
var bcrypt = require('bcrypt');
// represents hash on server
var hashInDb;
// Client
bcrypt.hash('bacon' + 'appsalt', 10, function (err, hash) {
console.log("bacon with appsalt: " + hash);
// --> Send Hash to REST
// Server
bcrypt.hash(hashInDb + 'usersalt', 10, function (err, hash) {
hashInDb = hash;
login();
});
});
// Client
var login = function () {
console.log("in DB:" + hashInDb);
bcrypt.compare('bacon' + 'appsalt', hashInDb, function (err, res) {
console.log("result: " + res);
});
}
@donothingloop
Copy link

No warranty for correctness and security :)

@eiabea
Copy link
Author

eiabea commented Aug 26, 2015

// represents hash on server
var hashInDb;
var userPw = 'bacon';

// Client - Executed when password is changed
bcrypt.hash(userPw + 'appsalt', 10, function (err, hash) {
  storeHashOnServer(hash)
});

// Server
var storeHashOnServer = function(hash){
  bcrypt.hash(hash + "salt", 10, function(err, hash) {
    hashInDb = hash;

    login(hash);

  })
};

// Client
var login = function (hashFromClient) {
  bcrypt.hash(hashFromClient, 10, function(err, hash) {
    bcrypt.compare(hashInDb, hash, function (err, res) {
      console.log("result: " + res);
    })
  });
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment