Junior — Middle
Can you list the main characteristics of hash functions?
sobes.tech AI
Answer from AI
The main characteristics of hash functions:
- Deterministic: for the same input, the hash function always returns the same hash.
- Fast computation: the hash should be computed quickly for any input.
- Distribution: hashes should be evenly distributed across the range to minimize collisions.
- Collision resistance: the probability that different inputs produce the same hash (collision) should be minimal.
- Pre-image resistance (for cryptographic hash functions): it should be infeasible to reconstruct the original data from the hash.
Example in Go:
import (
"fmt"
"hash/fnv"
)
func hash(s string) uint32 {
h := fnv.New32a()
h.Write([]byte(s))
return h.Sum32()
}
func main() {
fmt.Println(hash("example"))
}
Here, the FNV hash function is used for fast string hashing.