From 0a1855bfb3cb425785084befeba7581b5db83b9e Mon Sep 17 00:00:00 2001 From: disarray2077 <86157825+disarray2077@users.noreply.github.com> Date: Sat, 1 Jan 2022 21:00:31 -0300 Subject: [PATCH] Add Stream support to SHA256 --- .../src/Security/Cryptography/SHA256.bf | 25 ++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/BeefLibs/corlib/src/Security/Cryptography/SHA256.bf b/BeefLibs/corlib/src/Security/Cryptography/SHA256.bf index c349bb19..c37c98d9 100644 --- a/BeefLibs/corlib/src/Security/Cryptography/SHA256.bf +++ b/BeefLibs/corlib/src/Security/Cryptography/SHA256.bf @@ -1,3 +1,5 @@ +using System.IO; + namespace System.Security.Cryptography { struct SHA256Hash @@ -44,7 +46,7 @@ namespace System.Security.Cryptography } } - struct SHA256 + class SHA256 { int mDataLen; int mBitLength; @@ -228,5 +230,26 @@ namespace System.Security.Cryptography sha256.Update(data); return sha256.Finish(); } + + public static Result Hash(Stream stream) + { + let sha256 = scope SHA256(); + + loop: while (true) + { + uint8[4096] buffer; + switch (stream.TryRead(.(&buffer, 4096))) + { + case .Ok(let bytes): + if (bytes == 0) + break loop; + sha256.Update(.(&buffer, bytes)); + case .Err(let err): + return .Err(err); + } + } + + return sha256.Finish(); + } } }