2

我一直在编写一些软件,它使用“随机”字符串来创建用于 URL 的确认密钥哈希。试图让最终用户看不到请求的 ID 号。一段时间以来,我一直在使用 crypt 代码:http ://blog.kevburnsjr.com/php-unique-hash ,它使用了一些黄金素数,我正在对请求的 ID 号进行哈希处理。我的问题是,自从从 32 位服务器切换到 64 位服务器后,这个 5 个字符的哈希现在被复制了。

考虑切换到 base 36 编码,但这有可能在 100 万个条目后再次复制自身。并不是说我计划有 100 万个条目,但谁知道呢。(3 年后已经有多达 9600 个条目)。

创建“哈希”以隐藏将创建无冲突环境的请求的主要 ID 号的最佳方法是什么?

在这一点上,我不担心长度,即使它是 5 个字符,它可能会与现在的内容产生冲突。

4

3 回答 3

3

请参阅少数有用的 PHP 手册注释之一:http : //www.php.net/manual/en/function.uniqid.php#94959,它生成符合 UUIDv4 的标识符。

function v4()
{
    return sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x',

      // 32 bits for "time_low"
      mt_rand(0, 0xffff), mt_rand(0, 0xffff),

      // 16 bits for "time_mid"
      mt_rand(0, 0xffff),

      // 16 bits for "time_hi_and_version",
      // four most significant bits holds version number 4
      mt_rand(0, 0x0fff) | 0x4000,

      // 16 bits, 8 bits for "clk_seq_hi_res",
      // 8 bits for "clk_seq_low",
      // two most significant bits holds zero and one for variant DCE1.1
      mt_rand(0, 0x3fff) | 0x8000,

      // 48 bits for "node"
      mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff)
    );
}

请注意,这mt_rand不是很好,您可能更喜欢openssl_random_pseudo_bytes在它的位置使用。

于 2012-09-04T20:13:27.637 回答
1

这可能是你所追求的:

<?php
// You should create your own salt (hand-typed), and
// ideally, it should be the same length as the hash
// (e.g. for SHA512, it should be 512 chars long).
$salt = 'hi48hO*$H84hOhe8o4hnioH$*oihnO$8hOHTO84HoihOhno$i*HOIHNOh$Oh48oh$';
for($i = 0; $i < 100; $i++) {
    $random = hash('sha512', mt_rand() . microtime() . mt_rand() . $salt);
    echo $random;
}
?>

这产生了非常独特的价值。例如:

aee73ae66f294e26f29c8ede73eb2d44a2887251b466aff7ad8aef7cd5246fd4b3b0c5a538e684deab7304144c34a6bbd591366ea375dfad75abccc606dc2b010b0d028e41d070aa74017bbd65c4391e7b0281036aebc47da68c8802b32c9cc06d9107f2dbad7cb744852bf8fe05aa082a3e80d6d41dd52039b9eb4d9e25d347515a1a1ee3e86aa64235f86da42d5e269520b6f7e3fc2974a72e02a31d5769b91b1ce70769c9bac8fc61bb365757b220812906dc9ff4990a28dc933ab726efb17a548e89c4f0618c5b4a276ebe92b60121a2308a65fa17027bacb69feba8d6e1027ca06db3eb6609226406161e50945ca7216573b17896b585bfae34bff9835eaea694a6c411721063181e19cd82fddf3320e89d832321f6fae8a4a7ece3a864696b4b23664098e497d97fce77c61f417e29b07f808341e848783ac8a800dad8ef5ee3c947fd6a103ca173e147b4af158bd61ed66602f9cf9c598e27ad78c0c65b012d5d781fb8fcd02130f3d2775cda572c7355d3676cbd4f1e81397925bb5584a624b55356ae48c215eff055bc57686527e155d347c2cac49f82327da8f679b139c5923b5dbb6ab1817dea682d8f7d50d2e5c9715f84e77736df98a98b2c23b53603eb2ea181c38f2399fba52777fbb0efd0e993ee727e815ca37e68b5f87edd8980e2244964880366a7928526840f71808f494fb341ae9beda4bd004d64ce210480eaba8487b3533063466916b8ad673928a388b87c7e130d3e5b9e9d41b0e020bb5aa939b9066d4985ccbc4b32aeb8377e41789148a9b163d71a080b8395512609e3c0ff3937d766bc7ed456f436c64ccf08e1aa2446037d6c7a624388a87beed887f9181a2e252d478e35672b87831b452e0f470afdbdc49143f0d9a6d950fb16f4714b7e3529b3ea888491f6d394fe82c0840e01fc5c5a64c36bb194b5619112b0c7a40886545d385f8ec699aa56ef7c9bbed9afec7ca5749e7e8fd4ce61b45c423113837670639665b24d3809693ff0b6e358e65e030e277b30980accf1ecae6760f585fa5d3d473cbd24f5e976899adcadd64d24aeff9c6ca28de2af51cc9540431380eda7c435fb977e14580b8769c011aa59158993c8a04708288b399c9f3d85995a1f20103d8e83e8d4f5f4ae705c5ecf49cb1972aaa8413509d579e294687c76caef5fcbe27709de1b94e8ed278e3aa8ca1639cbd92a414bdb9023b89853698dc57ea2bbae6dd6a831877249c3bba271c8c4a53bcb986a12f7a1121d4eb713673eb756a2750c4cc50421918ad95c84536e56ea71cfa8073286e840661e1bfd2c8f26fb075f98816d056c59ca7cabe352e9df4b2dae48103c040aa9e12246ab04cf3c4e2994c25a4623350286fd616040aa6914884fdb0926e1e20a1623d65347b3751c6a0b2900f70663c8391140ae0c562f5a868040500ea3f6df0f0b94d714a93d76ac98e39f00136c3e23f1240966aaf206477c457e9aa3dfc419e8cce8adb08aa926e8d270c582c930d2ba81076149eb38b8fb417e4943da2268b23adc71dd4a782e69b3eab0dd95ef3ec6fe0683515cd120a072fa3c8d3715ada41e57da61e9bbcd660ca0705c8ae10064cbdb4b8374bba3e8998f1eb0b2ae575b3eef337b1166def6909836054546abefe63399883878d015e0e2379b257ff4e491d9bfcafb109e52de36d2867df957880af651fcc1c9eb5e3790979db546247ef640e43e6910153eeee390c8d55e34f3573f1cdc5f679972cfdf2954a3f854dda90b772db8b92329068080a05c401b4313ab890532bae94a84fac3b7d6dcceb261b04d7547a9ee7c3306e008c1c75308a73812fbc83b92777df7485f5febeac55371586e973fa7a7ee61d55a5bc3515ae2873227639f0c228da5ed903c918721f4325125d2f201b9e4fffc3cbf8b938a12c0c8fa7a231817fb8e6cbc3ab8e76fa41f75ba0c4e91b044f690d72a1cc7fcfd808b0bc7940863b793debe34910348b71d4dec5053a64938de1c6b156a9ed11fe288239d5d84acb23822b092285e6b2f55b5e7b52f146715f87e0f971ad87cd1e296df6410ba8b350849837e91b2d2ec72af19ed3b0456959e4ed8db126399d014b54e6a409801a74fa52e46b0fc50620152a057082f9ef5c16e4fe8ea71f051307946d661ab9a99b94ead55a8129d40a872a2f0935d163035033997b3a65fcb5e66a0b8765c6cbff31726951503fcaa447d06713a8d3bc80fba323b215219f637636359377f2047840832d39880f1663e2aac98efdc14ef71b057241b15a88d7bbec133a26eb1d481f47b7c87b94f5d53827f8e1a9db13b9cba1cfd24a00f5897aa930f6fde2e016b8b3489da447afcb4401b036f9e046c57998be5b221c355f58a76fa8d46ab87b45f120138c51356f3fac0ecb724dc1ae72c2fbaf868c09922100a9a41a44b8fdb770a4ac7fa9fe8dacf1ff448db7a75ee0b8405fea768f3bea72cb75e813d3ef462db6784c565935d7ee546858dd1eb0f53b2fb14a543eab9113edfc9723108e9a0807bc2fe2ad3cdbb3786cfab9b0efa9ec2c65ef9cb01332a630c02ef4d13ed740fde7395cfd25f0e6430526247a422d63d0c9507528542cb611ed9d75793cab15c8441942cf1084673ea9e18b9a94728febae96c7a189c009bb8a7e6cb22e597723591c885cd9dfb65efe88268ba5ba1adea9c2f657bffe8808709279659e394b73b163778dc33233a9abe501d310e67b3fb33b95cc9b7bbafd6824631ea3462de1bbaf94d4af863c789461937228a2ec49c69dfac9efb9a063164b961946cbbed8ff9a9a3e49637a424c8f444179f028f39fb239b999075f0815519539d3ce101c6e3c38dfaac5ce570c5dec49f9e72bf6cd6c0c0002c04a7f1ceb92afff309a4b040c2e55e06f099a474e928cd8f97dd88538b58f07e5ab89a33b96325438834f1a21e54f5564bfd793acdd995872738e7c0eeefd70f925557d4d1718cfc968874fc0f3768ac0bf63a0634ba219942bb33d83e5053d8de3642852dfa15eca8896738a5d26f1978716af79158abeda54fa709a866acd31f4a049fa01f93774533ecb0989b9193ab4cdd180e6fd5ca255a395a714fa076cc912c424922dfb29fb2683e5248a6d714a36e7dfc528747dab611957501b55aaeaab0699efa262eddd554847da17631428be78600cac3c522eac43a4543b91737f23534c03d52054ee753ed6bbd50afe7a81ea2fbd3f53c3cee21b64d95b4698c16ec46f02b75425166e3a8e5f0744c84b0e47689babaf019f3e21eb763e5e7f3138943cf92a0f8557b39b592efe8eb917fb37dada3bf7cea20e54752688eb13e7ce5fe00ee25f5d436c92b414856d866d5b3b29cea35a30409744d84844730ee19fba10e9f1f2c26ca70b62312294357117cf5901643d79d726e9d2e7203a6cb6680073c6a7a579e99a9041388bab80e1efb503abbb7decabb6945024c63ebafcbf2fffe61fd4651c28bd7d6db6449a592e5d384312fd7caa0596a4bb63ecd3768c116be7c3467afafdc570eae6a6611b7387aa53c77311a149a700e09cd5e5bbc08b284f73521b75eef217694698c4e38992bbb2bfc887bd2d93ee479d638868aa8492ef08a66c0401152905eb70248b7d33731fb641e5dfc1ad5acb66fb68d9fc024d18ff49737a79385e4a10a5cfbce2e315c97f7a77300adaa395f7d1c9969926ca371bceb0ed0b11bdd4163a24d8db55ebcebe65617b9404d42ef669d6b1f47bac0d94cd5c716d52aa41697e4de24beacdeb97422f1f7fe24a976358564c9e0cae4b074cd75c082daf61514ddaa103450f27c4c7eac2f57532eec911a2e052b8bf0756f58da903a3bee80a425b8d93d3dfbb16cb9a1b65afbd30eaa1da03e6cdee7a9dd640ffb965054ccac4ebc842948080fd318f9b98d62a672bdbc9550a4d4d27cf69caaf0d3be0a78ebd18b574b0f2f101af768526e534f5b7d7d71fef6a4b6d6bd5b13eb60f21c233753c5f514d3fed51eb8ebc5ad7c2c0a2ee8f90af08e4731989a712564e11046bc12403b90cd95a40cac3cd9c458ee82b0aa3a18c8c3009a59b3f88be43bb65b6d5d27cbf5839564c898333f3ed95f849ba1e8db87701ad0999a36ab3538352cddbc8b499bb298e03acb950b7a982209287110866979e2df1850be5230c4ad895e632eda3cfa5912ee4f9c397fb008a75ec480785240dd27516a25a63d47983f2241aa293d170faa8123032b8cc0617deb676bf70df6006ada2e4fe2ed3e98f8f7273037caa1c103c98a221491cd1716ae0e1b6aea4282aedc1db1a9c739fd3fb7799a4c0e494d100af82290e63c69dbe7b5d9c65c0d84c797bf3a2f24a5f077340ffe78e933cad168ff5a39684e49dee501812e85956e39e1212e11086518b79df4d28a585519c423005cd55430b059b1fd06532b7c5d7ed16603046c8541a1446941e70396597e31db65cec2c2152e8afdd9e3afed6b25506b47cbdab03e06d33252810dcfafcf0159e1232d890d593343ad57beb657ef7997142f9790c9944f0afa9284df278fb8f2a9fe744ee0b2cc93af3f9ba5a2dfda0516533da6dd158c1cefe9e11940070d1bc5d34c6786d702a0d26f613359d2559547dcbf698833377112a4a30086c238f5bbf893af98a36477de955e8bf30a1c8c5b860d3a92f3a05638360fceef5f824d5037270220d16674920cee34c63800911f9d184db7165ae22c060e8b5c9d0abcee2f11af43052c637ce57ef6a23bb84e5153ece3f1b9bb9d82797a1f0bd15468e6ed3845907592ad4fbc2a4326ffcaf5cdba11b5eec186cda1647efcde4f90c54c2c299506af58a4235e0294d906c3b10ff0c7b599c6bfe1347d123bc2e8d7a4810bbc507645c33df534c7a98ad767972b20a29074264949d78dac9709384b6122e87b8705ac0aba78c0c0d1dbfb1fc8561486dd239546f8677f7ff55d69242a2d9789681766c2f485f2355049ba14acbaf6909e2112c4c7eef4100999290c68931988d760f8465f38c3fe1eb2af4a3eac1deab3229d0301bd5bcfbae1f6e0aad799932b9590d3042932c029a2f65540d871d8b85566aeb0a5efae6e69c8228aa541e1b712a14792bb91c46040afb7206e5355e7b2c8b2986298a36a16626ecd19b209296ce9e4e5a62f1ac9050590154c148471e74285ab8f756e798f405d17494ce526f3fa1b5099c90e09337beac3ff04fe3223c2e648853fb0157be508813ca1e209f45fa3801363a808181e5d3bc7460afec6c9be202a2bc67ae68b764381c1a5f375917e84d7b74db2a09c3011de14565a0adf30fdaac12947b84c7701b61c956142e512daeeb37cf3d5441ea4b1935b797b21e7e492942925cfcdc5012964e4d12a2f409f9d9eaf56d938450239aff5a5c886ef6c78707579727586a05166ef24de601f8cd1c6ff4fad9e2d5c4579feaba9ada14ac79c103e4da3b22c12e789aebdc3e9ce6480b31965ded0c8ac5383c179924b9f8f08d4dae30378d802b9ed13f14917d34632b10d9995c3098fa916c3d33b6e89011f0364f426bf2351733b1dda53f86329fc23a453e06cace4e044fd6de65023a79fe1504c55ee3f12b5a833197b19b38723b5aeb0da7da6a23aedd8ee335c4f533ee7555f044a30c80db732d77cb63892c524b5bf5b204ed8ba3dda8edb64a2f233d05770e7e00eb4d5da904ccb48802aa52f5b9db60c68c271ab3b7e771d4b5c8fe2b0c6e4cee957b986dff1df893c5a609f93f9167d97653e429eb751e1ecb55dce0ce24fdea7ceda8bbbee4669d917823436d4e9ac945434693ba5ff1e9505ea679a275ebe589dfba67e351b1e47d0c4cf9d0cf9ebff561d9d4fa9ef474ab52d8242b4bbf10d39842ac076b13e39666c17974cde543b5db161672dfd3e256a312e51f1e18bebae09bcf8338b8858eda6d898be718a8ef29da758977216a31701a0954672554b43b566feab52ccb1fa0f5baab1cfb054e7330c56a62d7d01568fa4efac640b441d602a5803718f8a39b01f085e2cdbe2cbf88ee102125919d31598ed44b434af6ab4e7fb61d45eba54c610d7b00b098e0cad71855f0147c721b7c1d3583d2f162c2e6df7ac788334215fff2e530376251c2c14c3047cc079c0f9155e88abc1b8c1c847eed1f0c9a80fbfe573fd32ff656a423d91c9cea72582dd3ee0de8b65edb956fbb824885c2b56fe89d761f3d3fd630223b6d4ffe1b76cd3abf681054b20e2e57613e6d3776e33bc508392a6c6671b432e857bad09d4dd769d4ca257a94b99105b684d8e1bc409be36d83da245f6b9b53bd1afb06186b148cc0eeef05bbc8d725769855b098af4b4e5232facd4afe98dd8f6d21b08fe56ec2a20fb421609d8ab694dbe685fc439ff9a7470337cf8213db5d5a86f5c42a129666b3f7c67c9832afcfc6921018cea2baa28948e175962a02168381f5199ee7d982578f1a35ed87c00dde00a7de085d22add775a8968a99853dc25a819602b745a154472c794d6284a1a0d999be245a6ab188885ff8e806be393ae2612e96ae71cd1f07d098b292751885d40c55faebb572b2c8da5c9a0fda9256d9dcce6c3eaabb0df117bfd8089ac114b0c9375ca5aa83de2f06a2d331fdc7750ed9b7654c554c21368ab1686289d4234352203089a1b5283f4544620897e7c27a229eebc8eb933899eee8a504f71c1640c7b4e92b05210ce73e79463a13942252c101252992b7b367fa8367edd472b0f5632ad16f7bb016d214029b05fb66f02e088b7fc823b0a87ff20dbd02365dd46189a70b04dd741cd0821db002cea8a34ca85fc5fa6ae47768c1e97e237d5d95f3f2e61fef6816aec87636bab8537604aeea352d822401070f4e94d3de3cb23ed8feb6cdc3e29e60ce06726c684a6f81f4e91d396317416b134e095cd5d83492f56a5b76400079942b9b8da7c93ee102a2add13426df07adaf863dba483896ae47a84817ecc8048e11491671cd9344ff40bc07169430cdb22b7d02f10bedd30dbd0fc7eabbc62255e9f252edd0c81bf47f646a38ae941b16af6cacd6d678d956c22bf3fb45b0ea37e6caf76723a13888e3019452d77b9665cd31c4b0298b5c995e5dd5a11c2807aba07ca440bc7c79f1e16920dbd9b209a19a629989ca9d799b4c087743299afba3142246c2bf52e8786618c67c3ced85cd207db230cfb754bfe9f6b95c0ad23394207d9ed4fc965339fd222a12654ba75a79ba1f242782a936afd9ad64897e201d375f0d41708e8c4e73d8bf8d0673e177f8ca7660305a9e4d288a80f0cb8e37469d27f7048aa5421d37af8603a029a4d63de0058f0d3a7e20f343a02d8d931d2cbccb487881ab174f31587fbfb59203669fb5822085c65e4104380ac60058ae7753728c0f6ed15244f170a9095173c560dc14cfc87130ffc91f504513a89aa8339ee69c67b19324a17927da212f058dae3c71d31b7d5bd98f60fced74167ed9fcfafbb9d50af9d251b941a60e2738791d2352143c4e91568b1b79539d1280e4d51ea7d20ad7bd79389c8368706c958b63e663ec79ae36d046f9787eb01e1e7aff28f19583556ca18f9bfaf4d5555f35dd3f9aabd029a58314c858deddf9f44929063a64a69cc2b6eb137e53aa81ad34f8bbc39596adb04c1b81284755a935e66867c4e04ec27b444cfd260a70d322498d7415c920bb9bf86af463c7e4fb63581ef8040438baf6396048c9aace50553d88be9c39d6dbb3f00ec6c5b2ab046374071cd77a11d9cdd627e4a5c93cf69a5b127934fe72a7850c07a69fc58d7d2b2702a7c294e00eaf412a25723e2af0b219d03a36a03ab103fb993f7785ca83328189c73ca0ab6a91b95fab8011c9b09c196f158c0067032e36beac5b4a699536a9b9ede57c40cd1d53ac53025a6d2105727cf4c48b1cdd7704730ded0d08f1ff89c13975a644d93440bc07aa7d1743569a0c5e72e6de713396fef2767e240583370498ef75152dd449852424cda852d1a3309291bfbd27466a781c7b7ce538d14e9b0a5c610f9dc7eadee137c4c3f5324d6cef230942a55cad1bc51fc6fc742cd15c6dab94d33a34b5b95e1001add22f8eb9aadb305f472d8c84785e06bcfc5dad05e53370b9035c861b9874936c22cb59b9aaf8d53f5f9a3e9c9d047463e792732192700ae9b4f2279c023ad3daaaf88c6178b13676bdb48e7cd214ba559d3b62e8263b1ebf980af0a919724bf62eb59b51c53b7c2d952fea5519b3d7058f6a3e5d276bb43a6f109d377825081b6982fc24f7acbb1e21e411e89e0f8c0e305b975524ff3b9459843bfdd22da500977176c6810e8d404f103fe6fb2f877df52b9428675862e65f78463e22745055a809c445ffcaf8ee4db70b3c2c59bb249a7102f3db075cb9ebb1a2a62fc77c6654e730efe4d04b8a8eedb0ae04b790511847ad6a9546a729598f853cfe08110b8993ed1c587867ca45c8a0a99517631b38c05c2c49b8a16bba958ecffb4d5aa9aa69d9644e5b8e427b5059eab5a66b21e4cb38036d9ee85488e354f5b462d20960594459daa2041fb2579f5f551afa14129213d03b66864b5102690d98686ea33bec9fac91c56b90ce3962877a45bf477bea100e0f0df655794cdb59e452e3cd30fb82ae4eb9127946a96e00e6a164b4d94fef2e570f7f3af2bcc1630436270e58b78b050c749c22086b70666ad621b325d2b0ddc4e0efc06914028c57678e99fbb400ff56baeb474005f9b7302bb2f231239d72cc55028dee64148916b17b9dc0376da7cd5c811aa5ee411a4bdc8ce721aa5d13d65265998723eefd26c4e32564f4cbeda3ed0f97c2f701f8e5c1bbe5b7d476cf8d0d5473c960094e91e324c583f7c2555c13c24ce60c599f815b3eb5d96af2e67cd9f56a6874f4a7b9b394a65e05ce96a29071d1c9ff0b7fb9c25cf7225a92b86139ce17360c99545b46cc3bd083e92c81644b5d50eb55ba2767c8174bacdf6995d53d6963f9d6d88ab43e8e441954cf586d9a7b706bf150d42fcc06b103040a4babaa85f2fa9ae1e8e773a0582c636cf3544fd1bdfd1ce5fdf2a818be513f0d878026c0163c2

我在这里做了一个例子。

于 2012-09-04T20:08:16.820 回答
-3

我用这个:

define('SALT', '@my#secret');
$hash = MD5( SALT . Uniqid() );
于 2012-09-04T20:06:46.177 回答