SHA1 online | SHA256 in JAVA | Secure password generator | Linux | Privacy Policy
SHA1 usage implementation in JAVA: sha1 of a text string and file's sha1 checksum verification.
SHA1 Java: sha1 of a text string
package main;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class HashTextTest {
/**
* @param args
* @throws NoSuchAlgorithmException
*/
public static void main(String[] args) throws NoSuchAlgorithmException {
System.out.println(sha1("test string to sha1"));
}
static String sha1(String input) throws NoSuchAlgorithmException {
MessageDigest mDigest = MessageDigest.getInstance("SHA1");
byte[] result = mDigest.digest(input.getBytes());
StringBuffer sb = new StringBuffer();
for (int i = 0; i < result.length; i++) {
sb.append(Integer.toString((result[i] & 0xff) + 0x100, 16).substring(1));
}
return sb.toString();
}
}
SHA1 Java: verifies file's SHA1 checksum
package main;
import java.io.FileInputStream;
import java.io.IOException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class HashFileTest {
public static void main(String[] args) throws NoSuchAlgorithmException, IOException {
boolean result = verifyChecksum("/home/eclipse-jee-indigo-SR2-linux-gtk-x86_64.tar.gz", "177750b65a21a9043105fd0820b85b58cf148ae4");
System.out.println("Does the file's checksum matches the expected one? " + result);
}
/**
* Verifies file's SHA1 checksum
* @param Filepath and name of a file that is to be verified
* @param testChecksum the expected checksum
* @return true if the expeceted SHA1 checksum matches the file's SHA1 checksum; false otherwise.
* @throws NoSuchAlgorithmException
* @throws IOException
*/
public static boolean verifyChecksum(String file, String testChecksum) throws NoSuchAlgorithmException, IOException
{
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[] hashBytes = sha1.digest();
StringBuffer sb = new StringBuffer();
for (int i = 0; i < hashBytes.length; i++) {
sb.append(Integer.toString((hashBytes[i] & 0xff) + 0x100, 16).substring(1));
}
String fileHash = sb.toString();
return fileHash.equals(testChecksum);
}
}