Search code examples
node.jscharacterbcrypt

Can colon and underscore characters be used in bcrypt hashes...?


I believe...from some research, that bcrypt allows any type of character to be taken in to be hashed...however I am looking for some verification of this please. I have the following code which produces two consecutive bcrypt hashes, first for a password and second for a name.
The hash for the password works without any issues. The hash for the name is not functioning...it writes "undefined" to the database. This name includes some special characters, such as an underscore ("_") and a colon (":")...are these characters permissible to be used in the hash generation?

const processes = require('./routes/processes');
namex = String(name) + String("_0:") + String(_year + _month + _day);

processes.hashPassword(password)
.then(function(hashedPassword) {
 newHash = hashedPassword;  //this works fine, returns a hash 
})
.then(processes.hashName(namex))  
.then(function(hashedName) {
 newName = hashedName;  //this returns 'undefined'...is not working...because of the special characters???
})
//code to write to database here...

Solution

  • In case anybody needs for future reference I was able to solve this using the following code modification:

    const processes = require('./routes/processes');
    namex = String(name) + String("_0:") + String(_year + _month + _day);
    
    processes.hashPassword(password)
    .then(function(hashedPassword) {
     newHash = hashedPassword;  //this works fine, returns a hash
     return processes.hashName(namex);  
      //NOTE:  The 'return' is CRITICAL to prevent an 'undefined' result 
      //for 'hashedName' in following '.then' statement...!!!
    })
    .then(function(hashedName) {
     newName = hashedName;  //this works fine, returns a hash
    })
    //code to write to database here... 
    

    The key to solve this issue is to add a 'return' statement when calling the subsequent promise. When doing this I managed to avoid an 'undefined' result. Many thanks to this article here: "https://pouchdb.com/2015/05/18/we-have-a-problem-with-promises.html" for helping me resolve this issue.