How to create an md5 hash because the crypt function generates using the md5 salt using the md5 function, not the crypt function?

I prefer to use the cryptography function in php to encrypt passwords and other one-way encryption requirements. Because I can use any supported encryption algorithm, changing the salt, and there are several more advantages. I usually don’t use salt, and it takes a random MD5 salt. I save this encryption string as a password hash in the database, and when authenticating the user, I use this as a salt for the crypt function. It works great in php. But when he needs some other programming language to create a hash, while I use the crypt function in the php part of the function, we run into a problem.

I would like to know if there is any simple way to create an MD5 hash (using the PHP function md5 () or another), which should be similar to what the crypt function generates when using the MD5 salt. If I can understand how this works in php without using the crypt function, then there may be a good opportunity to implement it in other programming languages.

+3
source share
1 answer

Here's the code in Java that implements the same function. This can help you do the same in other languages.

For PHP you can see this code:

    echo 'MD5:          ' . crypt('mypassword', '$1$somesalt$') . "\n";
    echo 'MD5:          ' . mycrypt('mypassword', 'somesalt') . "\n";

    function to64($s, $n)
    {
        $i64 = "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
        $r = '';
        while (--$n >= 0) {
            $ss = $s & 0x3f;
            $r .= $i64[$s & 0x3f];
            $s >>= 6;
         }
        return $r;
    }

    function mycrypt($v, $s) {
            $m = hash_init("md5");
            hash_update($m, $v);
            hash_update($m, '$1$');
            hash_update($m, $s);

            $m1 = hash_init("md5");
            hash_update($m1, $v);
            hash_update($m1, $s);
            hash_update($m1, $v);
            $final = hash_final($m1, true);
            for ($pl = strlen($v); $pl>0; $pl-=16) {
                    hash_update($m, substr($final, 0, $pl > 16? 16:$pl));
            }
            $final = "\0";
            for($i=strlen($v);$i!=0;$i>>=1) {
                    if (($i & 1) != 0) {
                            hash_update($m, $final);
                    } else {
                            hash_update($m, $v[0]);
                   }
            }
            $final = hash_final($m, true);
            for($i=0;$i<1000;$i++) {
                $m1 = hash_init("md5");

                if(($i&1)) {
                    hash_update($m1, $v);
                } else {
                    hash_update($m1, $final);
                }
                if(($i%3)) {
                    hash_update($m1, $s);
                }
                if(($i%7)) {
                    hash_update($m1, $v);
                }
                if(($i&1)) {
                    hash_update($m1, $final);
                } else {
                    hash_update($m1, $v);
                }
                $final = hash_final($m1, true);
            }
            $l = '$1$'.$s.'$';
            $l .= to64(ord($final[ 0])<<16 | (ord($final[ 6])<<8) | ord($final[12]), 4);
            $l .= to64(ord($final[ 1])<<16 | (ord($final[ 7])<<8) | ord($final[13]), 4);
            $l .= to64(ord($final[ 2])<<16 | (ord($final[ 8])<<8) | ord($final[14]), 4);
            $l .= to64(ord($final[ 3])<<16 | (ord($final[ 9])<<8) | ord($final[15]), 4);
            $l .= to64(ord($final[ 4])<<16 | (ord($final[10])<<8) | ord($final[ 5]), 4);
            $l .= to64(ord($final[11]), 2);

            return $l;
    }
0
source

Source: https://habr.com/ru/post/1792151/


All Articles