简体   繁体   English

从给定的数字字符串生成16位唯一的十六进制值

[英]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)是具有此块大小的块密码。

  1. 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(); 
  2. 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); 
  3. 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.

相关问题 从字符串生成9位唯一值 - Generate 9 digit unique value from a String 如何从给定的四个数字中生成一个唯一的数字,以及如何从生成的唯一数字中获取这些给定的数字? - How to generate an unique number from a given four numbers and getting these given numbers back from generated unique number? java-如何生成一个6位随机十六进制值 - java- how to generate a 6 digit random hexadecimal value 试图用Java中的每个唯一数字生成9位数字 - Trying to generate 9 digit numbers with each unique digits in Java 如何生成每个数字都是字符串或整数唯一的数字 - How to generate digit that every number is unique as a string or integer 如何通过部署在没有数据库的多个服务器上的服务为每个呼叫生成一个 16 位唯一号码? - How to generate a 16 digit unique number for each call by a service deployed on multiple servers without database? 如何从 Java UUID 生成唯一的 36 位(仅数字) - How to generate Unique 36 digit (ONLY NUMBER) from Java UUID 从唯一字符串输入生成唯一ID - Generate unique id from unique string input 什么java库提供了从给定字符集生成唯一随机字符串组合的工具? - What java library are there provides the the facility to generate unique random string combination from a given set of characters? 根据给定的数字集在Java中生成随机数 - To generate random numbers in java from given set of numbers
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM