Sobes.tech
Junior — Middle

Can you explain what a hash function is and how it is used to obtain a hash sum?

sobes.tech AI

Answer from AI

A hash function is an algorithm that transforms input data of arbitrary length into a fixed-length string (hash sum). The hash sum serves as a unique "fingerprint" of the data.

Main properties of hash functions:

  • Fast computation.
  • One-wayness (difficult to reconstruct the original data from the hash).
  • Minimization of collisions (different data with the same hash).

Applications:

  • File integrity verification (comparing hashes).
  • Password storage (with salt).
  • Fast data search and comparison.

Example in Python to obtain a SHA-256 hash sum:

import hashlib

text = "Sample data"
hash_object = hashlib.sha256(text.encode())
hash_hex = hash_object.hexdigest()
print(hash_hex)

This will output a 64-character string — the hash sum of the input data.