moccalotto / idhash
可逆(整数)ID 混淆器
1.0.0
2018-07-29 16:56 UTC
Requires
- php: >=5.5
Requires (Dev)
- phpspec/phpspec: ^2.5
This package is not auto-updated.
Last update: 2024-09-14 19:36:35 UTC
README
可逆(整数)ID 混淆器
创建类似于 imgur、pastebin、youtube 等使用的散列
默认密钥空间是标准的 base62 密钥空间。
安装
要将此包作为本地、按项目依赖项添加到您的项目中,只需将 moccalotto/idhash 作为依赖项添加到您的项目 composer.json 文件中,如下所示:
{
"require": {
"moccalotto/idhash": "~0.9"
}
}
或者,简单地调用 composer require moccalotto/idhash
<?php use Moccalotto\IdHash\IntHasher; use Moccalotto\IdHash\StringKey; use Moccalotto\IdHash\RandomKeyFactory; require 'vendor/autoload.php'; // you can generate a randomized base62 key by running bin/random_key $keyspace = '8w2JUNlFLxfuCXbjkOmBizsWHG9Ep5n4Pd70yZg63vAerQVTMIRhS1DKqocaYt'; // Create a key shuffler // This is roughly the same as $key = str_shuffle($keyspace), // but it guarantees that the output keyspace is different from the input keyspace // It also ensures that we squash duplicate characters $key_generator = new RandomKeyFactory($keyspace); // generate a random key $key = $key_generator->key(); // initialize a new IntHasher $IntHasher = new IntHasher($key); // generate new input value $input_int = mt_rand(); // encode the integer into a hash $hash_str = $IntHasher->intToHash($input_int); // decode the hash into an integer $output_int = $IntHasher->hashToInt($hash_str); // print the process values to screen printf('The generated key: %s%s', $key->keyString(), PHP_EOL); printf('Number to encode: %d%s', $input_int, PHP_EOL); printf('The encoded hash: %s%s', $hash_str, PHP_EOL); printf('Decoded number: %s%s', $output_int, PHP_EOL); printf('Success: %s%s', $input_int === $output_int ? 'Yes' : 'NO!', PHP_EOL); die($input_int !== $output_int);