偏执狂安全 UUID 生成
Paranoia secure UUID generation
我需要在分布式系统上生成许多唯一标识符。
- 在偏执狂模式下,我不想确定:
- 永不碰撞
- 防止确定计算机的位置 用于生成标识符(Mac 地址和日期时间)
我想生成 UUID。
如果我使用 UUID1(基于 MAC 地址、时间戳等...):
- 绝对不会有碰撞
- 可以找到位置
如果我使用 UUID4(基于随机生成器):
- 有可能发生碰撞(发生碰撞的几率真的非常非常小,但是存在!)
- 我确定无法确定位置(日期和计算机)
您有满足这两个约束的解决方案吗?
我们可以将 UUID1 与由 "secret" 键确定的 "static permutation" 函数结合起来吗?
该函数必须是双射的。
灵感来自: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 '-')
我们可以通过保留“-”字符的位置来改进排列"obfuscation"
也许我们可以简单地对 UUID 进行编码,使用 Base64 来简化 transfert/store 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"
这样一来,我的秘密问题就一直存在。
(注意:我们不需要解码功能)
我需要在分布式系统上生成许多唯一标识符。
- 在偏执狂模式下,我不想确定:
- 永不碰撞
- 防止确定计算机的位置 用于生成标识符(Mac 地址和日期时间)
我想生成 UUID。
如果我使用 UUID1(基于 MAC 地址、时间戳等...):
- 绝对不会有碰撞
- 可以找到位置
如果我使用 UUID4(基于随机生成器):
- 有可能发生碰撞(发生碰撞的几率真的非常非常小,但是存在!)
- 我确定无法确定位置(日期和计算机)
您有满足这两个约束的解决方案吗?
我们可以将 UUID1 与由 "secret" 键确定的 "static permutation" 函数结合起来吗? 该函数必须是双射的。
灵感来自: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 '-')
我们可以通过保留“-”字符的位置来改进排列"obfuscation"
也许我们可以简单地对 UUID 进行编码,使用 Base64 来简化 transfert/store 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"
这样一来,我的秘密问题就一直存在。
(注意:我们不需要解码功能)