School of Computer Science and Engineering Fall Semester 2020-21
School of Computer Science and Engineering Fall Semester 2020-21
School of Computer Science and Engineering Fall Semester 2020-21
Semester 2020-21
Name: Jaanaavi Wasade
Registration Number:18BCE0743
Course Code: CSE3501
Course Name: Information Security Analysis and audit
Slot: L21 +L22
Faculty: Vimala Devi K
Lab Assessment -3
Securing form authentication credentials
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
class GFG {
public static String encryptThisString(String input)
{
try {
MessageDigest md = MessageDigest.getInstance("SHA-1");
byte[] messageDigest = md.digest(input.getBytes());
BigInteger no = new BigInteger(1, messageDigest);
String hashtext = no.toString(16);
while (hashtext.length() < 32) {
hashtext = "0" + hashtext;
}
return hashtext;
}
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 = "Jaanaavi Wasade";
System.out.println("\n" + s1 + " : " + encryptThisString(s1));
String s2 = "hello world";
System.out.println("\n" + s2 + " : " + encryptThisString(s2));
}
}
In Cryptography, SHA is cryptographic hash function which takes input as 20 Bytes and
rendered the hash value in hexadecimal number, 40 digits long approx.
import java.math.BigInteger;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
class GFG {
public static byte[] getSHA(String input) throws NoSuchAlgorithmException
{
MessageDigest md = MessageDigest.getInstance("SHA-256");
return md.digest(input.getBytes(StandardCharsets.UTF_8));
}
public static String toHexString(byte[] hash)
{
BigInteger number = new BigInteger(1, hash);
StringBuilder hexString = new StringBuilder(number.toString(16));
while (hexString.length() < 32)
{
hexString.insert(0, '0');
}
return hexString.toString();
}
public static void main(String args[])
{
try
{
System.out.println("HashCode Generated by SHA-256 for:");
String s1 = "Jaanaavi Wasade";
System.out.println("\n" + s1 + " : " + toHexString(getSHA(s1)));
String s2 = "Information Security Analysis and Audit";
System.out.println("\n" + s2 + " : " + toHexString(getSHA(s2)));
}
catch (NoSuchAlgorithmException e) {
System.out.println("Exception thrown for incorrect algorithm: " + e);
}
}
}