Created
January 13, 2021 07:14
-
-
Save BluCobalt/67ba930a6173f900d4aa15ed081728b4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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