[英]Generate a 16 digit unique Hexadecimal value from given string of numbers
How would I go about doing that? 我将如何去做? I tried using SHA-1 and MD5 but the output is too long for my requirements and truncation would not make it unique.
我尝试使用SHA-1和MD5,但是输出对于我的要求来说太长了,截断不会使其唯一。
Input : String containing numbers eg (0302160123456789) 输入:包含数字的字符串,例如(0302160123456789)
Received output : 30f2bddc3e2fba9c05d97d04f8da4449 接收的输出:30f2bddc3e2fba9c05d97d04f8da4449
Desired Output: Unique number within range (0000000000000000 - FFFFFFFFFFFFFFFF) and 16 characters long 所需的输出:范围(0000000000000000-FFFFFFFFFFFFFFFF)之内的唯一数字,且长度为16个字符
Any help/ pointers are greatly appreciated. 任何帮助/指针将不胜感激。
are you going to receive more than FFFFFFFFFFFFFFFF different strings? 您将收到超过FFFFFFFFFFFFFFFF个不同的字符串吗?
if not then it's a simple problem of generating integers: the first string will get 0 the next 1 etc; 如果不是,那么这就是生成整数的简单问题:第一个字符串将得到0,接下来的1将得到等。 you just keep a list of the strings and check if something the same appears.
您只需保留一个字符串列表,然后检查是否出现相同的内容。
How big is your input domain? 您的输入域有多大? If it is bigger than your output domain, then the Pigeon Hole principle applies and you can't get unique output by definition.
如果它大于您的输出域,那么将采用Pigeon Hole原理,并且根据定义您将无法获得唯一的输出。
If the input domain is smaller or equal to the output domain, then you can easily accomplish this with a Pseudo-Random Permutation (PRP) that block ciphers provide. 如果输入域小于或等于输出域,则可以使用分组密码提供的伪随机排列(PRP)轻松完成此操作。
The output of 16 hexits is equivalent to 8 bytes and equivalent to 64 bit. 16个十六进制的输出等效于8个字节,等效于64位。 DES (and Triple DES) is a block cipher that has this block size.
DES(和三重DES)是具有此块大小的块密码。
Parse the input string to a byte array in a compact fashion. 以紧凑的方式将输入字符串解析为字节数组。 If the input always consists of numerical digits, you can use Ebbe M. Pedersen's approach with
如果输入始终由数字组成,则可以使用Ebbe M. Pedersen的方法
byte[] plaintext = new BigInteger("0302160123456789").toByteArray();
Then you can generate some random, but fixed key of 24 bytes for Triple DES and instantiate the cipher with: 然后,您可以为Triple DES生成一些随机但固定的24字节密钥,并使用以下方法实例化密码:
Cipher c = Cipher.getInstance("DESede/ECB/PKCS5Padding"); c.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key, "DESede")); byte[] ciphertext = c.doFinal(plaintext);
Use some kind of Hex converter to get the representation you want. 使用某种十六进制转换器来获取所需的表示形式。
You can "hash" numbers up to 36028797018963968 with this. 您可以使用此方法“散列”最大为36028797018963968的数字。 If you want larger numbers (up to 9223372036854775808), then you need to use
"DESede/ECB/NoPadding"
and pad yourself with some padding bytes. 如果您想要更大的数字(最多9223372036854754775808),则需要使用
"DESede/ECB/NoPadding"
并使用一些填充字节来填充自己。
You could just convert your number to hex using BigInteger
like this: 您可以使用
BigInteger
将数字转换为十六进制,如下所示:
String id = new BigInteger("0302160123456789").toString(16);
System.out.println(id);
That gives: 这给出了:
112d022d2ed15
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.