Skip to content

Commit

Permalink
Encodings.cs: Zero buffers.
Browse files Browse the repository at this point in the history
  • Loading branch information
samuel-lucas6 committed Nov 17, 2024
1 parent a46243a commit aae71ff
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions src/Geralt/Helpers/Encodings.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using static Interop.Libsodium;

namespace Geralt;
Expand All @@ -20,9 +21,14 @@ public static string ToHex(ReadOnlySpan<byte> data)
{
Validation.NotEmpty(nameof(data), data.Length);
Sodium.Initialize();
Span<byte> hex = new byte[data.Length * 2 + 1];
IntPtr ret = sodium_bin2hex(hex, (nuint)hex.Length, data, (nuint)data.Length);
return Marshal.PtrToStringAnsi(ret) ?? throw new FormatException("Error converting bytes to hex.");
Span<byte> hex = GC.AllocateArray<byte>(data.Length * 2 + 1, pinned: true);
try {
IntPtr ret = sodium_bin2hex(hex, (nuint)hex.Length, data, (nuint)data.Length);
return Marshal.PtrToStringAnsi(ret) ?? throw new FormatException("Error converting bytes to hex.");
}
finally {
CryptographicOperations.ZeroMemory(hex);
}
}

public static byte[] FromHex(string hex, string ignoreChars = HexIgnoreChars)
Expand All @@ -41,9 +47,14 @@ public static string ToBase64(ReadOnlySpan<byte> data, Base64Variant variant = B
Validation.NotEmpty(nameof(data), data.Length);
Sodium.Initialize();
int base64MaxLength = sodium_base64_encoded_len((nuint)data.Length, (int)variant);
Span<byte> base64 = new byte[base64MaxLength];
IntPtr ret = sodium_bin2base64(base64, (nuint)base64MaxLength, data, (nuint)data.Length, (int)variant);
return Marshal.PtrToStringAnsi(ret) ?? throw new FormatException("Error converting bytes to Base64.");
Span<byte> base64 = GC.AllocateArray<byte>(base64MaxLength, pinned: true);
try {
IntPtr ret = sodium_bin2base64(base64, (nuint)base64MaxLength, data, (nuint)data.Length, (int)variant);
return Marshal.PtrToStringAnsi(ret) ?? throw new FormatException("Error converting bytes to Base64.");
}
finally {
CryptographicOperations.ZeroMemory(base64);
}
}

public static byte[] FromBase64(string base64, Base64Variant variant = Base64Variant.Original, string ignoreChars = Base64IgnoreChars)
Expand Down

0 comments on commit aae71ff

Please sign in to comment.