summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid Peck <dmipeck@gmail.com>2024-05-14 19:12:38 +1200
committerDavid Peck <david@littlemonkey.co.nz>2024-05-14 19:22:50 +1200
commitd6715b4cdec889b503732ff0df963b3abea081d5 (patch)
treead379e279b3340b962fc6c58a1e69c283f7b0daa
parent557f63d03796db78255f055b6d06cb5f9195ff7e (diff)
downloadredot-engine-d6715b4cdec889b503732ff0df963b3abea081d5.tar.gz
Fix hashing context example
Example now works for any file size instead of just multiples of CHUNK_SIZE Example also uses correct method for looping over file data
-rw-r--r--doc/classes/HashingContext.xml10
1 files changed, 6 insertions, 4 deletions
diff --git a/doc/classes/HashingContext.xml b/doc/classes/HashingContext.xml
index f2681ae7b3..b42acb2b99 100644
--- a/doc/classes/HashingContext.xml
+++ b/doc/classes/HashingContext.xml
@@ -20,8 +20,9 @@
# Open the file to hash.
var file = FileAccess.open(path, FileAccess.READ)
# Update the context after reading each chunk.
- while not file.eof_reached():
- ctx.update(file.get_buffer(CHUNK_SIZE))
+ while file.get_position() &lt; file.get_length():
+ var remaining = file.get_length() - file.get_position()
+ ctx.update(file.get_buffer(min(remaining, CHUNK_SIZE)))
# Get the computed hash.
var res = ctx.finish()
# Print the result as hex string and array.
@@ -43,9 +44,10 @@
// Open the file to hash.
using var file = FileAccess.Open(path, FileAccess.ModeFlags.Read);
// Update the context after reading each chunk.
- while (!file.EofReached())
+ while (file.GetPosition() &lt; file.GetLength())
{
- ctx.Update(file.GetBuffer(ChunkSize));
+ int remaining = (int)(file.GetLength() - file.GetPosition());
+ ctx.Update(file.GetBuffer(Mathf.Min(remaining, ChunkSize)));
}
// Get the computed hash.
byte[] res = ctx.Finish();