Hashids是一个小巧的,开源的python库。可以使用他来进行对数字的编码加密成一个随机字符串。
安装:sudo pip install Hashids
使用例子:
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import hashids
def main():
hasher = hashids.Hashids(salt = "sasasa") # 添加一个salt ,Hashide通过接收salt值支持对散列的散列,可确保唯一性
ensecret = hasher.encode(12345678,478,78199,741)
print ensecret
desecret = hasher.decode(ensecret)
print desecret
#默认情况下哈系长度都是最短的,但是添加一个哈希长度,看起来更长
has_length_ensecret = hashids.Hashids(min_length = 16)
en_secret_length = has_length_ensecret.encode(123465,895,65)
print en_secret_length
de_secret_length = has_length_ensecret.decode(en_secret_length)
print de_secret_length
if __name__=="__main__":
main()