Skip to content

Instantly share code, notes, and snippets.

@BluCobalt
Created January 13, 2021 07:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BluCobalt/67ba930a6173f900d4aa15ed081728b4 to your computer and use it in GitHub Desktop.
Save BluCobalt/67ba930a6173f900d4aa15ed081728b4 to your computer and use it in GitHub Desktop.
package dev.blucobalt;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Locale;
/**
* @author luke
* Date: 2021/01/12
*/
public class Sha1Test
{
public static void main(String[] args)
throws NoSuchAlgorithmException, IOException
{
File test = new File("C:\\Users\\Lukes\\IdeaProjects\\nativetest\\TestFile.txt");
System.out.println(getSha1(test));
}
// credit goes to https://gist.github.com/zeroleaf/6809843 for this
public static String getSha1(File path)
throws NoSuchAlgorithmException, IOException
{
MessageDigest md = MessageDigest.getInstance("SHA-1");
md.update(Files.readAllBytes(path.toPath()));
StringBuilder sb = new StringBuilder();
byte[] digest = md.digest();
for (byte b : digest)
{
int value = b & 0xFF;
if (value < 16)
{
sb.append("0");
}
sb.append(Integer.toHexString(value).toUpperCase(Locale.ROOT));
}
return sb.toString();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment