From d0b166d8e4e326e84e7e43ac1ed5bc292be1d2fa Mon Sep 17 00:00:00 2001 From: Raul Santos Date: Wed, 5 Oct 2022 18:22:06 +0200 Subject: C#: Cleanup and sync crypto/buffer StringExtensions with core - Replaced `MD5Buffer`, `MD5Text`, `SHA256Buffer` and `SHA256Text` implementation to use the `System.Security.Cryptography` classes and avoid marshaling. - Added `SHA1Buffer` and `SHA1Text`. - Renamed `ToUTF8` to `ToUTF8Buffer`. - Renamed `ToAscii` to `ToASCIIBuffer`. - Added `ToUTF16Buffer` and `ToUTF32Buffer`. - Added `GetStringFromUTF16` and `GetStringFromUTF32`. --- .../GodotSharp/GodotSharp/Core/StringExtensions.cs | 128 +++++++++++++++------ 1 file changed, 94 insertions(+), 34 deletions(-) (limited to 'modules/mono/glue') diff --git a/modules/mono/glue/GodotSharp/GodotSharp/Core/StringExtensions.cs b/modules/mono/glue/GodotSharp/GodotSharp/Core/StringExtensions.cs index 92ea24f90b..dd399fb24b 100644 --- a/modules/mono/glue/GodotSharp/GodotSharp/Core/StringExtensions.cs +++ b/modules/mono/glue/GodotSharp/GodotSharp/Core/StringExtensions.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Globalization; using System.IO; using System.Security; +using System.Security.Cryptography; using System.Text; using System.Text.RegularExpressions; using Godot.NativeInterop; @@ -684,8 +685,8 @@ namespace Godot } /// - /// Converts the given byte array of ASCII encoded text to a string. - /// Faster alternative to if the + /// Converts ASCII encoded array to string. + /// Fast alternative to if the /// content is ASCII-only. Unlike the UTF-8 function this function /// maps every byte to a character in the array. Multibyte sequences /// will not be interpreted correctly. For parsing user input always @@ -699,13 +700,35 @@ namespace Godot } /// - /// Converts the given byte array of UTF-8 encoded text to a string. + /// Converts UTF-16 encoded array to string using the little endian byte order. + /// + /// A byte array of UTF-16 characters. + /// A string created from the bytes. + public static string GetStringFromUTF16(this byte[] bytes) + { + return Encoding.Unicode.GetString(bytes); + } + + /// + /// Converts UTF-32 encoded array to string using the little endian byte order. + /// + /// A byte array of UTF-32 characters. + /// A string created from the bytes. + public static string GetStringFromUTF32(this byte[] bytes) + { + return Encoding.UTF32.GetString(bytes); + } + + /// + /// Converts UTF-8 encoded array to string. /// Slower than but supports UTF-8 /// encoded data. Use this function if you are unsure about the /// source of the data. For user input this function /// should always be preferred. /// - /// A byte array of UTF-8 characters (a character may take up multiple bytes). + /// + /// A byte array of UTF-8 characters (a character may take up multiple bytes). + /// /// A string created from the bytes. public static string GetStringFromUTF8(this byte[] bytes) { @@ -1292,10 +1315,9 @@ namespace Godot /// The MD5 hash of the string. public static byte[] MD5Buffer(this string instance) { - using godot_string instanceStr = Marshaling.ConvertStringToNative(instance); - NativeFuncs.godotsharp_string_md5_buffer(instanceStr, out var md5Buffer); - using (md5Buffer) - return Marshaling.ConvertNativePackedByteArrayToSystemArray(md5Buffer); +#pragma warning disable CA5351 // Do Not Use Broken Cryptographic Algorithms + return MD5.HashData(Encoding.UTF8.GetBytes(instance)); +#pragma warning restore CA5351 } /// @@ -1306,10 +1328,7 @@ namespace Godot /// The MD5 hash of the string. public static string MD5Text(this string instance) { - using godot_string instanceStr = Marshaling.ConvertStringToNative(instance); - NativeFuncs.godotsharp_string_md5_text(instanceStr, out var md5Text); - using (md5Text) - return Marshaling.ConvertStringToManaged(md5Text); + return instance.MD5Buffer().HexEncode(); } /// @@ -1536,12 +1555,28 @@ namespace Godot return instance.TrimEnd(chars.ToCharArray()); } - if (end == len - 1) - { - return instance; - } + /// + /// Returns the SHA-1 hash of the string as an array of bytes. + /// + /// + /// The string to hash. + /// The SHA-1 hash of the string. + public static byte[] SHA1Buffer(this string instance) + { +#pragma warning disable CA5350 // Do Not Use Weak Cryptographic Algorithms + return SHA1.HashData(Encoding.UTF8.GetBytes(instance)); +#pragma warning restore CA5350 + } - return instance.Substr(0, end + 1); + /// + /// Returns the SHA-1 hash of the string as a string. + /// + /// + /// The string to hash. + /// The SHA-1 hash of the string. + public static string SHA1Text(this string instance) + { + return instance.SHA1Buffer().HexEncode(); } /// @@ -1552,10 +1587,7 @@ namespace Godot /// The SHA-256 hash of the string. public static byte[] SHA256Buffer(this string instance) { - using godot_string instanceStr = Marshaling.ConvertStringToNative(instance); - NativeFuncs.godotsharp_string_sha256_buffer(instanceStr, out var sha256Buffer); - using (sha256Buffer) - return Marshaling.ConvertNativePackedByteArrayToSystemArray(sha256Buffer); + return SHA256.HashData(Encoding.UTF8.GetBytes(instance)); } /// @@ -1566,10 +1598,7 @@ namespace Godot /// The SHA-256 hash of the string. public static string SHA256Text(this string instance) { - using godot_string instanceStr = Marshaling.ConvertStringToNative(instance); - NativeFuncs.godotsharp_string_sha256_text(instanceStr, out var sha256Text); - using (sha256Text) - return Marshaling.ConvertStringToManaged(sha256Text); + return instance.SHA256Buffer().HexEncode(); } /// @@ -1751,13 +1780,15 @@ namespace Godot /// /// Converts the String (which is a character array) to PackedByteArray (which is an array of bytes). - /// The conversion is speeded up in comparison to with the assumption - /// that all the characters the String contains are only ASCII characters. + /// The conversion is faster compared to , + /// as this method assumes that all the characters in the String are ASCII characters. /// - /// + /// + /// + /// /// The string to convert. /// The string as ASCII encoded bytes. - public static byte[] ToAscii(this string instance) + public static byte[] ToASCIIBuffer(this string instance) { return Encoding.ASCII.GetBytes(instance); } @@ -1785,14 +1816,43 @@ namespace Godot } /// - /// Converts the String (which is an array of characters) to PackedByteArray (which is an array of bytes). - /// The conversion is a bit slower than , but supports all UTF-8 characters. - /// Therefore, you should prefer this function over . + /// Converts the string (which is an array of characters) to an UTF-16 encoded array of bytes. + /// + /// + /// + /// + /// The string to convert. + /// The string as UTF-16 encoded bytes. + public static byte[] ToUTF16Buffer(this string instance) + { + return Encoding.Unicode.GetBytes(instance); + } + + /// + /// Converts the string (which is an array of characters) to an UTF-32 encoded array of bytes. + /// + /// + /// + /// + /// The string to convert. + /// The string as UTF-32 encoded bytes. + public static byte[] ToUTF32Buffer(this string instance) + { + return Encoding.UTF32.GetBytes(instance); + } + + /// + /// Converts the string (which is an array of characters) to an UTF-8 encoded array of bytes. + /// The conversion is a bit slower than , + /// but supports all UTF-8 characters. Therefore, you should prefer this function + /// over . /// - /// + /// + /// + /// /// The string to convert. /// The string as UTF-8 encoded bytes. - public static byte[] ToUTF8(this string instance) + public static byte[] ToUTF8Buffer(this string instance) { return Encoding.UTF8.GetBytes(instance); } -- cgit v1.2.3