Question

J'ai un hachage d'informations Base32.par exemple. IXE2K3JMCPUZWTW3YQZZOIB5XD6KZIEQ, et je dois le convertir en base16.

Comment puis-je le faire avec PHP ?

Mon code ressemble à ceci :

$hash32=strtolower($hash32);
echo $hash32; // shows - IXE2K3JMCPUZWTW3YQZZOIB5XD6KZIEQ
$hash32=sha1($hash32);
$hash16=base_convert($hash32, 32, 16);
echo "</br>";
echo $hash16 // shows - 3ee5e7325a282c56fe2011125e0492f6ffbcd467

Dans mon code, le hachage d'informations basé sur 16 n'est pas valide.

Le hachage Info valide est 45C9A56D2C13E99B4EDBC43397203DB8FCACA090

Comment puis-je obtenir un hachage d'informations valide ?

Merci

Était-ce utile?

La solution

Je vous réponds en omettant volontairement gmp_strval(gmp_init(strtoupper($hash32), 32), 16); lequel seulement marche avec BPF installé sur votre serveur.

function convBase($numberInput, $fromBaseInput, $toBaseInput)
{
    if ($fromBaseInput==$toBaseInput) return $numberInput;
    $fromBase = str_split($fromBaseInput,1);
    $toBase = str_split($toBaseInput,1);
    $number = str_split($numberInput,1);
    $fromLen=strlen($fromBaseInput);
    $toLen=strlen($toBaseInput);
    $numberLen=strlen($numberInput);
    $retval='';
    if ($toBaseInput == '0123456789')
    {
        $retval=0;
        for ($i = 1;$i <= $numberLen; $i++)
            $retval = bcadd($retval, bcmul(array_search($number[$i-1], $fromBase),bcpow($fromLen,$numberLen-$i)));
        return $retval;
    }
    if ($fromBaseInput != '0123456789')
        $base10=convBase($numberInput, $fromBaseInput, '0123456789');
    else
        $base10 = $numberInput;
    if ($base10<strlen($toBaseInput))
        return $toBase[$base10];
    while($base10 != '0')
    {
        $retval = $toBase[bcmod($base10,$toLen)].$retval;
        $base10 = bcdiv($base10,$toLen,0);
    }
    return $retval;
}

Cette fonction, trouvée ici, "convertit un nombre arbitrairement grand de n'importe quelle base en n'importe quelle base".Il vous suffit de convertir depuis socle 32 à base 16, ainsi:

alphabet en base 32 est: ABCDEFGHIJKLMNOPQRSTUVWXYZ234567

$hash32='IXE2K3JMCPUZWTW3YQZZOIB5XD6KZIEQ';
$hash16=convBase($hash32, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567', '0123456789ABCDEF');
//$hash16='45C9A56D2C13E99B4EDBC43397203DB8FCACA090'

Le problème ici était que base_convert mal gérer les grands nombres.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top