Skip to main content

practical 6

Aim : Java program to calculate SHA-1 hash value

code


// Java program to calculate SHA-1 hash value

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

public class sha1 {
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);
}
}


public static void main(String args[]) throws NoSuchAlgorithmException {

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

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

String s2 = "hello world";
System.out.println("\n" + s2 + " : " + encryptThisString(s2));
}
}

Save File

Save the file name as sha1.java

Compile


javac sha1.java
java sha1