summary refs log tree commit diff stats
path: root/lib/std
diff options
context:
space:
mode:
authorIgor Ribeiro de Assis <igor.assis@gmail.com>2020-11-18 20:55:49 +0100
committerGitHub <noreply@github.com>2020-11-18 20:55:49 +0100
commitbaaa19b92751598b6519a64eed2b1beb06b2e662 (patch)
tree051f0e0712520d27da10e9496f7afa08eeb202af /lib/std
parent87d3e5331a7838aa255deca8a840c2080b5424ce (diff)
downloadNim-baaa19b92751598b6519a64eed2b1beb06b2e662.tar.gz
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 <rumpf_a@web.de>

* Update lib/std/sha1.nim

Co-authored-by: Andreas Rumpf <rumpf_a@web.de>

* Directly break from loop

Co-authored-by: Andreas Rumpf <rumpf_a@web.de>

 [backport:1.2] [backport:1.4]
Diffstat (limited to 'lib/std')
-rw-r--r--lib/std/sha1.nim17
1 files changed, 16 insertions, 1 deletions
diff --git a/lib/std/sha1.nim b/lib/std/sha1.nim
index 8f35a44ff..9763b286e 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``.