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(); + } } } diff --git a/BeefLibs/corlib/src/Security/Cryptography/SHA256.bf b/BeefLibs/corlib/src/Security/Cryptography/SHA256.bf index c349bb19..32c21a6a 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 @@ -42,9 +44,29 @@ 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); + } } - struct SHA256 + class SHA256 { int mDataLen; int mBitLength; @@ -112,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 = ?; @@ -155,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) { @@ -170,7 +192,7 @@ namespace System.Security.Cryptography } } - public SHA256Hash Finish() mut + public SHA256Hash Finish() { int i = mDataLen; @@ -228,5 +250,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(); + } } }