I need a tool to generate a 6-digit "hash" from a file. Naturally, identical files should always create the same hash. What is a good way to do this?

  1. This is required on a vanilla debian system.
  2. I understand that with just 6-digits hash collisions can occur, but the hope is that it will be unlikely. In case of collision MD5 hashes can be used.
  3. Input files will most likely be PDFs, if that makes any difference.
  • You haven't said what you mean by "digit". I'm sure that Kamil Maciorowski's answer will work well for you, but you can reduce the likelihood of collisions by using crc32 (part of libarchive-zip-perl package), then convert the last six of the 8 hex digits to four characters by recoding in base64 ([a-zA-Z0-9/+]), thereby preserving the same level of uniqueness as the crc32. The recoding part can be done with { echo -n 0:; crc32 FileName|cut -c 3-8; }|xxd -r|base64. If you're interested, I'll document it fully in an answer. – AFH May 24 '17 at 23:24

Get any hash function with more digits and truncate.

Example (in Linux):

md5sum file | cut -c -6
  • 1
    Does it make a difference whether I take the first or last 6? – kabZX May 24 '17 at 21:14
  • 1
    @kabZX No. Just stick to your choice, of course. – Kamil Maciorowski May 24 '17 at 21:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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