Pages

Sunday, September 27, 2015

Password hash with SHA-1

In cryptography, SHA-1 (Secure Hash Algorithm 1) is a cryptographic hash function designed by the United States National Security Agency and is a U.S. Federal Information Processing Standard published by the United States NIST. SHA-1 is a member of the Secure Hash Algorithm family. The four SHA algorithms are structured differently and are named SHA-0, SHA-1, SHA-2, and SHA-3. SHA1 is a one-way hash function and it computes a 160-bit message digest. SHA-1 often appears in security protocols for example, many HTTPS websites use RSA with SHA-1 to secure their connections. BitTorrent uses SHA-1 to verify downloads. Git and Mercurial use SHA-1 digests to identify commits.


SHA1("UiTM Jasin Melaka 2015. Fare Well Everyone!")
Hexadecimal: 0eb2acda4c34c9a13097a299f561c7b2d592e8bf

Even a small change in the message will, with overwhelming probability, result in a completely different hash due to the avalanche effect.

SHA1("UiTM Jasin Melaka 2015. FareWell Everyone!")
Hexadecimal: 543232d470af26e6b25644f820cbab88c214d1f1



 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import java.security.*; 
public class HashTextTest {

    public static void main(String[] args){
        System.out.println(sha1("UiTM Jasin Melaka 2015. Fare Well Everyone!"));
    }
     
    public static String sha1(String str){        
        StringBuffer shasum = new StringBuffer();
        try{
           MessageDigest mDigest = MessageDigest.getInstance("SHA1");
           byte[] result = mDigest.digest(str.getBytes());   
           for (int i = 0; i < result.length; i++) {
               shasum.append(Integer.toString((result[i] & 0xff) + 0x100, 16).substring(1));
           }
        }
        catch(NoSuchAlgorithmException e){
           e.printStackTrace();   
        }
        
        return shasum.toString();
    }
}



File SHA-1 Checksum
You can check files have the correct checksum or not..


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import java.io.*;
import java.security.*;
 
public class HashFileTest {
     
    public static void main(String[] args){
        boolean result = verifyChecksum("/Downloads/Iso/kali-linux-2.0-amd64.iso", "aaeb89a78f155377282f81a785aa1b38ee5f8ba0");
        System.out.println("File's checksum matches the expected one? " + result);
    
    } 
    public static boolean verifyChecksum(String file, String checksum)
    {
        String shaHash = "";
        try{
           System.out.println("Verifying File..'"+file+"'");
           System.out.println("Please Wait..");
           
           MessageDigest sha1 = MessageDigest.getInstance("SHA1");
           FileInputStream fis = new FileInputStream(file);
            
           byte[] data = new byte[1024];
           int read = 0; 
           while ((read = fis.read(data)) != -1) {
               sha1.update(data, 0, read);
           }
           byte[] Bytes = sha1.digest();
     
           StringBuffer shasum = new StringBuffer();
           for (int i = 0; i < Bytes.length; i++) {
              shasum.append(Integer.toString((Bytes[i] & 0xff) + 0x100, 16).substring(1));
           }
            
           shaHash = shasum.toString();
        }
        catch(IOException | NoSuchAlgorithmException e){
           e.printStackTrace();   
        }
        
        return shaHash.equals(checksum);
    }
 
 
}

No comments:

Post a Comment