TheFootClan
TheFootClan

Reputation: 165

Hashing a string to a specific length

I have been tasked with getting a string to encrypted to a certain length. Ideally....the function should take as parameters...

  1. String to be encrypted of variable size
  2. the length of the resulting encrypted string

It does not need to be decrypted. I've been looking at SHA384, but I've been unable to understand the samples online to the point of modifying it to make it hash to a specified length.

Upvotes: 2

Views: 6974

Answers (3)

zaph
zaph

Reputation: 112855

You can't request the length, that is part of the method.

If you want a shorter output just remove the extra bytes, each byte (and bit) is individually random, it makes no difference which bytes are removed.


Hashing is not encryption, it is a one-way non-reversavble function that takes a variable amount of input and generates a fixed amount of output.

Do you need encryption of a hash?

Upvotes: 3

Patrick
Patrick

Reputation: 175

A hash is a one way function meaning it cannot be reversed. If you want to generate a license key (for example), you can simply substring the hashed string.

Upvotes: 0

rossum
rossum

Reputation: 15685

You do not say if you want a cryptographically secure hash or not. The cryptographic hashes: SHA-1, SHA-2, SHA-3 and others come with a fixed range of output sizes, usually relatively limited.

Non-cryptographic hashes usually have a wider range of sizes available. For a non-crypto hash I often suggest the FNV hash, which is easy to implement and offers a wide range of output sizes: 32 bits to 1024 bits.

Upvotes: 2

Related Questions