---
title: "Hash Functions"
canonical: "https://docs.magma-maths.org/CodingTheory/PseudoRandomBitSequences/hash.html"
source: "https://docs.magma-maths.org/CodingTheory/PseudoRandomBitSequences/hash.md"
part: "Coding Theory"
chapter: "Pseudo-Random Bit Sequences"
breadcrumbs:
  - {"title": "Magma Handbook", "source": "../../index.md"}
  - {"title": "Coding Theory", "source": "../index-coding-theory.md"}
  - {"title": "Pseudo-Random Bit Sequences", "source": "index-pseudo-random-bit-sequences.md"}
prev: {"title": "Decimation", "source": "misc.md"}
next: {"title": "Optimization", "source": "../../Optimization/index-optimization.md"}
intrinsics:
  - {"name": "SHA1", "signature": "SHA1(S): SeqEnum -> MonStgElt"}
  - {"name": "SHA1", "signature": "SHA1(s): MonStgElt -> MonStgElt"}
examples:
  - "ex-d53a66"
---

# Hash Functions

A hash function maps a string of bytes of arbitrary length to a bit string of fixed length, called the digest of the input, in such a way that the digests of distinct inputs are spread uniformly over the range. Magma provides the SHA-1 function specified in FIPS PUB 180-1 (1995), whose digest has 160 bits. SHA-1 is no longer considered collision resistant, so it should not be relied on where an adversary may choose the input; its digests remain useful as fixed-length fingerprints of data, for instance to detect a change in a file or to index a table by the contents of a long string.

<a id="function-sha1-seqenum"></a>
## `SHA1(S): SeqEnum -> MonStgElt`

<a id="function-sha1-monstgelt"></a>
## `SHA1(s): MonStgElt -> MonStgElt`

Given a sequence $S$ of integers in the range $[0, 255]$, regarded as a sequence of bytes, or a string $s$, regarded as the sequence of its bytes, return the SHA-1 digest of the input as a string of 40 lowercase hexadecimal digits.

<a id="example-ex-d53a66"></a>
## `Example: Digest of a String and of Its Bytes (ex-d53a66)`

The digest of a string agrees with that of the sequence of its byte values, and matches the test vector published for the message `"abc"`.

```magma
> SHA1("abc");
a9993e364706816aba3e25717850c26c9cd0d89d
> S := [StringToCode(c) : c in Eltseq("abc")]; S;
[ 97, 98, 99 ]
> SHA1(S);
a9993e364706816aba3e25717850c26c9cd0d89d
> SHA1("abd") eq SHA1("abc");
false
```
