Calculate the message digest of a text using the SHA-1 algorithm in JAVA | Cryptography


HARDWARE AND SOFTWARE REQUIREMENT:
1. Intel based Desktop PC: - RAM of 512 MB
2. Notepad/Notepad ++ editor
3. Net beans / Eclipse

THEORY:
Ø  A hash function is a special mathematical function that converts a numerical input value of arbitrary length into another compressed numerical value(fixed length). A hash performs one-way encryption.Values returned by a hash function are called message digest or simply hash values. hash codes or simply hashes.
Ø  It is fundamental part of password security, in terms of database storage. A hash function responsible for changing a text password into a more complex set of characters by using more complex operations than a familiar mathematical operation such as multiplication.
Ø  Using hashes and hexadecimal formats can help those who are storing passwords on a database to confuse hackers.
Ø  The Secure Hash Algorithm 1 (SHA-1) is a cryptographic computer security algorithm.SHA-1 produces a 160-bit hash value from the inputted data (data that requires encryption).
Ø  SHA-1 is commonly used in cryptographic applications and environments where the requirements  for data integrity is high.  Some of the protocols that use SHA-1 include:
·     Transport Layer Security (TLS)
·     Secure Sockets Layer (SSL
·     Pretty Good Privacy (PGP)
·     Secure Shell (SSH)
·     Secure/Multipurpose Internet Mail Extensions (S/MIME)
·     Internet Protocol Security (IPSec)


SOURCE CODE:
import java.security.*;
public class SHA1 {
    public static void main(String[] a) {
        try {
            MessageDigest md = MessageDigest.getInstance("SHA1");
            System.out.println("Message digest object info:");
            System.out.println("          Algorithm=" + md.getAlgorithm());
            System.out.println("          Provider=" + md.getProvider());
            System.out.println("          ToString=" + md.toString());
            String input = "";
            md.update(input.getBytes());
            byte[] output = md.digest();
            System.out.println();
            System.out.println("SHA1(\"" + input + "\")=" + bytesToHex(output));
            input = "abc";
            md.update(input.getBytes());
            output = md.digest();
            System.out.println();
            System.out.println("SHA1(\"" + input + "\")=" + bytesToHex(output));
            input = "abcdefghijklmnopqrstuvwxyz";
            md.update(input.getBytes());
            output = md.digest();
            System.out.println();
            System.out.println("SHA1(\"" + input + "\")=" + bytesToHex(output));
            System.out.println();
        } catch (Exception e) {
            System.out.println("Exception:" + e);
        }
    }
    private static String bytesToHex(byte[] b) {
        char hexDigit[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
        StringBuffer buf = new StringBuffer();

        for (int j = 0; j < b.length; j++) {
            buf.append(hexDigit[(b[j] >> 4) & 0x0f]);
            buf.append(hexDigit[b[j] & 0x0f]);
        }
        /*
        for loop in optimized way

        for (byte aB : b) {
            buf.append(hexDigit[(aB >> 4) & 0x0f]);
            buf.append(hexDigit[aB & 0x0f]);
        }
        */
        return buf.toString();
    }
}

OUTPUT:

VIVA QUESTIONS:

1.  What is JAVA?
2. What is SHA-1 Algorithm?
3. What is java script?
4. What is Cryptography?
5. What is Encryption?
                  6. What is Decryption?

Comments

Post a Comment

Search related post on google