You are on page 1of 4

Vidya Vikas Education Trust’s

Universal College of Engineering, Kaman Road, Vasai-401208


Accredited by B+ Grade by NAAC

Experiment no 6

Aim:To implement SHA algorithm.

Theory:

Introduction:
The "Secure Hash Algorithm" is widely known as SHA. The Secure Hash Algorithm is a
cryptographic hash function. A cryptographic hash function is an algorithm that randomly takes
data as input without a specific reason and produces an output of text in a coded form called
"Hash value". The coded text will be stored instead of the password that is used to verify the
user, and this enciphered text is used to verify the user instead of the password. The SHA is also
a non-reversible function similar to other cryptographic hash functions. SHA can be used to
create a text signature by taking input of 20 bytes long maximum. The Secure Hash Function
returns a 40-digit hexadecimal hash value as its output. Even the smallest changes in the input
can make a big difference in the coded text output. The phenomenon is called the avalanche
effect. The avalanche effect helps in securing the user data from attackers as it makes the
decrypting of code difficult.

SHA-1 or Secure Hash Algorithm 1 is a cryptographic hash function which takes an input and
produces a 160-bit (20-byte) hash value. This hash value is known as a message digest. This
message digest is usually then rendered as a hexadecimal number which is 40 digits long. It is a
U.S. Federal Information Processing Standard and was designed by the United States National
Security Agency. SHA-1 is now considered insecure since 2005. Major tech giants browsers like
Microsoft, Google, Apple and Mozilla have stopped accepting SHA-1 SSL certificates by 2017.
To calculate cryptographic hashing value in Java, MessageDigest Class is used, under the
package java.security.

Algorithm:
Step 1 − Append padding bits − The original message is padded and its duration is congruent to
448 modulo 512. Padding is continually inserted although the message already has the desired
length. Padding includes a single 1 followed by the essential number of 0 bits.
Step 2 − Append length − A 64-bit block considered as an unsigned 64-bit integer (most
essential byte first), and defining the length of the original message (before padding in step 1),
is added to the message. The complete message's length is a multiple of 512.
Step 3 −Initialize the buffer − The buffer includes five (5) registers of 32 bits each indicated by
A, B, C, D, and E. This 160-bit buffer can be used to influence temporary and final outcomes of
Vidya Vikas Education Trust’s
Universal College of Engineering, Kaman Road, Vasai-401208
Accredited by B+ Grade by NAAC

the compression function. These five registers are initialized to the following 32-bit integers (in
hexadecimal notation).

A = 67 45 23 01
B = ef cd ab 89
C = 98 ba dc fe
D = 10 32 54 76
E = c3 d2 e1 f0
The registers A, B, C, and D are actually the same as the four registers used in the MD5
algorithm. But in SHA-1, these values are saved in big-endian format, which defines that the
most essential byte of the word is located in the low-address byte position. Therefore the
initialization values (in hexadecimal notation) occurs as follows −
word A = 67 45 23 01
word B = ef cd ab 89
word C = 98 ba dc fe
word D = 10 32 54 76
word E = c3 d2 e1 f0
Step 4 − Process message in 512-bit blocks − The compression function is divided into 20
sequential steps including four rounds of processing where each round is made up of 20 steps.
The four rounds are structurally same as one another with the only difference that each round
need a different Boolean function, which it can define as f1, f2, f3, f4 and one of four multiple
additive constants Kt (0 ≤t ≤79) which is based on the step under consideration.
Step 5 − Output − After processing the final 512-bit message block t (considering that the
message is divided into t 512-bit blocks), it can obtain a 160-bit message digest.
Program:
// Java program to calculate SHA-1 hash value

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class GFG {


Vidya Vikas Education Trust’s
Universal College of Engineering, Kaman Road, Vasai-401208
Accredited by B+ Grade by NAAC

public static String encryptThisString(String input)


{
try {
// getInstance() method is called with algorithm SHA-1
MessageDigest md = MessageDigest.getInstance("SHA-1");

// digest() method is called


// to calculate message digest of the input string
// returned as array of byte
byte[] messageDigest = md.digest(input.getBytes());

// Convert byte array into signum representation


BigInteger no = new BigInteger(1, messageDigest);

// Convert message digest into hex value


String hashtext = no.toString(16);

// Add preceding 0s to make it 32 bit


while (hashtext.length() < 32) {
hashtext = "0" + hashtext;
}

// return the HashText


return hashtext;
}

// For specifying wrong message digest algorithms


catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
}

// Driver code
public static void main(String args[]) throws
NoSuchAlgorithmException
{
Vidya Vikas Education Trust’s
Universal College of Engineering, Kaman Road, Vasai-401208
Accredited by B+ Grade by NAAC

System.out.println("HashCode Generated by SHA-1 for: ");

String s1 = "GeeksForGeeks";
System.out.println("\n" + s1 + " : " + encryptThisString(s1));

String s2 = "hello world";


System.out.println("\n" + s2 + " : " + encryptThisString(s2));
}
}

Output:

Conclusion:

Hence we have successfully implemented SHA algorithm using java.

You might also like