Some Salt and Pepper for bcrypt
Recently we had Troy Hunt come along to chat with our students. He outlined methods of taking MD5 hashed passwords and then hashing them again with bcrypt. Troy also outlined [here] the use of a pepper key to encrypt the hashed output. One of the basic methods he outlined followed the Dropbox approach:
With this, we take the password and then hash it. In Dropbox’s case, they use SHA512, and which gives us 64 bytes of hashed output. bcrypt can then be used with a random salt and a cost of 10. Finally, the output from bcrypt is then encrypted with AES using a global pepper key. This key is the same or a wide range of password hashes.
So, let’s code for bcrypt with different costs and hash values [here]:
namespace bcrypt
{
using BCryptNet = BCrypt.Net.BCrypt;
using System.Security.Cryptography;
class Program
{
static void Main(string[] args)
{
int cost=4;
string hashtype="SHA2";
var hash=BCrypt.Net.HashType.SHA256;
string msg="Pa$$w0rd";
if (args.Length >0) msg=args[0];
if (args.Length >1) cost= int.Parse(args[1]);
if (args.Length >2) hashtype= args[2];
if (hashtype=="SHA256") hash=BCrypt.Net.HashType.SHA256;
if (hashtype=="SHA384") hash=BCrypt.Net.HashType.SHA384;
if (hashtype=="SHA512") hash=BCrypt.Net.HashType.SHA512;
string passwordHash = BCrypt.Net.BCrypt.EnhancedHashPassword(msg, hash,workFactor:cost);
Console.WriteLine("Enhanced Entropy Bcrypt");
Console.WriteLine("Password:\t{0}\nHash:\t\t{1}\nCost:\t\t{2}\nBCrypt:\t\t{3}",msg,hash,cost,passwordHash);
bool rtn= BCrypt.Net.BCrypt.EnhancedVerify(msg, passwordHash,hash);
Console.WriteLine("\nHash verified {0}",rtn);
}
}
}
A sample run with a cost of 4 and with SHA-256 is [here]:
Enhanced Entropy Bcrypt
Password: abc
Hash: SHA256
Cost: 4
BCrypt: $2a$04$uWdosFs19GVT5O9Q5hyKIeIySBtu6vhH5DJhNlYI7PCTL5YUa8uVG
Hash verified True
We can implement another types of pre-hash by specifying the hashType, such as for SHA-512:
hash=BCrypt.Net.HashType.SHA512;
string passwordHash = BCrypt.Net.BCrypt.EnhancedHashPassword(msg, hash,workFactor:cost);
Here is the code:
Once we create the output from bcrypt, we can then use the pepper key to encrypt the hash. The slight weakness with this, is where the pepper key is discovered by an intruder, as they will allow all the bcrypt outputs to be discovered.