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 1/4] 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(); + } } } 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 2/4] 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(); + } } } From 794bab2bfdaf8db54926b891f6a562780c34bedf Mon Sep 17 00:00:00 2001 From: disarray2077 <86157825+disarray2077@users.noreply.github.com> Date: Sat, 1 Jan 2022 21:03:29 -0300 Subject: [PATCH 3/4] Add missing methods to SHA256Hash --- .../src/Security/Cryptography/SHA256.bf | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/BeefLibs/corlib/src/Security/Cryptography/SHA256.bf b/BeefLibs/corlib/src/Security/Cryptography/SHA256.bf index c37c98d9..90d79f68 100644 --- a/BeefLibs/corlib/src/Security/Cryptography/SHA256.bf +++ b/BeefLibs/corlib/src/Security/Cryptography/SHA256.bf @@ -44,6 +44,26 @@ namespace System.Security.Cryptography return true; } } + + public override void ToString(String strBuffer) + { + for (let val in mHash) + { + val.ToString(strBuffer, "X2", null); + } + } + + public void Encode(String outStr) + { +#unwarn + HashEncode.HashEncode64(((uint64*)&mHash)[0], outStr); +#unwarn + HashEncode.HashEncode64(((uint64*)&mHash)[1], outStr); +#unwarn + HashEncode.HashEncode64(((uint64*)&mHash)[2], outStr); +#unwarn + HashEncode.HashEncode64(((uint64*)&mHash)[3], outStr); + } } class SHA256 From 3018a6030e24730d989d85e387e07e29861bb4b0 Mon Sep 17 00:00:00 2001 From: disarray2077 <86157825+disarray2077@users.noreply.github.com> Date: Sun, 2 Jan 2022 01:12:13 -0300 Subject: [PATCH 4/4] Remove unnecessary `mut`s This fixes some warnings because `SHA256` isn't a struct anymore. --- BeefLibs/corlib/src/Security/Cryptography/SHA256.bf | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/BeefLibs/corlib/src/Security/Cryptography/SHA256.bf b/BeefLibs/corlib/src/Security/Cryptography/SHA256.bf index 90d79f68..32c21a6a 100644 --- a/BeefLibs/corlib/src/Security/Cryptography/SHA256.bf +++ b/BeefLibs/corlib/src/Security/Cryptography/SHA256.bf @@ -134,7 +134,7 @@ namespace System.Security.Cryptography mData = .(?); } - void Transform() mut + void Transform() { uint32 a, b, c, d, e, f, g, h, i, j, t1, t2; uint32[64] m = ?; @@ -177,7 +177,7 @@ namespace System.Security.Cryptography mState[7] += h; } - public void Update(Span data) mut + public void Update(Span data) { for (int i = 0; i < data.Length; ++i) { @@ -192,7 +192,7 @@ namespace System.Security.Cryptography } } - public SHA256Hash Finish() mut + public SHA256Hash Finish() { int i = mDataLen;