Come utilizzare lo SHA1CryptoServiceProvider()
su un file per creare un checksum SHA1 del file?Come si esegue un checksum file SHA1 in C#?
risposta
Con il metodo ComputeHash. Vedi qui:
frammento di esempio:
using(var cryptoProvider = new SHA1CryptoServiceProvider())
{
string hash = BitConverter
.ToString(cryptoProvider.ComputeHash(buffer));
//do something with hash
}
Dove buffer è il contenuto del file.
using (FileStream fs = new FileStream(@"C:\file\location", FileMode.Open))
using (BufferedStream bs = new BufferedStream(fs))
{
using (SHA1Managed sha1 = new SHA1Managed())
{
byte[] hash = sha1.ComputeHash(bs);
StringBuilder formatted = new StringBuilder(2 * hash.Length);
foreach (byte b in hash)
{
formatted.AppendFormat("{0:X2}", b);
}
}
}
formatted
contiene la rappresentazione di stringa dell'hash SHA-1. Inoltre, utilizzando un FileStream
anziché un buffer di byte, lo ComputeHash
calcola l'hash in blocchi, quindi non è necessario caricare l'intero file in una volta sola, il che è utile per file di grandi dimensioni.
È necessario utilizzare un 'StringBuilder' invece di generare 20 stringhe nel processo di creazione della stringa hash. –
'FileStream' è' IDisposable' e dovrebbe essere usato anche in un blocco 'using'. –
La capacità iniziale di StringBuilder deve essere il doppio del numero di byte nell'hash –
Se stai già leggendo il file come uno stream, la tecnica seguente calcola l'hash mentre lo leggi. L'unica avvertenza è che è necessario consumare l'intero flusso.
class Program
{
static void Main(string[] args)
{
String sourceFileName = "C:\\test.txt";
Byte[] shaHash;
//Use Sha1Managed if you really want sha1
using (var shaForStream = new SHA256Managed())
using (Stream sourceFileStream = File.Open(sourceFileName, FileMode.Open))
using (Stream sourceStream = new CryptoStream(sourceFileStream, shaForStream, CryptoStreamMode.Read))
{
//Do something with the sourceStream
//NOTE You need to read all the bytes, otherwise you'll get an exception ({"Hash must be finalized before the hash value is retrieved."})
while(sourceStream.ReadByte() != -1);
shaHash = shaForStream.Hash;
}
Console.WriteLine(Convert.ToBase64String(shaHash));
}
}
+1 per 'CryptoStream'. potrebbe essere utile nel caso in cui tu voglia leggere un file da qualche parte (ad es .: da una richiesta http), scriverlo in qualche posto (es .: su disco) e allo stesso tempo calcolare l'hash. – tigrou
In un test su un file di grandi dimensioni, questo codice ha eseguito ordini di grandezza peggiori rispetto alle soluzioni 'ComputeHash'. Forse è la lettura "alla volta di ReadByte"? –
@MichaelKropat interessante. Buono a sapersi. 10 volte, 100 volte più lento? –
Inoltre si può provare:
FileStream fop = File.OpenRead(@"C:\test.bin");
string chksum = BitConverter.ToString(System.Security.Cryptography.SHA1.Create().ComputeHash(fop));
- 1. Come calcolare sha256 checksum file in Go
- 2. Come si esegue un file utilizzando Java?
- 3. Python - Come si esegue un file .py?
- 4. Come si esegue un no-op in C/C++?
- 5. Come si esegue un file Ruby in un ambiente Rails?
- 6. Come si esegue exec() un processo in background in C?
- 7. Come si esegue un programma C++ in XCode 4?
- 8. Come si esegue un file .bat da PHP?
- 9. Come si esegue uno script utilizzando un file BAT?
- 10. Come si crea un hash SHA1 in ruby?
- 11. Come si esegue un file js usando gli script npm?
- 12. come eseguirò un hash SHA1 su un file?
- 13. Estrarre l'hash SHA1 da un file torrent
- 14. Come si esegue l'aritmetica in un makefile?
- 15. Come utilizzare l'hashing SHA1 nella programmazione C
- 16. come si esegue un file php da ssh?
- 17. Algoritmo base64 SHA1 in javascript e C#
- 18. Come si esegue un programma Python?
- 19. Come si esegue un file bat in background da un altro file bat?
- 20. SHA1 hashing in SQLite: come?
- 21. Come calcolare il checksum
- 22. Come decifrare sha1 in php?
- 23. Codifica SHA1 in Haskell
- 24. checksum in rotazione nell'algoritmo rsync
- 25. Come si esegue TDD in Xcode?
- 26. Come si esegue un interprete Python in Emacs?
- 27. Come si esegue un comando sudo in Emacs?
- 28. Come si esegue Vim in Windows?
- 29. Come si esegue un ordinamento personalizzato complesso in Ruby?
- 30. Come si esegue un server Mercurial?
+1 per una grande punta sull'utilizzo del 'BitConverter' per generare un hex-stringa in un colpo solo. –
SHA1CryptoServiceProvider deve essere racchiuso in un blocco 'using' – Mike737
' BitConverter' separa i byte con un trattino AA-F0-CC a differenza della soluzione di @ mgbowen. Può o non può essere quello che si desidera. –