From 152cb8321e9eab1a555318a69213643ace62529c Mon Sep 17 00:00:00 2001 From: disarray2077 <86157825+disarray2077@users.noreply.github.com> Date: Sat, 1 Jan 2022 20:58:24 -0300 Subject: [PATCH] Add Stream support to MD5 --- .../corlib/src/Security/Cryptography/MD5.bf | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/BeefLibs/corlib/src/Security/Cryptography/MD5.bf b/BeefLibs/corlib/src/Security/Cryptography/MD5.bf index c79a0fd6..ed06f179 100644 --- a/BeefLibs/corlib/src/Security/Cryptography/MD5.bf +++ b/BeefLibs/corlib/src/Security/Cryptography/MD5.bf @@ -1,3 +1,5 @@ +using system.IO; + namespace System.Security.Cryptography { struct HashEncode @@ -343,5 +345,26 @@ namespace System.Security.Cryptography md5.Update(data); return md5.Finish(); } + + public static Result Hash(Stream stream) + { + let md5 = scope MD5(); + + loop: while (true) + { + uint8[4096] buffer; + switch (stream.TryRead(.(&buffer, 4096))) + { + case .Ok(let bytes): + if (bytes == 0) + break loop; + md5.Update(.(&buffer, bytes)); + case .Err(let err): + return .Err(err); + } + } + + return md5.Finish(); + } } }