I had the same question as the original poster, and he looked around a bit and tried different things to understand the mechanism. As others have already pointed out, salt concatenates with the final hash. So this means a couple of things:
- The algorithm must know the length of the salt
- Must also know the position of the salt in the last line. for example, if shifted by a certain number on the left or right.
These two things are usually hardcoded in the implementation, for example. bcrypt implementation source for bcryptjs defines the salt length as 16
var BCRYPT_SALT_LEN = 16;
So, to illustrate the basic concept of an idea, if you want to do it manually, it will be similar to the one below. I do not recommend implementing things like this when there are libraries that you can do for this.
var salt_length = 16; var salt_offset = 0; var genSalt = function(callback) { var alphaNum = '0123456789abcdefghijklmnopqurstuvwxyzABCDEFGHIJKLMNOPQURSTUVWXYZ'; var salt = ''; for (var i = 0; i < salt_length; i++) { var j = Math.floor(Math.random() * alphaNum.length); salt += alphaNum[j]; } callback(salt); }
Alappin May 24 '14 at 5:23 a.m. 2014-05-24 05:23
source share