How To Calculate The Hash (SHA-256) Of A File?
Data security is a crucial issue, especially in a digital world where hacking and privacy breaches are increasingly prevalent. To ensure the protection of computer files, one of the most effective means is the use of hashing algorithms. These have the feature of transforming any type of data into a unique digital fingerprint that preserves its integrity. In this article, we will explain how this fingerprint is calculated for a file. For this practical case, we will use the SHA-256, the hashing algorithm used by the Bitcoin protocol. But before that, let’s explore the fundamentals of hashing in general, and SHA-256 in particular.
What is hashing?
Hashing is a cryptographic process that allows converting any type of data into a unique fixed-size numeric value called “hash”. Hashes are generated using hashing algorithms that are both deterministic and irreversible. This means that the same input data will always produce the same hash result, while it is impossible to determine the initial value from its hash.
Hashing algorithms ensure that data is not altered. Because, in reality, even the slightest modification of the input data will result in the generation of a completely different hash.
Hash functions are very common in the field of computer security. They serve among others to:
- store passwords;
- secure personal data;
- verify file authenticity;
- preserve the integrity of transactions on the blockchain.
Depending on their specificities, there are several of them. Among the most commonly used, we can mention SHA-1, SHA-3, and SHA-256, which all belong to the family of Secure Hash Algorithms.
Introduction to SHA-256
The SHA-256 algorithm is a cryptographic hash function that generates a 256-bit digital fingerprint from variable-length data. This fingerprint is represented in the form of a 64-character hexadecimal hash.
SHA-256 was created in 2001 by the NSA (National Security Agency), an American intelligence agency. It is now widely used in the blockchain industry, particularly by Bitcoin, because it offers a high level of security.
What makes this algorithm unique is its complexity. Indeed, to generate a hash, SHA-256 performs a series of sophisticated binary calculations using various compression functions such as additions, substitutions, and rotations. It also relies on the construction of Merkle trees, thus providing enhanced integrity assurance of the data. All of this makes SHA-256 a very robust algorithm.
How to calculate the hash of a file using the SHA-256 algorithm?
To calculate the hash of a file using the SHA-256 algorithm, there are several tools available depending on your operating system.
On Windows
Using the command prompt
To calculate the SHA-256 hash of a file on Windows, you can use command line tools like CertUtil, PowerShell, or File Checksum Integrity Verifier (FCiV).
Once the tool you wish to use is installed on your computer, open it by pressing the Windows and R keys simultaneously.
Then, in the bottom left window that appears, type “cmd” and confirm. Then, determine the full path to the file to process. Finally, in the command prompt, type the corresponding command referring to the table below:
Tools | Commands |
PowerShell | “Get-FileHash -Algorithm SHA256 -Path [full path of the file]” |
CertUtil | “certutil -hashfile [full path of the file] SHA256” |
File Checksum Integrity Verifier (FCiV) | “fciv.exe -sha256 [full path of the file]” |
Using the graphical interface
There are also GUI applications that allow you to calculate the hash of a file in a few clicks. Some of them include: HashTab, MultiHasher, QuickHash, HashTools, HashCheck, HashCalc, and HashMyFiles.
The generic procedure when using this type of solution is as follows:
- Download and install the hashing software that best suits your needs;
- Open it and set the option to use the SHA-256 algorithm as the hashing method;
- Select the file you wish to process;
- Click the indicated button to start the conversion;
- Wait a moment for the software to display the hash corresponding to the file.
On Linux
On Linux, there are a few utilities to calculate the hash of a file in SHA-256. You can for example use sha256sum, OpenSSL, or Hashdeep. To do this, open the terminal.
Then, navigate to the directory containing the file to hash. Use the “cd” command if you do not know the full path to the file.
Make sure you have the necessary rights and permissions to access this directory. Finally, type the corresponding command.
Tools | Commands |
sha256sum | “sha256sum [file name]” |
OpenSSL | “openssl dgst -sha256 [file name]” |
Hashdeep | “hashdeep -c sha256 [file name]” |
On Mac OS
If you are on Mac OS, you can use shasum. To begin, open the terminal and navigate to the folder containing the file. Then, type the command below:
“shasum -a 256 [file name]”.
How to verify the authenticity of a file?
When you have doubts about the integrity of a file, you can verify its hash using the checksum SHA-256. To do this, first, calculate the digital fingerprint of the original file by applying the SHA-256 hashing algorithm to its content. Then, you must calculate the hash of the file to be verified in the same way and compare it with the hash value found earlier.
If the two hash values are identical, then the file has not been modified, and its integrity is intact. Conversely, if the hash values are different, this indicates that the file has been altered or corrupted, and its integrity should be questioned.
In conclusion
Hashing is a simple, reliable, and accessible method for protecting data against any attempt at corruption or hacking. More specifically, the level of security offered by the SHA-256 algorithm makes it a more interesting choice compared to other hashing techniques. Moreover, due to the many available tools, whether on Windows, Linux, or Mac OS, users are able to easily calculate the hash of their files to prevent them from being modified. That said, they can also use them in case of doubt to determine if files have been altered.
Maximize your Cointribune experience with our "Read to Earn" program! For every article you read, earn points and access exclusive rewards. Sign up now and start earning benefits.
L'équipe éditoriale de Cointribune unit ses voix pour s’exprimer sur des thématiques propres aux cryptomonnaies, à l'investissement, au métaverse et aux NFT, tout en s’efforçant de répondre au mieux à vos interrogations.
The views, thoughts, and opinions expressed in this article belong solely to the author, and should not be taken as investment advice. Do your own research before taking any investment decisions.