MySQL Hashing Function Implementation

前端 未结 7 1663
[愿得一人]
[愿得一人] 2020-12-05 03:31

I know that php has md5(), sha1(), and the hash() functions, but I want to create a hash using the MySQL PASSWORD() function. So far, the only way I can think of is to just

7条回答
  •  死守一世寂寞
    2020-12-05 03:50

    Perl 5 implementation of old_password() based on the PHP example.

    sub old_hash_password {
        my ($password) = @_;
    
        my $nr = 1345345333;
        my $nr2 = 0x12345671;
        my $add = 7;
    
        for (my $i = 0; $i < length($password); $i++) {
            my $byte = substr($password, $i, 1);
    
            next if ($byte eq ' ' || $byte eq "\t");
    
            my $ord_b = ord($byte);
            $nr ^= ((($nr & 63) + $add) * $ord_b) + (($nr << 8) & 0xFFFFFFFF);
            $nr2 += (($nr2 << 8) & 0xFFFFFFFF) ^ $nr;
            $add += $ord_b;
        }
    
        my $out_a = $nr & ((1 << 31) - 1);
        my $out_b = $nr2 & ((1 << 31) - 1);
    
        return sprintf("%08x%08x", $out_a, $out_b);
    }
    

提交回复
热议问题