mirror of
https://github.com/microsoft/PowerToys.git
synced 2025-12-16 19:57:57 +01:00
[PTRun]Fix Hash Collision in Image Cache (#31503)
This commit is contained in:
@@ -8,6 +8,6 @@ namespace Wox.Infrastructure.Image
|
|||||||
{
|
{
|
||||||
public interface IImageHashGenerator
|
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
|
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();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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)
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user