-
-
Notifications
You must be signed in to change notification settings - Fork 178
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve base64 methods on .NET 6 (#712)
- Loading branch information
1 parent
2c2c578
commit 15b12f5
Showing
9 changed files
with
141 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
using BenchmarkDotNet.Attributes; | ||
using System.Text; | ||
|
||
namespace Fluid.Benchmarks | ||
{ | ||
// | Method | Mean | Error | StdDev | Gen0 | Allocated | | ||
// |------------------------ |---------:|----------:|---------:|-------:|----------:| | ||
// | HashSha256ToHex | 213.7 ns | 48.62 ns | 2.67 ns | 0.0279 | 264 B | | ||
// | HashSha256StringBuilder | 666.4 ns | 369.69 ns | 20.26 ns | 0.1774 | 1672 B | | ||
|
||
[MemoryDiagnoser, ShortRunJob] | ||
public class HashingBenchmarks | ||
{ | ||
private readonly string _value = "this is some text to hash"; | ||
|
||
[Benchmark] | ||
public string HashSha256ToHex() | ||
{ | ||
var hash = System.Security.Cryptography.SHA256.HashData(Encoding.UTF8.GetBytes(_value)); | ||
return Fluid.Utils.HexUtilities.ToHexLower(hash); | ||
} | ||
|
||
[Benchmark] | ||
public string HashSha256StringBuilder() | ||
{ | ||
using var provider = System.Security.Cryptography.SHA256.Create(); | ||
var builder = new StringBuilder(64); | ||
#pragma warning disable CA1850 // Prefer static 'System.Security.Cryptography.MD5.HashData' method over 'ComputeHash' | ||
foreach (var b in provider.ComputeHash(Encoding.UTF8.GetBytes(_value))) | ||
#pragma warning restore CA1850 | ||
{ | ||
builder.Append(b.ToString("x2")); | ||
} | ||
|
||
return builder.ToString(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#if NET6_0_OR_GREATER | ||
|
||
namespace Fluid.Utils; | ||
|
||
public class HexUtilities | ||
{ | ||
private static ReadOnlySpan<byte> HexChars => "0123456789abcdef"u8; | ||
|
||
public static string ToHexLower(byte[] hash) | ||
{ | ||
return string.Create(hash.Length * 2, hash, (span, hash) => | ||
{ | ||
var j = 0; | ||
var length = hash.Length; | ||
for (var i = 0; i < length; i++) | ||
{ | ||
var b = hash[i]; | ||
span[j++] = (char)HexChars[(b >> 4) & 0x0f]; | ||
span[j++] = (char)HexChars[b & 0x0f]; | ||
} | ||
}); | ||
} | ||
} | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters