Friday, 6 September 2013

Create an own method for encode any Text into SHA1 in java


What is SHA ?   

The SHA hash functions are a set of cryptographic hash functions designed by the National Security Agency (NSA) and published by the NIST as a U.S. Federal Information Processing Standard. 
SHA stands for "Secure Hash Algorithm". The three SHA algorithms are structured differently and are distinguished as SHA-0, SHA-1, and SHA-2. The SHA-2 family uses an identical algorithm with a variable digest size which is distinguished as SHA-224, SHA-256, SHA-384, and SHA-512.


Below this code We are using MessageDigest class, first setting it to use SHA-1 algorithm, than feeding it with source data and getting byte array with hash value. To convert this array to hex-string we using our own method convertToHex.

Here is "SHA1DC" class, which single public static method SHA1 returns hexadecimal string representation for SHA1 hash of input argument.




-------------------------------------SHA1DCUSE.java----------------------------------------

package SHA1;

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStreamReader; 
import java.io.UnsupportedEncodingException; 
import java.security.NoSuchAlgorithmException; 
public class SHA1DCUSE { 
    public static void main(String[] args) throws IOException { 
        BufferedReader userInput = new BufferedReader (new InputStreamReader(System.in));
        System.out.println("Enter string:");
        String rawString = userInput.readLine();
        try { 
            System.out.println("SHA1 Encode: " + SHA1DC.SHA1(rawString));
        } catch (NoSuchAlgorithmException e) { 
            // TODO Auto-generated catch block 
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) { 
            // TODO Auto-generated catch block 
            e.printStackTrace();
        } 
    } 

----------------------------------------SHA1DC.java-------------------------------------------

package SHA1;

import java.io.UnsupportedEncodingException; 
import java.security.MessageDigest; 
import java.security.NoSuchAlgorithmException; 
public class SHA1DC { 
    private static String convertToHex(byte[] data) { 
        StringBuffer buf = new StringBuffer();
        for (int i = 0; i < data.length; i++) { 
            int halfbyte = (data[i] >>> 4) & 0x0F;
            int two_halfs = 0;
            do { 
                if ((0 <= halfbyte) && (halfbyte <= 9)) 
                    buf.append((char) ('0' + halfbyte));
                else 
                    buf.append((char) ('a' + (halfbyte - 10)));
                halfbyte = data[i] & 0x0F;
            } while(two_halfs++ < 1);
        } 
        return buf.toString();
    } 

    public static String SHA1(String text) 
    throws NoSuchAlgorithmException, UnsupportedEncodingException  { 
    MessageDigest md;
    md = MessageDigest.getInstance("SHA-1");
    byte[] sha1hash = new byte[40];
    md.update(text.getBytes("iso-8859-1"), 0, text.length());
    sha1hash = md.digest();
    return convertToHex(sha1hash);
    } 


No comments:

Post a Comment