我需要在分布式系统上生成许多唯一的标识符。
我认为生成UUID。
- I'm sure to never have collision
- It's possible to find the location
- It's possible to have collision (The chance of a collision is really, really, really small, **but exist !**)
- I'm sure it's not possible to dermine the location (date and computer)
您有满足这两种约束的解决方案吗?
发布于 2017-10-06 13:54:52
也许我们可以将UUID1与由“秘密”密钥确定的“静态排列”函数结合起来?函数必须是双射的。
灵感来源:http://code.activestate.com/recipes/126037-getting-nth-permutation-of-a-sequence/
def getPerm(seq, index):
"Returns the <index>th permutation of <seq>"
seqc= list(seq[:])
seqn= [seqc.pop()]
divider= 2 # divider is meant to be len(seqn)+1, just a bit faster
while seqc:
index, new_index= index//divider, index%divider
seqn.insert(new_index, seqc.pop())
divider+= 1
return "".join(seqn)
secret=123456**123456 # a big secret integer
secure_id=getPerm("af5f2a30-aa9e-11e7-abc4-cec278b6b50a",secret)
# "1-a-faa4cba8c5b0ae372a72-ec-1fb9560e" ==> Same character, just change order
secure_id=getPerm("aaaaaaa0-bbbb-cccc-xxxx-yyyyyyyyyy0y",secret)
# "c-b-axaxxybyyyx0ybacyaya-cy-caybay0y" ==> Same secret => Same permutation (check position of caracter '0' and '-')我们可以通过保留'-‘字符的位置来改进排列的“混淆”。
发布于 2017-10-06 14:14:36
也许我们可以简单地编码UUID,使用Base64方便地传输/存储ID。编码也是一个双射函数。
import base64
def encode(string,key):
encoded_chars = []
for i in xrange(len(string)):
key_c = key[i % len(key)]
encoded_c = chr((256 + ord(string[i]) + ord(key_c)) % 256)
encoded_chars.append(encoded_c)
encoded_string = "".join(encoded_chars)
return base64.urlsafe_b64encode(encoded_string)
def decode(encoded_string,key):
decoded_chars = []
string=base64.urlsafe_b64decode(encoded_string)
for i in xrange(len(string)):
key_c = key[i % len(key)]
decoded_c = chr((256 + ord(string[i]) - ord(key_c)) % 256)
decoded_chars.append(decoded_c)
encoded_string = "".join(decoded_chars)
return "".join(decoded_chars)
secure_id=encode("af5f2a30-aa9e-11e7-abc4-cec278b6b50a","secret")
# "1MuY2JfVppWQ08at2JKUo8qroMbF1Zmh1srGpJys1ZvFp5XV"
uuid=decode(secure_id,"secret")
# "af5f2a30-aa9e-11e7-abc4-cec278b6b50a"通过这种方式,我总是有秘密的问题。
(注意:我们不需要解码功能)
https://stackoverflow.com/questions/46606865
复制相似问题