safe enough 8-character short unique random string

前端 未结 7 1348
甜味超标
甜味超标 2021-02-01 01:34

I am trying to compute 8-character short unique random filenames for, let\'s say, thousands of files without probable name collision. Is this method safe enough?



        
7条回答
  •  -上瘾入骨i
    2021-02-01 02:23

    I am using hashids to convert a timestamp into a unique id. (You can even convert it back to a timestamp if you want).

    The drawback with this is if you create ids too fast, you will get a duplicate. But, if you are generating them with time in-between, then this is an option.

    Here is an example:

    from hashids import Hashids
    from datetime import datetime
    hashids = Hashids(salt = "lorem ipsum dolor sit amet", alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890")
    print(hashids.encode(int(datetime.today().timestamp()))) #'QJW60PJ1' when I ran it
    

提交回复
热议问题