Domanda

C'è un modo per prendere qualsiasi numero, da dire, 1-40.000 e generare un hash di 8 caratteri?

Stavo pensando di usare base_convert, ma non sono riuscito a trovare un modo per forzarlo ad essere un hash di 8 caratteri.

Qualsiasi aiuto sarebbe apprezzato!

È stato utile?

Soluzione

Perché non basta eseguire md5 e prendere i primi 8 caratteri?

Perché si vogliono un hash, non importa se le parti vengono scartati, ma piuttosto che lo stesso input produrrà lo stesso hash.

$hash = substr(md5($num), 0, 8);

Altri suggerimenti

>>> math.exp(math.log(40000)/8)
3.7606030930863934

Pertanto è necessario 4 cifre-simboli per produrre un hash di 8 caratteri da 40000:

sprintf("%08s", base_convert($n, 10, 4))

Per php:

$seed = 'JvKnrQWPsThuJteNQAuH';
$hash = sha1(uniqid($seed . mt_rand(), true));

# To get a shorter version of the hash, just use substr
$hash = substr($hash, 0, 10);

http://snipplr.com/view.php?codeview&id=20236

ci sono molti modi ...

Un esempio

$x = ?
$s = '';
for ($i=0;$i<8;++$i)
{
    $s .= chr( $x%26 + ord('a') );
    $x /= 26;
}
$hash = substr(hash("sha256",$num), 0, 8);

Così si desidera convertire un numero a 6 cifre in una stringa di 8 cifre riproducibile?

sprintf("%08d", $number);

Certamente un hash non è reversibile - ma senza un sale / IV potrebbe essere un po 'facile da individuare. Una soluzione migliore potrebbe essere:

substr(sha1($number . $some_secret),0,8);

C.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top