Methods Summary |
---|
public java.lang.String | getValue(java.io.File file)Computes a value for a file content with the specified digest algorithm.
initMessageDigest();
String checksum = null;
try {
if (!file.canRead()) {
return null;
}
FileInputStream fis = null;
byte[] buf = new byte[readBufferSize];
try {
messageDigest.reset();
fis = new FileInputStream(file);
DigestInputStream dis = new DigestInputStream(fis,
messageDigest);
while (dis.read(buf, 0, readBufferSize) != -1) {
// do nothing
}
dis.close();
fis.close();
fis = null;
byte[] fileDigest = messageDigest.digest();
StringBuffer checksumSb = new StringBuffer();
for (int i = 0; i < fileDigest.length; i++) {
String hexStr = Integer.toHexString(0x00ff & fileDigest[i]);
if (hexStr.length() < 2) {
checksumSb.append("0");
}
checksumSb.append(hexStr);
}
checksum = checksumSb.toString();
} catch (Exception e) {
return null;
}
} catch (Exception e) {
return null;
}
return checksum;
|
public void | initMessageDigest()Initialize the security message digest.
if (messageDigest != null) {
return;
}
if ((provider != null) && !"".equals(provider) && !"null".equals(provider)) {
try {
messageDigest = MessageDigest.getInstance(algorithm, provider);
} catch (NoSuchAlgorithmException noalgo) {
throw new BuildException(noalgo);
} catch (NoSuchProviderException noprovider) {
throw new BuildException(noprovider);
}
} else {
try {
messageDigest = MessageDigest.getInstance(algorithm);
} catch (NoSuchAlgorithmException noalgo) {
throw new BuildException(noalgo);
}
}
|
public boolean | isValid()This algorithm supports only MD5 and SHA.
return "SHA".equalsIgnoreCase(algorithm) || "MD5".equalsIgnoreCase(algorithm);
|
public void | setAlgorithm(java.lang.String algorithm)Specifies the algorithm to be used to compute the checksum.
Defaults to "MD5". Other popular algorithms like "SHA" may be used as well.
// ----- Algorithm-Configuration -----
this.algorithm = algorithm;
|
public void | setProvider(java.lang.String provider)Sets the MessageDigest algorithm provider to be used
to calculate the checksum.
this.provider = provider;
|
public java.lang.String | toString()Override Object.toString().
StringBuffer buf = new StringBuffer();
buf.append("<DigestAlgorithm:");
buf.append("algorithm=").append(algorithm);
buf.append(";provider=").append(provider);
buf.append(">");
return buf.toString();
|