# # # The Nim Compiler # (c) Copyright 2015 Nim Contributers # # See the file "copying.txt", included in this # distribution, for details about the copyright. # import strutils, unsigned const Sha1DigestSize = 20 type Sha1Digest = array[0 .. Sha1DigestSize-1, uint8] SecureHash* = distinct Sha1Digest proc sha1(src: string) : Sha1Digest proc secureHash*(str: string): SecureHash = SecureHash(sha1(str)) proc secureHashFile*(filename: string): SecureHash = secureHash(readFile(filename)) proc `$`*(self: SecureHash): string = result = "" for v in Sha1Digest(self): result.add(toHex(int(v), 2)) proc parseSecureHash*(hash: string): SecureHash = for i in 0.. = 56: innerHash(state, w) clearBuffer(w) w[15] = uint32(byteLen) shl 3 innerHash(state, w) # Store hash in result pointer, and make sure we get in in the correct order # on both endian models. for i in 0 .. Sha1DigestSize-1: result[i] = uint8((int(state[i shr 2]) shr ((3-(i and 3)) * 8)) and 255) proc sha1(src: string) : Sha1Digest = ## Calculate SHA1 from input string computeInternal(src)