• Some users have recently had their accounts hijacked. It seems that the now defunct EVGA forums might have compromised your password there and seems many are using the same PW here. We would suggest you UPDATE YOUR PASSWORD and TURN ON 2FA for your account here to further secure it. None of the compromised accounts had 2FA turned on.
    Once you have enabled 2FA, your account will be updated soon to show a badge, letting other members know that you use 2FA to protect your account. This should be beneficial for everyone that uses FSFT.

PHP and Mcrypt()

netsider

Limp Gawd
Joined
Oct 12, 2004
Messages
466
I didn't know whether to put this in webmastering/programming, or here... but decided it's probably more fit for here.

I was dissecting a PHP example found in the manual to better understand it, which I feel I now do. The example is found here. After understanding what it does (and after understanding - for the most part - the other hashing functions like hash and crypt) is whether or not mcrypt (more specifically, mcrypt_encrypt) can be used for password hashing, or if it should only be used for data. It seems like it's only to be used for data, so I answered this myself... but correct me if I'm wrong.

The answer I found here explains how one-way hashing functions shouldn't be used for passwords... which I understand (since mcrypt isn't one way). However, why isn't it?

I know mcrypt uses both a key and IV's to encrypt/decrypt data so that the encrypted data is never the same (even in different parts of the same block of data). I also know that WEP (in 802.11 wireless) isn't a one-way function (correct me if I'm wrong) and also uses initialization vectors, which can be analyzed to figure out the encryption key and ultimately decode transmissions. Is this the same thing with PHP's mcrypt(), and the reason it's not a one-way hashing function? Could enough IV's be gathered to recognize/compute a pattern to discover the key?

Also.. what are some examples of one-way hashing functions? I don't really know any other than WEP... since it seems like at first they're thought to be... or aren't they?

I know a lot of this sounds like I already know.. but I was just wanting to strengthen my understanding of it all... especially since I'm know dealing with some of this first hand somewhat (with PHP).

Thanks ;)

Edit: Nevermind.. I just thought about it and realized why. mcrypt_encrypt has an opposite function for decryption - mcrypt_decrypt, which decrypts it using the IV/key. The other functions are one way because they don't have an opposite function to decrypt the encrypted data. I guess I should wait a little bit before asking question... although this one I did ;) If anyone wants to go into why one way functions are one-way... go ahead, however.. because that I don't really know, although the algorithm can't be used directly in reverse and must be bruteforced. Thanks anyways guys..
 
Last edited:
Hashing (one way algorithm) are used exactly because it is easy to make a hash from string, but next to impossible to find out what string the hash represents. You should never have a password recovery feature, only password reset feature.

The recommended way to store passwords is to encrypt them using the crypt() function, with a Blowfish ($2y$) algorithm, of course with a salt.

Code:
	function generateSalt() {
		$random = openssl_random_pseudo_bytes(18);
		$salt = sprintf('$2y$%02d$%s',
				13, // 2^n cost factor
				substr(strtr(base64_encode($random), '+', '.'), 0, 22)
		);
		return $salt;
	}
$salt = generateSalt();
$hash = crypt($password, $salt)

Then you save the hash and salt to the database and that is all.
 
I didn't know whether to put this in webmastering/programming, or here... but decided it's probably more fit for here.

No, your first guess was better. Just because we deal with cryptocurrencies doesn't mean we are all manner of encryption specialists.
 
Back
Top