User ID obfuscation

后端 未结 3 533
醉话见心
醉话见心 2020-12-24 09:31

I expect this\'s been asked before but haven\'t really found an appropriate answer here and also don\'t have the time to come up with my own solution...

<
3条回答
  •  眼角桃花
    2020-12-24 10:06

    1. Use UUIDs

    2. Make another column in the user table for, e.g. 64 bit integers, and fill it with a random number (each time a new user registered - generate it and check it's unique). A number looks better than UUID, however a bit more coding required.

    3. Use maths. ;) You could generate pair of numbers X, Y such as X*Y = 1 (mod M). E.g. X=10000000019L, Y=1255114267L, and M=2^30. Then, you will have two simple functions:

    .

    long encode(long id)
    {  return (id * X) & M; }
    
    long decode(long encodedId)
    {  return (encodedId * Y) & M; }
    

    It will produce nearly random encoded ids. It's easy, but hackable. If someone would bother to hack it, he will be able to guess your numbers and see encoded values too. However, I am not completely sure which complexity it is, but as I remember it's not very easy to hack.

提交回复
热议问题