FileDocCategorySizeDatePackage
DigestAlgorithm.javaAPI DocApache Ant 1.706140Wed Dec 13 06:16:22 GMT 2006org.apache.tools.ant.types.selectors.modifiedselector

DigestAlgorithm

public class DigestAlgorithm extends Object implements Algorithm
Computes a 'hashvalue' for the content of file using java.security.MessageDigest. Use of this algorithm doesn't require any additional nested s. Supported s are:
namevaluesdescriptionrequired
algorithm.algorithm MD5 | SHA (default provider) name of the algorithm the provider should use no, defaults to MD5
algorithm.provider name of the provider to use no, defaults to null
version
2004-07-08
since
Ant 1.6

Fields Summary
private String
algorithm
MessageDigest algorithm to be used.
private String
provider
MessageDigest Algorithm provider
private MessageDigest
messageDigest
Message Digest instance
private int
readBufferSize
Size of the read buffer to use.
Constructors Summary
Methods Summary
public java.lang.StringgetValue(java.io.File file)
Computes a value for a file content with the specified digest algorithm.

param
file File object for which the value should be evaluated.
return
The value for that file

        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 voidinitMessageDigest()
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 booleanisValid()
This algorithm supports only MD5 and SHA.

return
true if all is ok, otherwise false.

        return "SHA".equalsIgnoreCase(algorithm) || "MD5".equalsIgnoreCase(algorithm);
    
public voidsetAlgorithm(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.

param
algorithm the digest algorithm to use



    // -----  Algorithm-Configuration  -----


                                       
        
        this.algorithm = algorithm;
    
public voidsetProvider(java.lang.String provider)
Sets the MessageDigest algorithm provider to be used to calculate the checksum.

param
provider provider to use

        this.provider = provider;
    
public java.lang.StringtoString()
Override Object.toString().

return
some information about this algorithm.

        StringBuffer buf = new StringBuffer();
        buf.append("<DigestAlgorithm:");
        buf.append("algorithm=").append(algorithm);
        buf.append(";provider=").append(provider);
        buf.append(">");
        return buf.toString();