Created
December 18, 2023 20:00
-
-
Save icarofreire/ec9b91c09d348074c30ad970e5e50e54 to your computer and use it in GitHub Desktop.
This file contains 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 deepsea.utilities; | |
import java.io.File; | |
import java.io.FileInputStream; | |
import java.io.InputStream; | |
import java.security.MessageDigest; | |
import java.io.IOException; | |
import java.nio.charset.StandardCharsets; | |
import java.security.DigestInputStream; | |
import java.security.NoSuchAlgorithmException; | |
/** | |
* classe para produzir hashs para arquivos ou string; | |
*/ | |
public class SHash { | |
private final String hashType = "SHA-256"; | |
// private final String hashType = "SHA-512"; | |
// private final String hashType = "SHA3-256"; | |
public String getFileHash(File file) { | |
String str = ""; | |
try { | |
// str = getHash(file); | |
str = checksum(file.getAbsolutePath()); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
return str; | |
} | |
public String getFileHash(String nameFile) { | |
String str = ""; | |
try { | |
// str = getHash(new File(nameFile)); | |
str = checksum(nameFile); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
return str; | |
} | |
public synchronized String getStringHash(String pText) { | |
byte[] input = pText.getBytes(StandardCharsets.UTF_8); | |
String algorithm = hashType; | |
MessageDigest md; | |
try { | |
md = MessageDigest.getInstance(algorithm); | |
} catch (NoSuchAlgorithmException e) { | |
throw new IllegalArgumentException(e); | |
} | |
byte[] result = md.digest(input); | |
// return result; | |
return toHexString(result); | |
} | |
private synchronized String getHash(File file) throws Exception { | |
InputStream fis = new FileInputStream(file); | |
byte buffer[] = new byte[1024]; | |
MessageDigest md5 = MessageDigest.getInstance(hashType); | |
for (int numRead = 0; (numRead = fis.read(buffer)) > 0;) { | |
md5.update(buffer, 0, numRead); | |
} | |
fis.close(); | |
return toHexString(md5.digest()); | |
} | |
/**\/ similar to getHash(File file); */ | |
private synchronized String checksum(String filePath) { | |
String algorithm = hashType; | |
MessageDigest md; | |
try { | |
md = MessageDigest.getInstance(algorithm); | |
} catch (NoSuchAlgorithmException e) { | |
throw new IllegalArgumentException(e); | |
} | |
try (InputStream is = new FileInputStream(filePath); | |
DigestInputStream dis = new DigestInputStream(is, md)) { | |
while (dis.read() != -1) ; //empty loop to clear the data | |
md = dis.getMessageDigest(); | |
} catch (IOException e) { | |
throw new IllegalArgumentException(e); | |
} | |
// return md.digest(); | |
return toHexString(md.digest()); | |
} | |
private String getHash(String nameFile) throws Exception { | |
return getHash(new File(nameFile)); | |
} | |
private String toHexString(byte b[]) { | |
StringBuilder sb = new StringBuilder(); | |
for (int i = 0; i < b.length; i++) { | |
sb.append(Integer.toHexString(b[i] & 0xFF)); | |
} | |
return sb.toString(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment