php - Sha1 substring question -
i making pastebin type site , trying make id random string paste.com/4rt65l
i getting sha1 of id before add database getting substring of first 8 characters of sha1. possibility of being double copy of same sha1? dont want accidentaly second paste id has been used?
well odds of having collision in 8 characters higher having collision 2 sha1 keys, doesn't mean happen.
i recommend testing on it. generate random input , see how long takes before have collision. if results, go it. otherwise, you'll need longer string.
edit: can calculate odds of collision looking @ birthday paradox.
basically, if taking first 8 hex digits sha-1, have 16**8 (4,294,967,296) different available combinations.
using online birthay paradox calculator, after 9200 hashes, have 1% chance of collision. take 30,000 hashes before have 10% chance, , 77,000 before have 50% chance.
its important point out long hash function decent job of being pseudo-random, doesn't matter 1 use (whether sha1, md5, or form of checksum)--these numbers assume random inputs, , can approach these values using increasingly better hash functions.
so in end, depends on how traffic expecting. if small site, can away it. if large amount of traffic, odds of collision high.
Comments
Post a Comment