Note: In order to use this module, run nimble install checksums.

SHA-1 (Secure Hash Algorithm 1) is a cryptographic hash function which takes an input and produces a 160-bit (20-byte) hash value known as a message digest.

Please note that SHA-1 has been formally deprecated since 2011 and it is strongly recommended to switch to stronger hash functions such as the SHA-2 or SHA-3 family.

Even though SHA-1 is formally deprecated, this module itself is not deprecated and will continue to be usable.

See also

Example:

  1. import src/checksums/sha1
  2. let accessName = secureHash("John Doe")
  3. assert $accessName == "AE6E4D1209F17B460503904FAD297B31E9CF6362"

Example: cmd: -r:off

  1. import src/checksums/sha1
  2. let
  3. a = secureHashFile("myFile.nim")
  4. b = parseSecureHash("10DFAEBF6BFDBC7939957068E2EFACEC4972933C")
  5. assert a == b, "files don't match"

Types

  1. SecureHash = distinct Sha1Digest
  1. Sha1Digest = array[0 .. 20 - 1, uint8]
  1. Sha1State = object

Procs

  1. proc `$`(self: SecureHash): string {....raises: [], tags: [], forbids: [].}

Returns the string representation of a SecureHash.

See also:

Example:

  1. let hash = secureHash("Hello World")
  2. assert $hash == "0A4D55A8D778E5022FAB701977C5D840BBC486D0"
  1. proc `==`(a, b: SecureHash): bool {....raises: [], tags: [], forbids: [].}

Checks if two SecureHash values are identical.

Example:

  1. let
  2. a = secureHash("Hello World")
  3. b = secureHash("Goodbye World")
  4. c = parseSecureHash("0A4D55A8D778E5022FAB701977C5D840BBC486D0")
  5. assert a != b
  6. assert a == c
  1. proc finalize(ctx: var Sha1State): Sha1Digest {....raises: [], tags: [],
  2. forbids: [].}

Finalizes the Sha1State and returns a Sha1Digest.

If you use the secureHash proc, there’s no need to call this function explicitly.

  1. proc isValidSha1Hash(s: string): bool {....raises: [], tags: [], forbids: [].}

Checks if a string is a valid sha1 hash sum.

  1. proc newSha1State(): Sha1State {....raises: [], tags: [], forbids: [].}

Creates a Sha1State.

If you use the secureHash proc, there’s no need to call this function explicitly.

  1. proc parseSecureHash(hash: string): SecureHash {....raises: [ValueError], tags: [],
  2. forbids: [].}

Converts a string hash to a SecureHash.

See also:

Example:

  1. let
  2. hashStr = "0A4D55A8D778E5022FAB701977C5D840BBC486D0"
  3. secureHash = secureHash("Hello World")
  4. assert secureHash == parseSecureHash(hashStr)
  1. proc secureHash(str: openArray[char]): SecureHash {....raises: [], tags: [],
  2. forbids: [].}

Generates a SecureHash from str.

See also:

Example:

  1. let hash = secureHash("Hello World")
  2. assert hash == parseSecureHash("0A4D55A8D778E5022FAB701977C5D840BBC486D0")
  1. proc secureHashFile(filename: string): SecureHash {....raises: [IOError],
  2. tags: [ReadIOEffect], forbids: [].}

Generates a SecureHash from a file.

See also:

  1. proc update(ctx: var Sha1State; data: openArray[char]) {....raises: [], tags: [],
  2. forbids: [].}

Updates the Sha1State with data.

If you use the secureHash proc, there’s no need to call this function explicitly.