[#20] Client: Optimize memory usage
Avoid memory allocation, use cache and static Signed-off-by: Pavel Gross <p.gross@yando.com>
This commit is contained in:
parent
35fe791406
commit
0ddde467cd
46 changed files with 596 additions and 372 deletions
|
@ -1,11 +1,18 @@
|
|||
using Google.Protobuf;
|
||||
using Org.BouncyCastle.Crypto.Digests;
|
||||
using System.Security.Cryptography;
|
||||
using System.Threading;
|
||||
|
||||
namespace FrostFS.SDK.Cryptography;
|
||||
|
||||
public static class Extentions
|
||||
{
|
||||
private static readonly SHA256 _sha256 = SHA256.Create();
|
||||
private static SpinLock _spinlockSha256 = new();
|
||||
|
||||
private static readonly SHA512 _sha512 = SHA512.Create();
|
||||
private static SpinLock _spinlockSha512 = new();
|
||||
|
||||
internal static byte[] RIPEMD160(this byte[] value)
|
||||
{
|
||||
var hash = new byte[20];
|
||||
|
@ -15,15 +22,41 @@ public static class Extentions
|
|||
digest.DoFinal(hash, 0);
|
||||
return hash;
|
||||
}
|
||||
|
||||
public static byte[] Sha256(this byte[] value)
|
||||
{
|
||||
var sha256 = SHA256.Create();
|
||||
return sha256.ComputeHash(value);
|
||||
}
|
||||
|
||||
public static ByteString Sha256(this IMessage data)
|
||||
{
|
||||
return ByteString.CopyFrom(data.ToByteArray().Sha256());
|
||||
}
|
||||
|
||||
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[] Sha512(this byte[] value)
|
||||
{
|
||||
bool lockTaken = false;
|
||||
try
|
||||
{
|
||||
_spinlockSha512.Enter(ref lockTaken);
|
||||
|
||||
return _sha512.ComputeHash(value);
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (lockTaken)
|
||||
_spinlockSha512.Exit(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue