Junior — Middle
Explain the principle of data transformation using hash functions.
sobes.tech AI
Answer from AI
A hash function is a method of transforming input data of arbitrary length into a fixed size, usually in the form of a number or a fixed-length string (hash code). The main principle is that each unique input produces a unique or nearly unique hash.
In C#, classes from the System.Security.Cryptography namespace, such as SHA256 or MD5 (although MD5 is considered insecure), are often used to compute hashes. An example of using SHA256:
using System.Security.Cryptography;
using System.Text;
string input = "Sample data";
using (SHA256 sha256 = SHA256.Create())
{
byte[] bytes = Encoding.UTF8.GetBytes(input);
byte[] hash = sha256.ComputeHash(bytes);
string hashString = BitConverter.ToString(hash).Replace("-", "");
Console.WriteLine(hashString);
}
Hash functions are used for data integrity verification, fast searching, password storage (with salt), and other tasks where unique and quick data identification is important.