mirror of
https://github.com/microsoft/PowerToys.git
synced 2024-11-27 23:19:13 +08:00
[PTRun]Fix Hash Collision in Image Cache (#31503)
This commit is contained in:
parent
44f3abb6a9
commit
16257d80f6
@ -8,6 +8,6 @@ namespace Wox.Infrastructure.Image
|
||||
{
|
||||
public interface IImageHashGenerator
|
||||
{
|
||||
string GetHashFromImage(ImageSource image);
|
||||
string GetHashFromImage(ImageSource image, string filePath);
|
||||
}
|
||||
}
|
||||
|
@ -15,7 +15,7 @@ namespace Wox.Infrastructure.Image
|
||||
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")]
|
||||
public string GetHashFromImage(ImageSource image)
|
||||
public string GetHashFromImage(ImageSource image, string filePath)
|
||||
{
|
||||
if (!(image is BitmapSource bitmapSource))
|
||||
{
|
||||
@ -26,13 +26,12 @@ namespace Wox.Infrastructure.Image
|
||||
{
|
||||
using (var outStream = new MemoryStream())
|
||||
{
|
||||
// PngBitmapEncoder enc2 = new PngBitmapEncoder();
|
||||
// enc2.Frames.Add(BitmapFrame.Create(tt));
|
||||
var enc = new JpegBitmapEncoder();
|
||||
// Dynamically selecting the encoder based on the file extension to preserve the original image format characteristics as much as possible.
|
||||
BitmapEncoder encoder = GetEncoderByFileExtension(filePath);
|
||||
var bitmapFrame = BitmapFrame.Create(bitmapSource);
|
||||
bitmapFrame.Freeze();
|
||||
enc.Frames.Add(bitmapFrame);
|
||||
enc.Save(outStream);
|
||||
encoder.Frames.Add(bitmapFrame);
|
||||
encoder.Save(outStream);
|
||||
var byteArray = outStream.GetBuffer();
|
||||
return Convert.ToBase64String(SHA1.HashData(byteArray));
|
||||
}
|
||||
@ -43,5 +42,24 @@ namespace Wox.Infrastructure.Image
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -266,7 +266,7 @@ namespace Wox.Infrastructure.Image
|
||||
if (imageResult.ImageType != ImageType.Error && imageResult.ImageType != ImageType.Cache)
|
||||
{
|
||||
// we need to get image hash
|
||||
string hash = _enableImageHash ? _hashGenerator.GetHashFromImage(img) : null;
|
||||
string hash = _enableImageHash ? _hashGenerator.GetHashFromImage(img, path) : null;
|
||||
|
||||
if (hash != null)
|
||||
{
|
||||
|
Loading…
Reference in New Issue
Block a user