[PTRun]Fix Hash Collision in Image Cache (#31503)

This commit is contained in:
gokcekantarci
2024-03-14 18:05:43 +03:00
committed by GitHub
parent 44f3abb6a9
commit 16257d80f6
3 changed files with 26 additions and 8 deletions

View File

@@ -8,6 +8,6 @@ namespace Wox.Infrastructure.Image
{ {
public interface IImageHashGenerator public interface IImageHashGenerator
{ {
string GetHashFromImage(ImageSource image); string GetHashFromImage(ImageSource image, string filePath);
} }
} }

View File

@@ -15,7 +15,7 @@ namespace Wox.Infrastructure.Image
public class ImageHashGenerator : IImageHashGenerator public class ImageHashGenerator : IImageHashGenerator
{ {
[System.Diagnostics.CodeAnalysis.SuppressMessage("Security", "CA5350:Do Not Use Weak Cryptographic Algorithms", Justification = "Level of protection needed for the image data does not require a security guarantee")] [System.Diagnostics.CodeAnalysis.SuppressMessage("Security", "CA5350:Do Not Use Weak Cryptographic Algorithms", Justification = "Level of protection needed for the image data does not require a security guarantee")]
public string GetHashFromImage(ImageSource image) public string GetHashFromImage(ImageSource image, string filePath)
{ {
if (!(image is BitmapSource bitmapSource)) if (!(image is BitmapSource bitmapSource))
{ {
@@ -26,13 +26,12 @@ namespace Wox.Infrastructure.Image
{ {
using (var outStream = new MemoryStream()) using (var outStream = new MemoryStream())
{ {
// PngBitmapEncoder enc2 = new PngBitmapEncoder(); // Dynamically selecting the encoder based on the file extension to preserve the original image format characteristics as much as possible.
// enc2.Frames.Add(BitmapFrame.Create(tt)); BitmapEncoder encoder = GetEncoderByFileExtension(filePath);
var enc = new JpegBitmapEncoder();
var bitmapFrame = BitmapFrame.Create(bitmapSource); var bitmapFrame = BitmapFrame.Create(bitmapSource);
bitmapFrame.Freeze(); bitmapFrame.Freeze();
enc.Frames.Add(bitmapFrame); encoder.Frames.Add(bitmapFrame);
enc.Save(outStream); encoder.Save(outStream);
var byteArray = outStream.GetBuffer(); var byteArray = outStream.GetBuffer();
return Convert.ToBase64String(SHA1.HashData(byteArray)); return Convert.ToBase64String(SHA1.HashData(byteArray));
} }
@@ -43,5 +42,24 @@ namespace Wox.Infrastructure.Image
return null; return null;
} }
} }
public static BitmapEncoder GetEncoderByFileExtension(string filePath)
{
string fileExtension = Path.GetExtension(filePath).ToLowerInvariant();
switch (fileExtension)
{
case ".png":
return new PngBitmapEncoder();
case ".jpg":
case ".jpeg":
return new JpegBitmapEncoder();
case ".bmp":
return new BmpBitmapEncoder();
default:
// Default to PNG if the format is unknown or unsupported because PNG is a lossless compression format
return new PngBitmapEncoder();
}
}
} }
} }

View File

@@ -266,7 +266,7 @@ namespace Wox.Infrastructure.Image
if (imageResult.ImageType != ImageType.Error && imageResult.ImageType != ImageType.Cache) if (imageResult.ImageType != ImageType.Error && imageResult.ImageType != ImageType.Cache)
{ {
// we need to get image hash // we need to get image hash
string hash = _enableImageHash ? _hashGenerator.GetHashFromImage(img) : null; string hash = _enableImageHash ? _hashGenerator.GetHashFromImage(img, path) : null;
if (hash != null) if (hash != null)
{ {