Skip to content

Instantly share code, notes, and snippets.

@vcsjones
Created February 2, 2022 15:42
Show Gist options
  • Save vcsjones/78a4d099902ba5e172a6e374281638ce to your computer and use it in GitHub Desktop.
Save vcsjones/78a4d099902ba5e172a6e374281638ce to your computer and use it in GitHub Desktop.
HMAC benchmarks
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<RootNamespace>benchmark_scratch</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.13.1" />
</ItemGroup>
</Project>
using System.Security.Cryptography;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
BenchmarkRunner.Run<HmacBenchMark>();
[MemoryDiagnoser]
public class HmacBenchMark
{
private readonly byte[] _hmacKey = RandomNumberGenerator.GetBytes(32);
private readonly byte[] _hmacData = RandomNumberGenerator.GetBytes(32);
[Benchmark]
public byte[] HmacCreateDispose()
{
using (HMACSHA1 hmac = new HMACSHA1(_hmacKey))
{
return hmac.ComputeHash(_hmacData);
}
}
[Benchmark]
public byte[] HmacOneShot()
{
return HMACSHA1.HashData(_hmacKey, _hmacData);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment