# Hashing

This chapter is dedicated to the hash functions and objects, which are a subpart of the standard security module.

## Hash functions

Hashing is the ability to generate an almost unique representation from a string. Although, there is no guarantee that two different string will not produce the same result -- known as a collision -- the sophistication of the hashing function attempt to minimize such eventuality. The hashing process is not reversible.

There are several hashing functions available in the public domain. To name a few, MD5 is the message digest 5, and SHA is the secure hash algorithm. The following table illustrates the size of the result with different hashing functions.

### Hasher function result size

Function | Result size
--- | ---
MD-2 | 128 bits
MD-4 | 128 bits
MD-5 | 128 bits
SHA-1 | 160 bits
SHA-256 | 256 bits
SHA-384 | 384 bits
SHA-512 | 512 bits

### Hasher object

The `Hasher` class is a text hashing computation class. The class computes a hash value from a literal object, a buffer or an input stream. Once computed, the hash value is stored as an array of bytes that can be retrieved one by one or at all in the form of a string representation.

### Creating a hasher

Several hasher objects are available in the module. For example, the `Md5` object is the hasher object that implements the MD-5 algorithm. The constructor does not take any argument.

# get a MD-5 hasher const md (afnix:sec:Md5) # check the object afnix:sec:hasher-p md # true

The `compute` method computes the hash value. For example, the string "abc" returns the value "900150983CD24FB0D6963F7D28E17F72" which is 16 bytes long.

const hval (md:compute "abc")

### Creating a SHA hasher

There are several SHA objects that produces results of different size as indicated in the next table.

### Secure hash algorithm size and constructor

Hasher | Size | Constructor
--- | --- | ---
SHA-1 | 160 bits | Sha1
SHA-256 | 256 bits | Sha256
SHA-384 | 384 bits | Sha384
SHA-512 | 512 bits | Sha512

The `compute` method computes the hash value. For example, the string "abc" returns with SHA-1 the value "A9993E364706816ABA3E25717850C26C9CD0D89D" which is here 20 bytes long.