What Is SHA-256 and How To Calculate SHA-256 In Different Programming Languages?

Hash algorithms are the heart of the cryptography and security. SHA-256 is a Secure Hash Algorithm which will generate an output hash value in 256 bit. SHA-256 is designed by the National Security Agency  (NSA). SHA-256 is one of the cryptographic hash functions. SHA-256 has also named a one-way function where the generated hash value cannot be reversed theoretically. This makes SHA-256 very useful for password validation, challenge hash authentication, anti-tamper, digital signatures, X.509, SSL/TLS certificates, etc.

SHA-256 Cryptographic Features

As SHA-256 is a cryptographic function it provides some features for cryptographic and security operations.

  • SHA-256 generated or calculated data will be One-Way. This means it can not be reversed back to the original data with a function. There is an exception where brute-force or rainbow tables can be used.
  • SHA-256 is a member of the SHA-2 cryptographic hash functions family which is created by NSA.
  • SHA-256 is an anti-tamper feature which means calculating the hash of the same data at different times will provide the same hash value. If there is a single bit change in the data the hash value will be very different. So we can decide the tamper with this feature.
  • SHA-256 can be used in challenge handshake authentication because the password is not transmitted in cleartext.
  • SHA-256 can be used in digital signatures in order to check the given data validity and integrity with authenticity.

Generate SHA-256 For Given Text In Python

In Python programming language SHA-256 is provided by the hashlib module. We will use sha256() function. In order to provide a string to calculate the hash, we should encode the string with the encode() function like below. We will print the calculated hash value in hexadecimal format with the hexdigest() function.

import hashlib
Generate SHA-256 For Given Text In Python
Generate SHA-256 For Given Text In Python

We can see that we have calculated two data hash values. These data is very similar where the first one is mysecretpassword and the second one is mysecretpasswrt where only the last letter is different. This changes the calculated hash value completely.

LEARN MORE  HMAC (Hach-based Message Authentication Code) Tutorial

Generate SHA-256 For Given Text In Go

Go is a new generation programming language where SHA-256 is supported. We can use the sha256 module Sum256() function by providing the data.

s := "mysecretpassword"

sha256 := sha256.Sum256([]byte(s))

fmt.Printf("%x\n", sha256)

Generate SHA-256 For Given Text In PHP

PHP is a web and server-side scripting and programming language. We can use hash() function by providing the data we can to calculate its hash.  We will also provide the hash algorithm we want to use which is sha256 in this case.

echo $hashvalue;
Generate SHA-256 For Given Text In PHP
Generate SHA-256 For Given Text In PHP

Generate SHA-256 For Given Text In JavaScript

JavaScript programming language does not provide built-in SHA-256 algorithm support. So we have to find some external or third-party libraries in order to calculate SHA-256 in JavaScript scripts. The following JavaScript library can be used to calculate SHA-256.


var hashvalue=sha256('mysecretpassword')

Generate SHA-256 For Given Text In PowerShell

PowerShell provides the HashAlgorithms library in order to implement SHA256. We will use Create() function and provide the hash algorithm name which is SHA256.

$StringBuilder = New-Object System.Text.StringBuilder





Generate SHA-256 For Given Text In Java

Java programming language provides the SHA-256 for a long time. We will use MessageDigest library in order to create an SHA-256 hash object. Then we will use digest() function in order to calculate the hash of the given text. Calculated hash value will be stored into the byte array named encodedhash.

MessageDigest digest = MessageDigest.getInstance("SHA-256");

byte[] encodedhash = digest.digest("mysecretpassword".getBytes(StandardCharsets.UTF_8));

Leave a Comment