1
0
Fork 0
mirror of https://github.com/beefytech/Beef.git synced 2025-06-25 19:18:01 +02:00

Add Stream support to SHA256

This commit is contained in:
disarray2077 2022-01-01 21:00:31 -03:00 committed by GitHub
parent 152cb8321e
commit 0a1855bfb3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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<SHA256Hash> 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();
}
}
}