-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
Refine hashing usages #15621
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -165,8 +165,6 @@ private string GetHash(string queryStringTokenKey) | |
|
||
entry.SlidingExpiration = TimeSpan.FromHours(5); | ||
|
||
using var hmac = new HMACSHA256(_hashKey); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Removing the custom instantiation, it's recommended to use the static methods. |
||
|
||
// 'queryStringTokenKey' also contains prefix. | ||
var chars = queryStringTokenKey.AsSpan(TokenCacheKeyPrefix.Length); | ||
|
||
|
@@ -177,11 +175,11 @@ private string GetHash(string queryStringTokenKey) | |
: new byte[requiredLength]; | ||
|
||
// 256 for SHA-256, fits in stack nicely. | ||
Span<byte> hashBytes = stackalloc byte[hmac.HashSize]; | ||
Span<byte> hashBytes = stackalloc byte[HMACSHA256.HashSizeInBytes]; | ||
|
||
var stringBytesLength = Encoding.UTF8.GetBytes(chars, stringBytes); | ||
|
||
hmac.TryComputeHash(stringBytes[..stringBytesLength], hashBytes, out var hashBytesLength); | ||
HMACSHA256.TryHashData(_hashKey, stringBytes[..stringBytesLength], hashBytes, out var hashBytesLength); | ||
|
||
entry.Value = result = Convert.ToBase64String(hashBytes[..hashBytesLength]); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,8 @@ namespace OrchardCore.Media.Processing | |
/// </summary> | ||
public class MediaTokenSettingsUpdater : FeatureEventHandler, IModularTenantEvents | ||
{ | ||
private const int DefaultMediaTokenKeySize = 64; | ||
|
||
private readonly ISiteService _siteService; | ||
private readonly ShellSettings _shellSettings; | ||
|
||
|
@@ -38,10 +40,7 @@ public async Task ActivatedAsync() | |
{ | ||
var siteSettings = await _siteService.LoadSiteSettingsAsync(); | ||
|
||
var rng = RandomNumberGenerator.Create(); | ||
|
||
mediaTokenSettings.HashKey = new byte[64]; | ||
rng.GetBytes(mediaTokenSettings.HashKey); | ||
mediaTokenSettings.HashKey = RandomNumberGenerator.GetBytes(DefaultMediaTokenKeySize); | ||
siteSettings.Put(mediaTokenSettings); | ||
|
||
await _siteService.UpdateSiteSettingsAsync(siteSettings); | ||
|
@@ -65,10 +64,7 @@ private async Task SetMediaTokenSettingsAsync(IFeatureInfo feature) | |
var siteSettings = await _siteService.LoadSiteSettingsAsync(); | ||
var mediaTokenSettings = siteSettings.As<MediaTokenSettings>(); | ||
|
||
var rng = RandomNumberGenerator.Create(); | ||
|
||
mediaTokenSettings.HashKey = new byte[64]; | ||
rng.GetBytes(mediaTokenSettings.HashKey); | ||
mediaTokenSettings.HashKey = RandomNumberGenerator.GetBytes(DefaultMediaTokenKeySize); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use of static method |
||
siteSettings.Put(mediaTokenSettings); | ||
|
||
await _siteService.UpdateSiteSettingsAsync(siteSettings); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,7 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO.Hashing; | ||
using System.Linq; | ||
using System.Security.Cryptography; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.Extensions.Logging; | ||
|
@@ -126,20 +125,9 @@ private string GetContentItemFolder(ContentItem contentItem) | |
private async Task<string> GetFileHashAsync(string filePath) | ||
{ | ||
using var fs = await _fileStore.GetFileStreamAsync(filePath); | ||
using HashAlgorithm hashAlgorithm = MD5.Create(); | ||
var hash = hashAlgorithm.ComputeHash(fs); | ||
return ByteArrayToHexString(hash); | ||
} | ||
|
||
public static string ByteArrayToHexString(byte[] bytes) | ||
{ | ||
var sb = new StringBuilder(); | ||
foreach (var b in bytes) | ||
{ | ||
sb.Append(b.ToString("x2").ToLower()); | ||
} | ||
|
||
return sb.ToString(); | ||
var hash = new XxHash32(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
await hash.AppendAsync(fs); | ||
return Convert.ToHexString(hash.GetCurrentHash()).ToLowerInvariant(); | ||
} | ||
|
||
private static string GetFileExtension(string path) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.IO.Hashing; | ||
using System.Linq; | ||
using System.Net.Http.Headers; | ||
using System.Security.Cryptography; | ||
using System.Text; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
@@ -200,9 +200,14 @@ private static FileStream CreateTemporaryFile(string tempPath, long size) | |
|
||
private static string CalculateHash(params string[] parts) | ||
{ | ||
var hash = SHA256.HashData(Encoding.UTF8.GetBytes(string.Join(string.Empty, parts))); | ||
var hash = new XxHash64(); | ||
|
||
return Convert.ToHexString(hash); | ||
foreach (var part in parts) | ||
{ | ||
hash.Append(Encoding.UTF8.GetBytes(part)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No string concatenation necessary with this one. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. Something like There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
|
||
} | ||
|
||
return Convert.ToHexString(hash.GetCurrentHash()); | ||
} | ||
|
||
private sealed class ChunkedFormFile : IFormFile, IDisposable | ||
|
There was a problem hiding this comment.
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.