How unique is UUID?

后端 未结 11 1857
终归单人心
终归单人心 2020-11-22 05:40

How safe is it to use UUID to uniquely identify something (I\'m using it for files uploaded to the server)? As I understand it, it is based off random numbers. However, it s

11条回答
  •  无人及你
    2020-11-22 05:54

    The answer to this may depend largely on the UUID version.

    Many UUID generators use a version 4 random number. However, many of these use Pseudo a Random Number Generator to generate them.

    If a poorly seeded PRNG with a small period is used to generate the UUID I would say it's not very safe at all. Some random number generators also have poor variance. i.e. favouring certain numbers more often than others. This isn't going to work well.

    Therefore, it's only as safe as the algorithms used to generate it.

    On the flip side, if you know the answer to these questions then I think a version 4 uuid should be very safe to use. In fact I'm using it to identify blocks on a network block file system and so far have not had a clash.

    In my case, the PRNG I'm using is a mersenne twister and I'm being careful with the way it's seeded which is from multiple sources including /dev/urandom. Mersenne twister has a period of 2^19937 − 1. It's going to be a very very long time before I see a repeat uuid.

    So pick a good library or generate it yourself and make sure you use a decent PRNG algorithm.

提交回复
热议问题