The file upload methods require a checksum (which is a SHA-256 hash of the file’s content) to ensure that the binary content is transferred correctly. Java SDK contains the utility class, Checksum
, which calculates the checksum for a file.
You can use the following code sample to calculate checksum:
import java.security.*;
...
try (FileInputStream fis = new FileInputStream(file)) {
MessageDigest md;
try {
md = MessageDigest.getInstance("SHA-256");
}
catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
byte[] mdbytes;
{
byte[] buf = new byte[1024];
int nread;
while ((nread = fis.read(buf)) != -1)
md.update(buf, 0, nread);
mdbytes = md.digest();
}
StringBuilder rv = new StringBuilder();
for (byte mdbyte : mdbytes)
rv.append(Integer.toString((mdbyte & 0xff) + 0x100, 16).substring(1));
return rv.toString();
}
Copyright © 2015-2017, Verizon and/or its Licensors. All rights reserved.