生成唯一的跟踪号 [英] Generating unique tracking numbers
问题描述
我正在构建一个小型Rails应用程序,我需要一种方法来生成跟踪号,以提供给客户以检查其状态.我想要的东西与主要运输公司所使用的相似,但期限不长.我以为12-16个字符长是可以的,但是我不希望输入0000000001这样的数字,因为人们只能输入递增的数字然后四处窥视.
I am building a small rails app and I need a way to generate tracking numbers to give to customers to check up on their status. I want something similar to what the major shipping companies use, but not as long. Im thinking 12-16 characters long is fine, but I dont want something like 0000000001 where people can just type in incremented numbers and peep around.
生成唯一的数字/字母组合跟踪代码的好方法是什么?
What is a good way to generate unique number/letter combination tracking codes?
推荐答案
对于毫无意义的数据,时间的散列加盐总是坚如磐石,而且不容易猜到(原谅Python,我已经听说过这个Ruby的东西,但从未将它掌握在我的手中):
For meaningless data, hashes of the time plus a salt are always rock solid, and can't be guessed easily (forgive the Python, I've heard of this Ruby thing but never held it in my hands):
>>> hashlib.md5(str(time.time()) + "!salt!").hexdigest()
'7a8b73fa7e0dadf246612e6001ede165'
如果愿意,可以将其缩小:
Shorten it, if you like:
>>> hashlib.md5(str(time.time()) + "!salt!").hexdigest()[:16]
'46ffb69ebc96412d'
如果愿意,可以使用整数来代替,但是长度可以随该代码而变化,除非您将键盘零位填充:
Use an integer instead, if you like, but the length has the chance to vary with this code, unless you zero-pad:
>>> int(hashlib.md5(str(time.time()) + "!salt!").hexdigest()[:13], 16)
1346212029197308
在"zomg md5不是加密安全"之前:
In before "zomg md5 isn't crypto secure":
>>> int(hashlib.sha256(str(time.time()) + "!salt!").hexdigest()[:13], 16)
1948411134966366
地狱,您甚至不必花费时间,可以使用自动递增的整数,只要您对其加盐即可:
Hell, you don't even have to use time, you can use an autoincrementing integer, as long as you salt it:
>>> int(hashlib.sha256(str(1) + "!salt!").hexdigest()[:13], 16)
1269883740611281
>>> int(hashlib.sha256(str(2) + "!salt!").hexdigest()[:13], 16)
3655373802716929
哈希.他们不能做什么?
这篇关于生成唯一的跟踪号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!