String file_path = “your file from your file system “;
System.out.println(“File to upload ” + file_path);
FileInputStream fis = null;
try {
File file = new File(file_path);
if (!file.exists() || file.length() == 0) {
Utils.stampaLogE(“Conversion Hash SHA-256 . Bad input !!! ( File not found / File empty)…”);
throw new RuntimeException(“Conversion Hash SHA-256 . Bad input !!! ( File not found / File empty )…”);
}
fis = new FileInputStream(file);
byte[] fileBytes = new byte[(int) file.length()];
MessageDigest md = MessageDigest.getInstance(“sha-256”);
int length;
while ((length = fis.read(fileBytes)) != -1) {
md.update(fileBytes, 0, length);
}
byte[] raw = md.digest();
StringBuilder sb = new StringBuilder();
String base64 = Base64.encode(raw);
return base64;
} catch (Exception e) {
e.printStackTrace();
throw e ;
} finally {
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Get hash-256 from file into base64