mirror of
https://github.com/nim-lang/Nim.git
synced 2026-02-17 00:24:16 +00:00
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]
This commit is contained in:
committed by
GitHub
parent
87d3e5331a
commit
baaa19b927
@@ -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``.
|
||||
|
||||
Reference in New Issue
Block a user