I desperately need a hash / compression function that is suited to shorten text.

The context is this: I want to bring order and sort my box with hundreds of charging adapters (duh). After I determined for each one to what ever it belongs to, I want to label it accordingly.

Why don´t I just enumerate them? I have tried this in the past and it didn´t work out well for me. I forgot where the file is that mapped numbers, and also the 'order' didn´t make any sense.

What I want to do: have a function f that would do something like e.g. "Nikon CoolPix S3300" -> a9cd81d2, so a function that shortens a string that can contain alphanumeric letters and outputs a shorter (hexanumerical) string that could also decoded / reversed later to find out what was the original input.

If I wanted to simply encode the string, the output would always be longer. In ASCII or UTF-8, a single character would then by 1 byte each at least, meaning two hexadecimal numbers per letter. The result is that the output is not shorter but much longer.

I also considered hash functions. However, I´m unsure about this because of these factors:

  1. Most hash functions have a quite long output that wouldn´t fit onto my labels, e.g. 128 bits, which is 32 hex digits.
  2. Cryptographic hash functions aren´t intended to be reversed. I am aware that some of the old hash functions like e.g. md5 can be cracked, but I want something that could be decoded back in a matter of seconds, even on a CPU.

So in the end, I am looking for something with these properties:

  • Is fast to compute in both directions (hashing/reversing or encoding decoding)
  • Has outputs of equal sizes, like hash functions
  • The outputs are short, e.g. 4 bytes
  • Despite short outputs, the collision probability of natural language strings is generally small
  • Restoring / decoding the original string is accurate. If collisions exist, it is likely to get the original input string

Does a hash / encoding algorithm exist that can fulfill these needs?

  • $\begingroup$ I want to check if this is a hypothetical problem or not? Since if it is not, it really sounds like an XY problem. $\endgroup$
    – Emma
    Commented Apr 17, 2022 at 14:59
  • $\begingroup$ Not hypothetical, something I want to put into practice - confirmed. $\endgroup$
    – PandaPhi
    Commented Apr 17, 2022 at 20:44
  • $\begingroup$ Hash functions aren't compression algorithms, but you can implement a compression algorithm using a rolling hash to find repeating sequences in a file. $\endgroup$ Commented Jan 24 at 20:06

1 Answer 1


If you insist that it be reservible, what you want is data compression. Unfortunately in practice it is unlikely that the result will be short enough to be useful. At that point I recommend you buy a label-maker and label it with "Nikon CoolPix S3300" and give up on trying to encode that in some fancy way.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.