From baaa19b92751598b6519a64eed2b1beb06b2e662 Mon Sep 17 00:00:00 2001 From: Igor Ribeiro de Assis Date: Wed, 18 Nov 2020 20:55:49 +0100 Subject: [PATCH] Do not read the whole file to compute SHA1 hash (fixes 15997) (#16006) * Do not read the whole file to compute SHA1 hash (fixes 15997) * Update lib/std/sha1.nim Co-authored-by: Andreas Rumpf * Update lib/std/sha1.nim Co-authored-by: Andreas Rumpf * Directly break from loop Co-authored-by: Andreas Rumpf [backport:1.2] [backport:1.4] --- lib/std/sha1.nim | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/lib/std/sha1.nim b/lib/std/sha1.nim index 8f35a44ff6..9763b286e5 100644 --- a/lib/std/sha1.nim +++ b/lib/std/sha1.nim @@ -213,7 +213,22 @@ proc secureHashFile*(filename: string): SecureHash = ## **See also:** ## * `secureHash proc <#secureHash,openArray[char]>`_ for generating a ``SecureHash`` from a string ## * `parseSecureHash proc <#parseSecureHash,string>`_ for converting a string ``hash`` to ``SecureHash`` - secureHash(readFile(filename)) + const BufferLength = 8192 + + let f = open(filename) + var state = newSha1State() + var buffer = newString(BufferLength) + while true: + let length = readChars(f, buffer, 0, BufferLength) + if length == 0: + break + buffer.setLen(length) + state.update(buffer) + if length != BufferLength: + break + close(f) + + SecureHash(state.finalize()) proc `$`*(self: SecureHash): string = ## Returns the string representation of a ``SecureHash``.