/**
*
* Copyright (c) 2014, the Railo Company Ltd. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*
**/
package lucee.commons.digest;
import java.io.InputStream;
import java.security.MessageDigest;
import lucee.commons.io.IOUtil;
import lucee.commons.io.res.Resource;
public class MD5Checksum {
public static byte[] createChecksum(Resource res) throws Exception {
InputStream is = res.getInputStream();
try{
byte[] buffer = new byte[1024];
MessageDigest complete = MessageDigest.getInstance("MD5");
int numRead;
do {
numRead = is.read(buffer);
if (numRead > 0) {
complete.update(buffer, 0, numRead);
}
}
while (numRead != -1);
return complete.digest();
}
finally {
IOUtil.closeEL(is);
}
}
// see this How-to for a faster way to convert
// a byte array to a HEX string
public static String getMD5Checksum(Resource res) throws Exception {
byte[] b = createChecksum(res);
String result = "";
for (int i=0; i < b.length; i++) {
result +=
Integer.toString( ( b[i] & 0xff ) + 0x100, 16).substring( 1 );
}
return result;
}
}