I am trying to figure out if I can get the hash value of a string using the standard library hash function in C. I am using gcc
compiler on Ubuntu
.
Asked
Active
Viewed 236 times
1

Franc
- 319
- 9
- 28
-
3Which hash value do you want? MD5? SHA1? SHA256? SHA512? There are others. – Tangentially Perpendicular Jan 05 '23 at 06:40
-
[related](https://stackoverflow.com/questions/47732850/is-there-a-c-hashing-function-with-no-dependencies) – Déjà vu Jan 05 '23 at 06:44
-
2There isn't a hash function defined by the C Standard. There isn't a hash function defined by POSIX either, AFAIK. So, you will have to look at other libraries for such a function. There are many hashing algorithms, of greater or lesser value. How secure must your hashed value be? How long must it be? What are you going to use the hash for? – Jonathan Leffler Jan 05 '23 at 06:50
-
@JonathanLeffler Yeah, POSIX has hash table routines (`hcreate()` etc.), but no hash function for some reason. – Shawn Jan 05 '23 at 07:27
-
@JonathanLeffler My requirement is to hash the string and get integer value. So I don't have to use strcmp() instead I can use == operator directly. This is an optimisation approach for a specific use case I am looking into. – Franc Jan 05 '23 at 12:24
-
1@Franc CRC-32 should do what you want. There isn't one in the standard library though there are lots of implementations online, including here https://stackoverflow.com/questions/26049150/calculate-a-32-bit-crc-lookup-table-in-c-c. – Dipstick Jan 05 '23 at 13:23
-
see https://stackoverflow.com/q/251346/1216776 – stark Jan 05 '23 at 14:01
1 Answers
0
Is there a standard library function to get hash value of a string in C?
No.
gcc compiler on Ubuntu.
I think worth mentioning, there is a super old function crypt
used by the Linux system password database (see man 5 shadow
) https://www.gnu.org/software/libc/manual/html_node/Passphrase-Storage.html .

KamilCuk
- 120,984
- 8
- 59
- 111