Files hash Calculation at Secure Exchanges
At Secure Exchanges, we use a robust and efficient method to calculate the SHA-512 hash of files of all sizes. Our approach ensures the security and integrity of the data while optimizing performance for large files.
Hash Calculation Method
Files of 1 GB or less:
- For files of 1 GB or less, we use a standard method to calculate the SHA-512 hash in a single pass over the entire file.
Files larger than 1 GB:
- For files larger than 1 GB, we divide the file into 100 MB chunks.
- We calculate the SHA-512 hash of each chunk individually.
- We combine the hashes of each chunk into a single string.
- We then calculate the final hash of this combined string.
Online Consultation
To provide our users with maximum flexibility, anyone with a file can also visit our dedicated page to calculate the hash of their file: Hash Calculator. This service is designed to provide a quick and reliable hash calculation, regardless of the file size.
As of June 24, 2024, we have implemented this new hash calculation method to improve performance and manage large files more efficiently. Before this date, the hash was calculated in the traditional way. 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 high-performance solutions for all your file management needs.
Code sample
Here are examples of code in C# and 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;
}
}
Javascript
- var window = self;
- var document = {};
- onmessage = async function (args) {
- var obj = args.data;
- let reader = new FileReader();
- var hash = {};
- var chunkSize = 100 * 1024 * 1024; // 100MB chunk size for large files
- var largeFileTrigger = 1000 * 1024 * 1024; // 1GB file will use block approach
- var isLargeFile = obj.File.size > largeFileTrigger;
- const chunksQuantity = Math.ceil(obj.File.size / chunkSize);
- const chunksQueue = new Array(chunksQuantity).fill().map((_, index) => index).reverse();
- let blockHashes = [];
- reader.onload = async function (evt) {
- if (isLargeFile) {
- let blockHash = await digestMessage(evt.currentTarget.result);
- blockHashes.push(blockHash);
- } else {
- blockHashes.push(evt.currentTarget.result);
- }
- readNext();
- }
- let readNext = async function () {
- if (chunksQueue.length > 0) {
- const chunkId = chunksQueue.pop();
- const sentSize = chunkId * chunkSize;
- const chunk = obj.File.slice(sentSize, sentSize + chunkSize);
- reader.readAsArrayBuffer(chunk);
- } else {
- reader.abort();
- var hexHash = null;
- if (isLargeFile) {
- let combinedHashBuffer = new TextEncoder().encode(blockHashes.join(''));
- hexHash = await digestMessage(combinedHashBuffer);
- } else {
- hexHash = await digestMessage(blockHashes[0]);
- }
- hash.SHA512 = hexHash.toUpperCase();
- postMessage({ Hash: hash, File: obj.File, ID: obj.ID, ReturnObject: obj.ReturnObject });
- }
- }
- readNext();
- }
- // Function to digest message and return hex string
- async function digestMessage(buffer) {
- const hashBuffer = await crypto.subtle.digest('SHA-512', buffer);
- const hashArray = Array.from(new Uint8Array(hashBuffer));
- const hashHex = hashArray.map(b => b.toString(16).padStart(2, '0')).join('');
- return hashHex;
- }
Related Articles
My Secure Exchanges tab is no longer present in my Outlook ribbon.
If your Secure Exchanges tab is no longer present in your Outlook ribbon but was previously : Please follow these steps : 1. Go to Outlook, under File / Account Information / Manage COM Add-ins. 2. If the OSecureExchanges files is there, check if the ...
What are the configuration settings for Secure Exchanges on Gmail?
To configure the Secure Exchanges settings on Gmail, you need to click on the Secure Exchanges extension and then on Settings. You will therefore see the following window appear with various options: You can thus define the options of the emails by ...
What are the Secure Exchanges configuration settings on Outlook?
Various Secure Exchanges settings can be configured on Outlook: 1. Messages Settings : On this pane, you can define the options for sending and receiving your secure emails, the language of your connector and communication, the security and opening ...
Installing the new Secure Exchanges SSL certificate - Administrator
If you have another procedure, you can certainly use it. The script we provide is simply a tool to help streamline the process. The important thing is to ensure that our new certificate is properly added to TrustedPublisher for each Secure Exchanges ...
How to keep encrypted files?
In order for SESAR to keep your encrypted files on disk, you must generate a new public/private key. Keep the private key in a safe place and share your public key with the Secure Exchanges team. (to use SESAR restore you must enter your private key ...