Generating an SHA-256 Hash From Command Line


As technology becomes increasingly integrated into our daily lives, security concerns continue to grow. It's more important than ever to ensure that our data is secure and protected from cybercriminals. One way to help protect data is to use a hash function. SHA-256 is one such hash function that can be used to create a unique digital fingerprint for data. In this article, we'll explore how to generate an SHA-256 hash from command line.

What is SHA-256?

SHA-256 is a cryptographic hash function that takes input data and produces a fixed-length output, or hash, of 256 bits. function is designed to be one-way, meaning that it is easy to compute hash of some input data, but it is virtually impossible to determine input data from hash. This property makes SHA-256 useful for ensuring integrity of data and detecting any changes made to it.

SHA-256 is widely used in security applications, including digital signatures, password storage, and message authentication. It is also used in cryptocurrencies, such as Bitcoin, to secure transactions and ensure that no one can tamper with transaction data.

Generating an SHA-256 hash from command line

Generating an SHA-256 hash from command line is a straightforward process. You can use a variety of tools and programming languages to generate a hash, but we'll focus on using command line to keep things simple.

Here are steps to generate an SHA-256 hash from command line −

  • Open a terminal or command prompt.

  • Navigate to directory where file you want to hash is located. For example, if file is on your desktop, you would navigate to desktop directory.

  • Enter command to generate SHA-256 hash. command syntax will depend on tool or programming language you're using. We'll cover a few examples below.

Let's take a closer look at some examples of how to generate an SHA-256 hash from command line.

Using OpenSSL

OpenSSL is a popular open-source tool that provides a suite of cryptographic functions, including SHA-256. To use OpenSSL to generate an SHA-256 hash, follow these steps −

  • Open a terminal or command prompt.

  • Navigate to directory where file you want to hash is located.

  • Enter following command −

openssl sha256 filename

Replace "filename" with name of file you want to hash. For example, if file is called "document.txt," command would be −

openssl sha256 document.txt

Press enter. SHA-256 hash of file will be displayed in terminal window.

Using sha256sum command

Many Linux distributions come with sha256sum command pre-installed. This command calculates SHA-256 hash of a file and compares it to a known hash value to verify its integrity. Here's how to use sha256sum command −

  • Open a terminal.

  • Navigate to directory where file you want to hash is located.

  • Enter following command:sha256sum filename

Replace "filename" with name of file you want to hash. For example, if file is called "document.txt," command would be −

sha256sum document.txt

Press enter. SHA-256 hash of file will be displayed in terminal window.

Using PowerShell

If you're using a Windows operating system, you can use PowerShell to generate an SHA-256 hash. Here's how −

  • Open PowerShell.

  • Navigate to directory where file you want to hash is located.

  • Enter following command −

Get-FileHash filename -Algorithm SHA256 | Select-Object Hash

Replace "filename" with name of file you want to hash. For example, if file is called "document.txt," command would be −

Get-FileHash document.txt -Algorithm SHA256 | Select-Object Hash

Press enter. SHA-256 hash of file will be displayed in PowerShell window.

Verifying integrity of a file with SHA-256

One of main use cases for SHA-256 is verifying integrity of a file. By comparing hash of a file with a known hash value, you can determine whether file has been tampered with or corrupted.

To verify integrity of a file with SHA-256, follow these steps −

  • Obtain known SHA-256 hash value of file. This value can be obtained from a trusted source, such as website where file was downloaded from or author of file.

  • Generate SHA-256 hash of file using one of methods described above.

  • Compare generated hash with known hash value. If two values match, file has not been tampered with or corrupted.

In addition to methods described above, there are several other tools and programming languages you can use to generate an SHA-256 hash. Here are a few additional examples −

Using Python

Python is a popular programming language that has built-in support for SHA-256. Here's how to generate an SHA-256 hash of a file using Python −

  • Open a terminal or command prompt.

  • Navigate to directory where file you want to hash is located.

  • Enter following command −

python -c "import hashlib;print(hashlib.sha256(open('filename','rb').read()).hexdigest())"

Replace "filename" with name of file you want to hash. For example, if file is called "document.txt," command would be −

python -c "import hashlib;print(hashlib.sha256(open('document.txt','rb').read()).hexdigest())"

Press enter. SHA-256 hash of file will be displayed in terminal window.

Using sha256 command in macOS

If you're using a macOS operating system, you can use built-in sha256 command to generate an SHA-256 hash. Here's how −

  • Open a terminal.

  • Navigate to directory where file you want to hash is located.

  • Enter following command −

shasum -a 256 filename

Replace "filename" with name of file you want to hash. For example, if file is called "document.txt," command would be −

shasum -a 256 document.txt

Press enter. SHA-256 hash of file will be displayed in terminal window.

Using CertUtil command in Windows

Windows also has a built-in command for generating SHA-256 hashes called CertUtil. Here's how to use CertUtil to generate an SHA-256 hash −

  • Open a command prompt.

  • Navigate to directory where file you want to hash is located.

  • Enter following command −

CertUtil -hashfile filename SHA256

Replace "filename" with name of file you want to hash. For example, if file is called "document.txt," command would be −

CertUtil -hashfile document.txt SHA256

Press enter. SHA-256 hash of file will be displayed in command prompt window.

Conclusion

SHA-256 is a powerful hash function that can be used to generate a unique digital fingerprint for data. By using command line, you can easily generate an SHA-256 hash of a file and verify its integrity. examples provided in this article should give you a good starting point for working with SHA-256 on command line.

Remember that while SHA-256 is a powerful tool, it's important to use it in conjunction with other security measures to protect your data. Always be sure to use strong passwords, keep your software up to date, and practice safe browsing habits to help keep your data secure.

Updated on: 14-Mar-2023

10K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements