I have some strings that have been encrypted using the PHP function crypt()
.
The outputs look something like this:
$1$Vf/.4.1.$CgCo33ebiHVuFhpwS.kMI0
$1$84..vD4.$Ps1PdaLWRoaiWDKCfjLyV1
$1$or1.RY4.$v3xo04v1yfB7JxDj1sC/J/
While I believe crypt() is using the MD5 algorithm, the outputs are not valid MD5 hashes.
Is there a way of converting the produced hashes into valid MD5 hashes (16-byte hex values)?
Update:
Thanks for the replies so answers so far. I'm pretty sure the crypt function used is using some sort of MD5 algorithm. What I'm looking to do is convert the ouput that I have into an MD5 hash that looks something like the following:
9e107d9d372bb6826bd81d3542a419d6
e4d909c290d0fb1ca068ffaddf22cbd0
d41d8cd98f00b204e9800998ecf8427e
(taken from Wikipedia)
Is there a way of converting from the hashes I have to ones like the above?
From http://php.net/crypt:
You want the
md5()
function:$1$ indeed means that this is a MD5 hash, but crypt generates a random salt. This is why you find a different MD5 value. If you include the generated salt you will find the same result.
The salt is base64 encoded in the output, as the hash.
The algorithm used is a system wide parameter. Generally this is MD5, you are right.
I believe the answer to my original question is no, you can't convert from one format to the other.
The hashes generated by php crypt() appear to be generate by a version of the FreeBSD MD5 hash implementation created by Poul-Henning Kamp.
http://people.freebsd.org/~phk/
OK, so maybe this answer is a year late, but I'll give it a shot. In your own answer, you note that
crypt()
is using the FreeBSD MD5, which also does some interesting transformations on the salt before running the hash, so the result of what I'm about to give you will never quite match up with the results of a call tomd5()
. That said, the only difference between the output you are seeing and the format you are used to is that the output you are seeing is encoded as followsTo my knowledge, the alphabet used by crypt looks like this:
So, with all of this borne in mind, here is how you can convert the 22 character crypt-base64 hash into a 32 character base16 (hexadecimal) hash:
First, you need something to convert the base64 (with custom alphabet) into a raw 16-byte MD5 hash.
Now that you've got the raw data, you'll need a method to convert it to the base-16 format you're expecting, which couldn't be easier.
Finally, since the output of crypt includes a lot of data we don't need (and, in fact, cannot use) for this process, a short and sweet function to not only tie these two together but to allow for direct input of output from crypt.
Given these functions, the base16 representation of your three examples are:
Of course, it is important to remember that they were always valid, just formatted differently.
From the documentation, this depends on the system. You can force the algorithm used by setting the salt parameter. From the docs: