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:
parent
152cb8321e
commit
0a1855bfb3
1 changed files with 24 additions and 1 deletions
|
@ -1,3 +1,5 @@
|
||||||
|
using System.IO;
|
||||||
|
|
||||||
namespace System.Security.Cryptography
|
namespace System.Security.Cryptography
|
||||||
{
|
{
|
||||||
struct SHA256Hash
|
struct SHA256Hash
|
||||||
|
@ -44,7 +46,7 @@ namespace System.Security.Cryptography
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
struct SHA256
|
class SHA256
|
||||||
{
|
{
|
||||||
int mDataLen;
|
int mDataLen;
|
||||||
int mBitLength;
|
int mBitLength;
|
||||||
|
@ -228,5 +230,26 @@ namespace System.Security.Cryptography
|
||||||
sha256.Update(data);
|
sha256.Update(data);
|
||||||
return sha256.Finish();
|
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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue