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:
parent
87d3e5331a
commit
baaa19b927
1 changed files with 16 additions and 1 deletions
|
|
@ -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``.
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue