1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
| /*
* Hasher.java
*/
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
/**
*
* @author Romain Absil, Yann D'Isanto
*/
public class Hasher {
private MessageDigest algorithm;
/**
* SHA-1 algorithm.
**/
public static final String SHA1 = "SHA-1";
/**
* SHA-256 algorithm.
**/
public static final String SHA256 = "SHA-256";
/**
* SHA-384 algorithm.
**/
public static final String SHA384 = "SHA-384";
/**
* SHA-512 algorithm.
**/
public static final String SHA512 = "SHA-512";
/**
* MD2 algorithm.
**/
public static final String MD2 = "MD2";
/**
* MD5 algorithm.
**/
public static final String MD5 = "MD5";
public Hasher() {
try {
this.algorithm = MessageDigest.getInstance(MD5);
} catch (NoSuchAlgorithmException ex) {
ex.printStackTrace();
}
}
/**
* Create a Hasher that will uses the specified algorithm.
* @param algorithm the algorithm
*/
public Hasher(MessageDigest algorithm) {
this.algorithm = algorithm;
}
/**
* Create a Hasher that will uses the specified algorithm.
* @param algorithm the algorithm
*/
public Hasher(String algorithm) throws NoSuchAlgorithmException {
this.algorithm = MessageDigest.getInstance(algorithm);
}
/**
* Set the algorithm.
* @param algorithm the algorithm
*/
public void setAlgorithm(MessageDigest algorithm) {
this.algorithm = algorithm;
}
/**
* Set the algorithm.
* @param algorithm the algorithm
*/
public void setAlgorithm(String algorithm) throws NoSuchAlgorithmException {
setAlgorithm(MessageDigest.getInstance(algorithm));
}
/**
* Returns the algorithm.
* @return the algorithm
*/
public MessageDigest getAlgorithm() {
return algorithm;
}
/**
* Hashes the file denoted by the specified location
* @param pathname file location
* @return the hash as a bytes array
*/
public byte[] hash(String pathname) throws FileNotFoundException, IOException {
return hash(new File(pathname));
}
/**
* Hashes the specified file.
* @param file the file to hash
* @return the hash as a bytes array
*/
public byte[] hash(File file) throws FileNotFoundException, IOException {
FileInputStream fis = new FileInputStream(file);
byte[] hash = null;
try {
hash = hash(fis);
} finally {
fis.close();
}
return hash;
}
/**
* Hashes the specified input stream.
* @param is the input stream to hash
* @return the hash as a bytes array
*/
public byte[] hash(InputStream is) throws IOException {
BufferedInputStream bis = new BufferedInputStream(is);
algorithm.reset();
byte[] data = new byte[2048];
int nbRead = 0;
while((nbRead = bis.read(data)) > 0) {
algorithm.update(data, 0, nbRead);
}
bis = null;
return algorithm.digest();
}
/**
* Hashes the file denoted by the specified location
* @param pathname file location
* @return the hash as a String
*/
public String hashToString(String pathname) throws FileNotFoundException, IOException {
return hashToString(hash(pathname));
}
/**
* Hashes the specified file.
* @param file the file to hash
* @return the hash as a String
*/
public String hashToString(File file) throws FileNotFoundException, IOException {
return hashToString(hash(file));
}
/**
* Hashes the specified input stream.
* @param is the input stream to hash
* @return the hash as a String
*/
public String hashToString(InputStream is) throws IOException {
return hashToString(hash(is));
}
/**
* Returns the specified bytes array hash as a String
* @param hash the hash as a bytes array
* @return the hash as a String
*/
public String hashToString(byte[] hash) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < hash.length; i++) {
int v = hash[i] & 0xFF;
if(v < 16) {
sb.append("0");
}
sb.append(Integer.toString(v, 16));
}
return sb.toString();
}
} |