0% found this document useful (0 votes)
63 views2 pages

SHA1

This Java code provides a method to encrypt strings using the SHA-1 hashing algorithm. The encryptThisString method takes a string as input, uses the SHA-1 MessageDigest to generate a byte array hash, converts it to a hex string, and returns the 32-character encrypted string. The main method calls encryptThisString on two sample strings and prints the results.

Uploaded by

Digital Nikhil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
63 views2 pages

SHA1

This Java code provides a method to encrypt strings using the SHA-1 hashing algorithm. The encryptThisString method takes a string as input, uses the SHA-1 MessageDigest to generate a byte array hash, converts it to a hex string, and returns the 32-character encrypted string. The main method calls encryptThisString on two sample strings and prints the results.

Uploaded by

Digital Nikhil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

import java.math.

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

public class HmacSha1Signature {


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
{

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

String s1 = "I like harry potter";


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

String s2 = "hello digital world";


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

You might also like