mirror of
https://github.com/nim-lang/Nim.git
synced 2026-02-18 08:58:39 +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]
(cherry picked from commit baaa19b927)
This commit is contained in:
committed by
narimiran
parent
71c0b6e76c
commit
ee6b9d37c0
@@ -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