0

I have the following strings:

"184500000712_1_new_store1",
"184500000712_4_new_store1",
"184500000712_12_new_store1",
"195600000819_1_new_store2",
...

I am trying to turn each individual string into a unique 10-digit code like so:

184500000712_1_new_store1 => K14K4O9WVQ
184500000712_4_new_store1 => E93N7L2PXC
184500000712_12_new_store1 => A89F5I6JIO
195600000819_1_new_store2 => J00B1I5KFU

Also the string-to-code output must always be consistent. Fe, utf-8 base64 encoding of the string 184500000712_1_new_store1 would always output the code MTg0NTAwMDAwNzEyXzFfbmV3X3N0b3JlMQ==

Thanks!

Artjom B.
  • 61,146
  • 24
  • 125
  • 222
user1828780
  • 537
  • 1
  • 8
  • 22
  • If you can compress each of the strings into 7.5 bytes (yes, I mean 60 bit), then you can use a pseudo-random permutation to get a unique resulting value. If you can't compress it, then the only way would be to manage a (potentially huge) map of known inputs and randomly generated outputs. – Artjom B. Mar 30 '16 at 20:07
  • Nice! I hadn't thought of compression. I have some strings that are 10X the length of the example strings. Could compression still preserve uniqueness – user1828780 Mar 31 '16 at 16:13
  • As I said, you would need to "compress" the input strings to 60 **bits**. This would only be possible if they are actually of low entropy or you can find a way to compress them yourself, because you can see some kind of rule in them. If you can't then you're out of luck. The best you could then do, is using a hash function, truncate the output to 60 bits, used base64 encoding and remove the padding characters. A cryptographic hash function does not guarantee uniqueness, but it makes collisions improbable. – Artjom B. Mar 31 '16 at 16:17

1 Answers1

0

If it doesn't need to be a secure hash (which it probably couldn't be, given that it is only 10 characters), you can check this answer for a quick hash generator: Generate a Hash from string in Javascript/jQuery

Truncate the string to your desired length and you should be ok. Depending on how many of these you're creating, you may need to be mindful of collisions.

Community
  • 1
  • 1
Michael
  • 668
  • 5
  • 13