唯一,不可预测的12位整数ID [英] Unique, unpredictable, 12 digit, integer id
问题描述
我将如何生成此代码...我想保持主键顺序,并为添加到数据库中的每个新对象生成一个12位数字的唯一引脚.
How would I go about generating this... I want to keep my primary key sequential and have a 12 digit unique pin generated for each new object added to the database.
它不能自动递增的原因是我不希望序列号易于猜测.
The reason it cant just be autoincrement is i don't want the sequential numbers to be easily guessable.
它必须是整数,因为我将需要在电话簿上拨打验证码.
It needs to be integer numbers, because I'm going to have verification codes that need to be dialed on a phone pad.
推荐答案
使用唯一的递增数字和随机生成的数字的串联.
Use a concatenation of a unique incremented number and a randomly generated number.
唯一的递增数字可确保结果是唯一的,而随机生成的数字将使其难以猜测.
The unique incremented number ensures that the result is unique, and the randomly generated number makes it hardly guessable.
这是简单,并且保证没有没有碰撞(1个).结果为增量,部分随机和不可预测(前提是生成的随机数部分具有良好的PRNG).
This is simple and guaranteed to have no collision (1). The result is incremental, partly random, and non-predictable (provided that the random number part is generated with a good PRNG).
(1):您必须用零填充id
和random
,或者用一些非数字字符将它们分开.
(1): You have to either pad id
and random
with zeros, or to separate them with some non-digit character.
对于MySQL数据库,这可以转换为:
With a MySQL db, this translates to:
CREATE TABLE foo (
id int not null auto_increment,
random int not null,
...
primary key (id)
);
这篇关于唯一,不可预测的12位整数ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!