# Name: Dil Prasad Kuwor Roll no.

: 02/064 Subject: Cryptography Assignment : Hash Function Implementation

Date: 11/24/2010

HASH FUNCTION Implementation:
Java Code: import java.util.*; class HasfCode{ public static void main (String[] args) { String plaintext1,plaintext2="",index="0123456789ABCDEF",HashCode=""; Scanner in=new Scanner(System.in); System.out.println("Enter the message:"); plaintext1=in.nextLine(); for(int i=plaintext1.length()-1;i>=0;i--) { plaintext2=plaintext2+plaintext1.charAt(i); } ArrayList<Integer> list1 = new ArrayList<Integer>(); ArrayList<Integer> list2 = new ArrayList<Integer>(); ArrayList<Integer> xor = new ArrayList<Integer>(); list1=findbit(plaintext1); list2=findbit(plaintext2); String bin = "",bin1=""; if(list1.size()!=list2.size()) { if(list1.size()<list2.size()) { for(int i=list1.size();i<list2.size();i++) { list1.add(0); } } if(list1.size()>list2.size()) { for(int i=list2.size();i<list1.size();i++) { list2.add(0); } } } for(int i=list1.size()-1; i>=0; i--) { bin = bin + String.valueOf(list1.get(i)); bin1 = bin1 + String.valueOf(list2.get(i)); if(list1.get(i)!=list2.get(i)) { xor.add(1); } 1

else{ xor.add(0); } } System.out.println(bin); System.out.println(bin1); for(Integer o : xor)// FOR EACH { System.out.printf("%d",o); } int c=0,j=0; while(j<xor.size()) { double power=3,k=0; for(int i=0;i<4;i++) { k=k+(double )xor.get(c)*Math.pow(2,power); power=power-1; c++; } int l=(int )k; l=l%15; HashCode =HashCode + index.charAt(l); j=c; } System.out.println("\nThe hash code of the above message is"+HashCode); } public static String findIndex(String test) { String letter[] = {"0","1","2","3","4","5","6","7","8","9","A", "B", "C", "D", "E", "F"}; String k; for(int j=0; j<letter.length; j++) { if(test.equalsIgnoreCase(letter[j])) { k=String.valueOf(j); return k; } } return null; } public static ArrayList<Integer> findbit(String plainText) { ArrayList<Integer> list= new ArrayList<Integer>(); for(int j=0;j<plainText.length();j++) { char c = plainText.charAt(j); String val=String.valueOf(c); String index1=findIndex(val); int p=Integer.parseInt(index1); for(int i=3;i>=0;i--) { 2

list.add(p%2); p=p/2; } } return list; } } Output:

The End

3