-
Notifications
You must be signed in to change notification settings - Fork 862
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[v4] CryptoUtil
refactoring opportunities.
#3386
Comments
Definitely are areas if performance improvements now that we are moving up the minimum .NET version. Do you know if the System.IO.Hashing is any faster then the one the SDK uses? MD5 is challenging for .NET Framework in a FIPS environment. Like you said we added it 9 years ago because you can't instantiate the MD5 objects in .NET Framework in a FIPS environment. We are not abandoning .NET Framework as part of V4 so don't see us switching away. |
I ran some benchmarks for the CRC algorithms. For CRC32 the Results
Codeusing System.IO.Hashing;
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using Aws.Crt.Checksums;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Configs;
using BenchmarkDotNet.Running;
BenchmarkRunner.Run<Crc32Benchmark>();
[GroupBenchmarksBy(BenchmarkLogicalGroupRule.ByCategory)]
public class Crc32Benchmark
{
[Params(16, 1024, 1 << 20, 1 << 26)]
public int Size { get; set; }
private byte[] _data = null!;
[GlobalSetup]
public void Setup()
{
_data = new byte[Size];
Random.Shared.NextBytes(_data);
}
[Benchmark]
[BenchmarkCategory("Crc32")]
public uint Crc32SystemIoHashing()
{
return Crc32.HashToUInt32(_data);
}
[Benchmark(Baseline = true)]
[BenchmarkCategory("Crc32")]
public uint Crc32AwsCrt()
{
return Crc.crc32(_data);
}
[Benchmark]
[BenchmarkCategory("Crc32c")]
public uint Crc32cBitOperations()
{
return Crc32cBitOperations(_data);
}
[Benchmark(Baseline = true)]
[BenchmarkCategory("Crc32c")]
public uint Crc32cAwsCrt()
{
return Crc.crc32c(_data);
}
private static uint Crc32cBitOperations(ReadOnlySpan<byte> buffer)
{
ref byte start = ref MemoryMarshal.GetReference(buffer);
ref byte end = ref Unsafe.Add(ref start, buffer.Length);
uint crc = 0;
while (Unsafe.ByteOffset(ref start, ref end) >= sizeof(ulong))
{
crc = BitOperations.Crc32C(crc, Unsafe.ReadUnaligned<ulong>(ref start));
start = ref Unsafe.Add(ref start, sizeof(ulong));
}
while (Unsafe.ByteOffset(ref start, ref end) >= sizeof(uint))
{
crc = BitOperations.Crc32C(crc, Unsafe.ReadUnaligned<uint>(ref start));
start = ref Unsafe.Add(ref start, sizeof(uint));
}
while (Unsafe.IsAddressLessThan(ref start, ref end))
{
crc = BitOperations.Crc32C(crc, start);
start = ref Unsafe.Add(ref start, 1);
}
return ~crc;
}
} |
The system's MD5 implementation becomes increasingly faster on larger buffer sizes. Considering the FIPS implications, we could try using it first, and add fall back to the managed implementation if it throws.
|
Describe the feature
I read the code in the
CryptoUtil.cs
file and noticed that there are some things that could be improved.Use Case
Cleaning-up the codebase and improving maintainability.
Proposed Solution
System.IO.Hashing
to implement CRC hashing.AWSSDK.Extensions.System.IO.Hashing
package that provides an alternative implementation.CryptoUtil
could even be removed from the public API but that would be a needless breaking change.Other Information
No response
Acknowledgements
AWS .NET SDK and/or Package version used
v4
Targeted .NET Platform
All
Operating System and version
All
The text was updated successfully, but these errors were encountered: