2
votes

currently making a little analysis on the authentication based on password on linux, i understood the following :

users passwords hashes are stored in /etc/shadow, with the salt used to generate them. The id also provide the algorithm used to get the hash. To verify if a password is correct, we pass the id and the salt to the crypt function, generate a temporary hash and compare it to the hash registered.

However, when you create a new user for example, you need to populate that shadow file with hash and salt. Hash is of course obtained by calling crypt function, but i can't figure out how is the salt generated when invoking that crypt function.

Indeed, from what i have read and understoof of the libc code relative to crypt function, there is nothing relative to generate a random salt. We can provide crypt with a random salt we have created by hand when creating a new user, but we almost always create users without providing salts for their password hashing.

So, how is that salt generated the first time when creating a user? Is /dev/random used?

I would appreciate some code or commands in the answer.

thanks!

1
The DEV/URANDOM is the right source to read from, but different algorithms may have different requirements for the salt (BCrypt for example accepts only characters of a given alphabet).martinstoeckli
@martinstoeckli What makes you think that the` bcrypt` salt "accepts only characters of a given alphabet"? That is incorrect.zaph
@zaph - The algorithm BCrypt does accept any salt, but not the implementation of the crypt() utility, which is what the php function used to call. It requires an encoded salt of the following alphabet ./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz. The newer password_hash() function accepts any salt, but transforms it to this alphabet (if necessary) before calling the crypt function.martinstoeckli

1 Answers

0
votes

There is no standard function to generate the salt but every good library should generate a random one for you. As written in another comment some salts have to be of a specific alphabet like "A-Za-z0-9./" for crypt() while others accept random data.

For Java, look at the Apache Commons Codec project which implements a Unix crypt() compatible function. It's a simple for loop that choses random characters from a given alphabet:

http://grepcode.com/file/repo1.maven.org/maven2/commons-codec/commons-codec/1.10/org/apache/commons/codec/digest/B64.java#B64.getRandomSalt%28int%29