62 lines
1.6 KiB
C#
62 lines
1.6 KiB
C#
using System;
|
|
using System.Security.Cryptography;
|
|
using Google.Protobuf;
|
|
|
|
namespace FrostFS.SDK.Cryptography;
|
|
|
|
public static class FrostFsExtensions
|
|
{
|
|
public static byte[] Sha256(this IMessage data)
|
|
{
|
|
using var sha256 = SHA256.Create();
|
|
using HashStream stream = new(sha256);
|
|
data.WriteTo(stream);
|
|
return stream.Hash();
|
|
}
|
|
|
|
public static Guid ToUuid(this ByteString id)
|
|
{
|
|
if (id == null)
|
|
throw new ArgumentNullException(nameof(id));
|
|
|
|
return new Guid(
|
|
(id[0] << 24) + (id[1] << 16) + (id[2] << 8) + id[3],
|
|
(short)((id[4] << 8) + id[5]),
|
|
(short)((id[6] << 8) + id[7]),
|
|
id[8],
|
|
id[9],
|
|
id[10],
|
|
id[11],
|
|
id[12],
|
|
id[13],
|
|
id[14],
|
|
id[15]);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Serializes Guid to binary representation in direct order bytes format
|
|
/// </summary>
|
|
/// <param name="id"></param>
|
|
/// <returns></returns>
|
|
public unsafe static void ToBytes(this Guid id, Span<byte> span)
|
|
{
|
|
var pGuid = (byte*)&id;
|
|
|
|
span[0] = pGuid[3];
|
|
span[1] = pGuid[2];
|
|
span[2] = pGuid[1];
|
|
span[3] = pGuid[0];
|
|
span[4] = pGuid[5];
|
|
span[5] = pGuid[4];
|
|
span[6] = pGuid[7];
|
|
span[7] = pGuid[6];
|
|
span[8] = pGuid[8];
|
|
span[9] = pGuid[9];
|
|
span[10] = pGuid[10];
|
|
span[11] = pGuid[11];
|
|
span[12] = pGuid[12];
|
|
span[13] = pGuid[13];
|
|
span[14] = pGuid[14];
|
|
span[15] = pGuid[15];
|
|
}
|
|
}
|