mirror of
https://github.com/beefytech/Beef.git
synced 2025-07-04 23:36:00 +02:00
Merge pull request #1335 from disarray2077/patch-1
Add Stream support to Cryptography classes
This commit is contained in:
commit
308eae6341
2 changed files with 70 additions and 4 deletions
|
@ -1,3 +1,5 @@
|
||||||
|
using system.IO;
|
||||||
|
|
||||||
namespace System.Security.Cryptography
|
namespace System.Security.Cryptography
|
||||||
{
|
{
|
||||||
struct HashEncode
|
struct HashEncode
|
||||||
|
@ -343,5 +345,26 @@ namespace System.Security.Cryptography
|
||||||
md5.Update(data);
|
md5.Update(data);
|
||||||
return md5.Finish();
|
return md5.Finish();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static Result<MD5Hash> 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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
using System.IO;
|
||||||
|
|
||||||
namespace System.Security.Cryptography
|
namespace System.Security.Cryptography
|
||||||
{
|
{
|
||||||
struct SHA256Hash
|
struct SHA256Hash
|
||||||
|
@ -42,9 +44,29 @@ namespace System.Security.Cryptography
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public override void ToString(String strBuffer)
|
||||||
|
{
|
||||||
|
for (let val in mHash)
|
||||||
|
{
|
||||||
|
val.ToString(strBuffer, "X2", null);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
struct SHA256
|
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
|
||||||
{
|
{
|
||||||
int mDataLen;
|
int mDataLen;
|
||||||
int mBitLength;
|
int mBitLength;
|
||||||
|
@ -112,7 +134,7 @@ namespace System.Security.Cryptography
|
||||||
mData = .(?);
|
mData = .(?);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Transform() mut
|
void Transform()
|
||||||
{
|
{
|
||||||
uint32 a, b, c, d, e, f, g, h, i, j, t1, t2;
|
uint32 a, b, c, d, e, f, g, h, i, j, t1, t2;
|
||||||
uint32[64] m = ?;
|
uint32[64] m = ?;
|
||||||
|
@ -155,7 +177,7 @@ namespace System.Security.Cryptography
|
||||||
mState[7] += h;
|
mState[7] += h;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Update(Span<uint8> data) mut
|
public void Update(Span<uint8> data)
|
||||||
{
|
{
|
||||||
for (int i = 0; i < data.Length; ++i)
|
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;
|
int i = mDataLen;
|
||||||
|
|
||||||
|
@ -228,5 +250,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