Skip to content
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

Refine hashing usages #15621

Merged
merged 4 commits into from
Mar 29, 2024
Merged

Refine hashing usages #15621

merged 4 commits into from
Mar 29, 2024

Conversation

sebastienros
Copy link
Member

No description provided.

@@ -66,6 +66,7 @@
<PackageManagement Include="StackExchange.Redis" Version="2.7.33" />
<PackageManagement Include="StyleCop.Analyzers" Version="1.1.118" />
<PackageManagement Include="System.Linq.Async" Version="6.0.1" />
<PackageManagement Include="System.IO.Hashing" Version="8.0.0" />
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Contains new hashing algorithm, official Ms library.

@@ -165,8 +165,6 @@ private string GetHash(string queryStringTokenKey)

entry.SlidingExpiration = TimeSpan.FromHours(5);

using var hmac = new HMACSHA256(_hashKey);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removing the custom instantiation, it's recommended to use the static methods.


mediaTokenSettings.HashKey = new byte[64];
rng.GetBytes(mediaTokenSettings.HashKey);
mediaTokenSettings.HashKey = RandomNumberGenerator.GetBytes(DefaultMediaTokenKeySize);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use of static method

}

return sb.ToString();
var hash = new XxHash32();
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MD5/SHA1 should not be used for security reasons (I have been told by experts). xxHash32/64/128 are also much faster so the ones to use for non-cryptographic hashes.

return Convert.ToHexString(hash);
foreach (var part in parts)
{
hash.Append(Encoding.UTF8.GetBytes(part));
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No string concatenation necessary with this one.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: since you're changing the algorithm, I presume the result is only used internally and can change at any time.
If so, you may want to use the UTF-16 representation here, to avoid an intermediate string allocation.

Something like hash.Append(MemoryMarshal.AsBytes<char>(part));

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here is a benchmark with UTF8 vs Marshal.AsBytes, with and without the HEX encoding. Was wondering is the encoding would cancel out the benefits, still good. A little slower however, probably because of the extra hashing work.

| Method       | Mean      | Error     | StdDev   | Gen0   | Allocated |
|------------- |----------:|----------:|---------:|-------:|----------:|
| UTF8         |  77.37 ns | 39.491 ns | 2.165 ns | 0.0348 |     328 B |
| UTF8ToHex    |  82.21 ns | 51.756 ns | 2.837 ns | 0.0408 |     384 B |
| Marshal      | 104.60 ns | 25.270 ns | 1.385 ns | 0.0196 |     184 B |
| MarshalToHex |  91.35 ns |  7.423 ns | 0.407 ns | 0.0254 |     240 B |

@sebastienros
Copy link
Member Author

@kevinchalet if you have any question or comment

Copy link
Member

@kevinchalet kevinchalet left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool stuff happening here 😃

return Convert.ToHexString(hash);
foreach (var part in parts)
{
hash.Append(Encoding.UTF8.GetBytes(part));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: since you're changing the algorithm, I presume the result is only used internally and can change at any time.
If so, you may want to use the UTF-16 representation here, to avoid an intermediate string allocation.

Something like hash.Append(MemoryMarshal.AsBytes<char>(part));

@MikeAlhayek MikeAlhayek merged commit 861be5f into main Mar 29, 2024
5 checks passed
@MikeAlhayek MikeAlhayek deleted the sebros/hmac branch March 29, 2024 22:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants