At Secure Exchanges, we use a robust and efficient method to calculate the SHA-512 hash of files of all sizes. Our approach ensures data security and integrity while optimizing performance for large files.
Hash Calculation Method
Files of 1 GB or less :
- For files of size equal to or less than 1 GB, we use a standard method of calculating the SHA-512 hash in a single pass on the entire file.
Files larger than 1 GB :
- For files larger than 1 GB, we divide the file into 100 MB blocks.
- We calculate the SHA-512 hash of each block individually.
- We combine the hashes of each block into a single string.
- We calculate the final hash of this combined string.
Online Consultation
To offer our users maximum flexibility, anyone with a file can also consult our dedicated page to calculate its hash: Hash Calculator . This service is designed to provide a fast and reliable hash calculation, regardless of file size.
Starting June 24, 2024, we implemented this new hash calculation method to improve performance and the handling of large files. Prior to this date, hashing was calculated using the traditional method. This update reflects our ongoing commitment to optimizing our services to meet the needs of our users.
At Secure Exchanges, we are committed to providing secure and efficient solutions for all your file management needs.
Example Code
Here are some code examples in C#, JavaScript to calculate the SHA-512 hash of a file using our approach.
c#
using System;
using System.Collections.Generic;
using System.IO;
using System.Security.Cryptography;
using System.Text;
- public static class Hashing
- {
- public static string GetSHA512OfFile(string filePath)
- {
- if (File.Exists(filePath))
- {
- const int chunkSize = 100 * 1024 * 1024; // 100 MB
- const long largeFileTrigger = 1000 * 1024 * 1024; // 1 GB
- FileInfo fileInfo = new FileInfo(filePath);
- long fileSize = fileInfo.Length;
- if (fileSize <= largeFileTrigger)
- {
- // For files smaller than or equal to 1 GB, calculate hash directly
- using (var stream = new BufferedStream(File.OpenRead(filePath), chunkSize))
- {
- return GetSHA512OfStream(stream);
- }
- }
- else
- {
- // For files larger than 1 GB, use chunked approach
- List<string> blockHashes = new List<string>();
- using (var stream = new BufferedStream(File.OpenRead(filePath), chunkSize))
- {
- byte[] buffer = new byte[chunkSize];
- int bytesRead;
- while ((bytesRead = stream.Read(buffer, 0, chunkSize)) > 0)
- {
- using (SHA512 sha512 = SHA512.Create())
- {
- byte[] chunkHash = sha512.ComputeHash(buffer, 0, bytesRead);
- blockHashes.Add(BitConverter.ToString(chunkHash).Replace("-", "").ToLower());
- }
- }
- }
- // Combine block hashes and calculate final hash
- string combinedHashString = string.Join("", blockHashes);
- using (SHA512 sha512 = SHA512.Create())
- {
- byte[] finalHash = sha512.ComputeHash(Encoding.UTF8.GetBytes(combinedHashString));
- return BitConverter.ToString(finalHash).Replace("-", "").ToUpper();
- }
- }
- }
- else
- {
- return null;
- }
- }
- public static string GetSHA512OfStream(Stream stream)
- {
- using (var sha = SHA512.Create())
- {
- return GetHexaString(sha.ComputeHash(stream));
- }
- }
- private static string GetHexaString(byte[] hash)
- {
- if (hash != null)
- {
- StringBuilder sb = new StringBuilder();
- for (int i = 0; i < hash.Length; i++)
- {
- sb.Append(hash[i].ToString("X2"));
- }
- return sb.ToString();
- }
- return string.Empty;
}
}