[#43] Client: Memory optimization
Signed-off-by: Pavel Gross <p.gross@yadro.com>
This commit is contained in:
parent
5e86f53b0e
commit
87fe8db674
76 changed files with 399 additions and 3668 deletions
|
@ -1,20 +1,9 @@
|
|||
using System;
|
||||
using System.IO;
|
||||
using System.Security.Cryptography;
|
||||
using System.Threading;
|
||||
using CommunityToolkit.HighPerformance;
|
||||
using Org.BouncyCastle.Crypto.Digests;
|
||||
|
||||
namespace FrostFS.SDK.Cryptography;
|
||||
|
||||
public static class Extentions
|
||||
{
|
||||
private static readonly SHA256 _sha256 = SHA256.Create();
|
||||
private static SpinLock _spinlockSha256;
|
||||
|
||||
private static readonly SHA512 _sha512 = SHA512.Create();
|
||||
private static SpinLock _spinlockSha512;
|
||||
|
||||
internal static byte[] RIPEMD160(this byte[] value)
|
||||
{
|
||||
var hash = new byte[20];
|
||||
|
@ -24,72 +13,4 @@ public static class Extentions
|
|||
digest.DoFinal(hash, 0);
|
||||
return hash;
|
||||
}
|
||||
|
||||
public static byte[] Sha256(this byte[] value)
|
||||
{
|
||||
bool lockTaken = false;
|
||||
try
|
||||
{
|
||||
_spinlockSha256.Enter(ref lockTaken);
|
||||
|
||||
return _sha256.ComputeHash(value);
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (lockTaken)
|
||||
{
|
||||
_spinlockSha256.Exit(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static byte[] Sha256(this ReadOnlyMemory<byte> value)
|
||||
{
|
||||
bool lockTaken = false;
|
||||
try
|
||||
{
|
||||
_spinlockSha256.Enter(ref lockTaken);
|
||||
|
||||
return _sha256.ComputeHash(value.AsStream());
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (lockTaken)
|
||||
{
|
||||
_spinlockSha256.Exit(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static byte[] Sha512(this ReadOnlyMemory<byte> value)
|
||||
{
|
||||
bool lockTaken = false;
|
||||
try
|
||||
{
|
||||
_spinlockSha512.Enter(ref lockTaken);
|
||||
|
||||
return _sha512.ComputeHash(value.AsStream());
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (lockTaken)
|
||||
_spinlockSha512.Exit(false);
|
||||
}
|
||||
}
|
||||
|
||||
public static byte[] Sha512(this Stream stream)
|
||||
{
|
||||
bool lockTaken = false;
|
||||
try
|
||||
{
|
||||
_spinlockSha512.Enter(ref lockTaken);
|
||||
|
||||
return _sha512.ComputeHash(stream);
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (lockTaken)
|
||||
_spinlockSha512.Exit(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue