frostfs-sdk-csharp/src/FrostFS.SDK.Cryptography/UUID.cs
Pavel Gross 749000a090
All checks were successful
DCO / DCO (pull_request) Successful in 33s
lint-build / dotnet8.0 (pull_request) Successful in 1m4s
lint-build / dotnet8.0 (push) Successful in 49s
[#28] Client: Apply code optimizations
Signed-off-by: Pavel Gross <p.gross@yadro.com>
2024-11-18 17:00:19 +03:00

57 lines
1.3 KiB
C#

using System;
using Google.Protobuf;
namespace FrostFS.SDK.Cryptography;
public static class UUIDExtension
{
public static Guid ToUuid(this ByteString id)
{
if (id == null)
throw new ArgumentNullException(nameof(id));
var orderedBytes = GetGuidBytesDirectOrder(id.Span);
return new Guid(orderedBytes);
}
/// <summary>
/// Serializes Guid to binary representation in direct order bytes format
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public static byte[] ToBytes(this Guid id)
{
var bytes = id.ToByteArray();
var orderedBytes = GetGuidBytesDirectOrder(bytes);
return orderedBytes;
}
private static byte[] GetGuidBytesDirectOrder(ReadOnlySpan<byte> source)
{
if (source.Length != 16)
throw new ArgumentException("Wrong uuid binary format");
return [
source[3],
source[2],
source[1],
source[0],
source[5],
source[4],
source[7],
source[6],
source[8],
source[9],
source[10],
source[11],
source[12],
source[13],
source[14],
source[15]
];
}
}