mirror of
https://github.com/space-wizards/RobustToolbox.git
synced 2026-09-15 14:52:35 +02:00
* New HWID system prep * Allow HWID to be disabled. Both client and server can now request HWID to be disabled. On the server via CVar, if disabled the client won't send it. On the client via env var, if disabled it won't be sent to the client. This involved moving legacy HWID to be sent in MsgEncryptionResponse instead of MsgLoginStart. This means the legacy HWID won't be available anymore if the connection isn't authenticated. * Fix tests * Fix another test * Review * Thanks Rider
67 lines
2.2 KiB
C#
67 lines
2.2 KiB
C#
using System;
|
|
using System.Diagnostics.CodeAnalysis;
|
|
using System.Text;
|
|
|
|
namespace Robust.Shared.Utility
|
|
{
|
|
internal static class Base64Helpers
|
|
{
|
|
/// <summary>
|
|
/// Converts a byte array such as a hash to a Base64 representation that is URL safe.
|
|
/// </summary>
|
|
/// <param name="data"></param>
|
|
/// <returns>A base64url string form of the byte array.</returns>
|
|
public static string ConvertToBase64Url(byte[]? data)
|
|
{
|
|
return data == null ? "" : ConvertToBase64Url(Convert.ToBase64String(data));
|
|
}
|
|
|
|
/// <summary>
|
|
/// Converts a a Base64 string to one that is URL safe.
|
|
/// </summary>
|
|
/// <returns>A base64url formed string.</returns>
|
|
public static string ConvertToBase64Url(string b64Str)
|
|
{
|
|
if (b64Str is null)
|
|
{
|
|
throw new ArgumentNullException(nameof(b64Str));
|
|
}
|
|
|
|
var cut = b64Str[^1] == '=' ? b64Str[^2] == '=' ? 2 : 1 : 0;
|
|
b64Str = new StringBuilder(b64Str).Replace('+', '-').Replace('/', '_').ToString(0, b64Str.Length - cut);
|
|
return b64Str;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Converts a URL-safe Base64 string into a byte array.
|
|
/// </summary>
|
|
/// <param name="s">A base64url formed string.</param>
|
|
/// <returns>The represented byte array.</returns>
|
|
public static byte[] ConvertFromBase64Url(string s)
|
|
{
|
|
var l = s.Length % 3;
|
|
var sb = new StringBuilder(s);
|
|
sb.Replace('-', '+').Replace('_', '/');
|
|
for (var i = 0; i < l; ++i)
|
|
{
|
|
sb.Append('=');
|
|
}
|
|
|
|
s = sb.ToString();
|
|
return Convert.FromBase64String(s);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Convert a byte array to base64. Returns null if the input byte array is null.
|
|
/// </summary>
|
|
[return: NotNullIfNotNull(nameof(data))]
|
|
public static string? ToBase64Nullable(byte[]? data)
|
|
{
|
|
if (data == null)
|
|
return null;
|
|
|
|
return Convert.ToBase64String(data, Base64FormattingOptions.None);
|
|
}
|
|
}
|
|
}
|