bisogno MD5 hash per un in System.Drawing.Image memoriabisogno MD5 hash per un in System.Drawing.Image memoria
risposta
Ecco un frammento di base. Vedi anche il commento di @JaredReisinger per alcune domande.
using System.Security.Cryptography;
using System.Text;
using System.Drawing.Imaging;
// ...
// get the bytes from the image
byte[] bytes = null;
using(MemoryStream ms = new MemoryStream())
{
image.Save(ms,ImageFormat.Gif); // gif for example
bytes = ms.ToArray();
}
// hash the bytes
MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
byte[] hash = md5.ComputeHash(bytes);
// make a hex string of the hash for display or whatever
StringBuilder sb = new StringBuilder();
foreach (byte b in hash)
{
sb.Append(b.ToString("x2").ToLower());
}
Non dovrebbe leggere "foreach (byte b in hash)'? –
oops, grazie :) –
Grazie per aver risposto alla domanda e non aver speso tutte le tue energie preoccupandoti del motivo per cui ho bisogno di sapere. Vorrei che più persone su SO fossero più come te. Inoltre, puoi evitare il ciclo StringBuilder e foreach. Vedi la mia risposta. –
Un semplice esempio, basato su sample in MSDN; si noti che questo hash dipende dalla rappresentazione interna dell'immagine e non corrisponderà all'hash creato da un file.
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Security.Cryptography;
using System.Text;
class Program
{
static string getMd5Hash(byte[] buffer)
{
MD5 md5Hasher = MD5.Create();
byte[] data = md5Hasher.ComputeHash(buffer);
StringBuilder sBuilder = new StringBuilder();
for (int i = 0; i < data.Length; i++)
{
sBuilder.Append(data[i].ToString("x2"));
}
return sBuilder.ToString();
}
static byte[] imageToByteArray(Image image)
{
MemoryStream ms = new MemoryStream();
image.Save(ms, ImageFormat.Bmp);
return ms.ToArray();
}
static void Main(string[] args)
{
Image image = Image.FromFile(@"C:\tmp\Jellyfish.jpg");
byte[] buffer = imageToByteArray(image);
string md5 = getMd5Hash(buffer);
}
}
Per essere in grado di utilizzare la classe MD5
è necessario aggiungere un riferimento a System.Security
.
A seconda dell'utilizzo dell'hash, è necessario considerare il fatto che MD5 non è più all'avanguardia e che sono disponibili migliori funzioni di hash se è necessario un hash forte.
Devo dire che questo è ben messo insieme .... con ciò che i libri di testo insegnano anche agli esaminatori. – IbrarMumtaz
Grazie agli altri utenti che hanno risposto. Ecco cosa ho finito per fare:
MemoryStream ms = new MemoryStream()
image.Save(ms, ImageFormat.Png);
byte[] imgBytes = ms.ToArray();
MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
byte[] hash = md5.ComputeHash(imgBytes);
string imageMD5 = BitConverter.ToString(hash).Replace("-", "").ToLower();
ms.Dispose();
- 1. MD5 hash in silverlight
- 2. Padding in MD5 Hash Algorithm
- 3. MD5 Funzione hash in Excel?
- 4. MD5 Hash From String
- 5. Java calcolare hash MD5
- 6. Python hash MD5 confronto
- 7. Combinazione di valori hash MD5
- 8. System.Drawing.Image per lo streaming C#
- 9. Perl Regular Expressions per abbinare un hash MD5?
- 10. Tornando sbagliato hash MD5 in C
- 11. Perché "hash ('md5', 'string')" più veloce di "md5 ('string')"?
- 12. Converti System.Drawing.Image in System.Windows.Controls.Image?
- 13. Best practice per la memorizzazione efficiente hash MD5 in mysql
- 14. Come ottenere un hash MD5 da una stringa in Golang?
- 15. Come posso ottenere un hash MD5 in ColdFusion?
- 16. Come creare un hash MD5 di una stringa in C?
- 17. Come creare un hash MD5 di una stringa in RubyMotion
- 18. C# Generazione più veloce di hash MD5
- 19. Combina gli hash MD5 di più file
- 20. MD5 Funzione hash in excel senza utilizzare VBA
- 21. La funzione restituisce un diverso hash MD5 ogni volta
- 22. calcola e visualizza il file MD5 Hash in un'etichetta
- 23. MySQL: aggiornamento di una tabella completa, inserimento di un hash MD5, per ogni riga uno specifico
- 24. Come ottenere la dimensione del file di "System.Drawing.Image"
- 25. L'API WinRT gestita include un modo per calcolare un hash MD5?
- 26. Come creare ImageBrush da System.Drawing.Image in WPF?
- 27. Crea le tue collisioni MD5
- 28. Usa SHA-512 e salta all'hash una password hash MD5?
- 29. Perché il pip fallisce con un hash md5 non valido per il pacchetto?
- 30. Laravel 4 Auth - usa md5 invece di Hash :: make() integrato
Dove sei bloccato? È relativamente semplice. –
Il titolo * non * dice tutto ... perché hai bisogno di un hash? Deve essere identico all'hash di un file su disco equivalente? Presumibilmente, a un certo punto, paragonerai l'hash a qualcosa ... – JaredReisinger
@Jared - http://uploadscreenshot.com/api-documentation#variables Ora, sai cosa so: ho un System.Drawing .Immagine in memoria e necessario inviare un hash MD5 dell'oggetto Immagine a tale API. –