/*
* Copyright 2009 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.gradle.util;
import org.gradle.api.UncheckedIOException;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.io.File;
import java.io.InputStream;
import java.io.FileInputStream;
import java.io.IOException;
/**
* @author Hans Dockter
*/
public class HashUtil {
public static String createHash(String scriptText) {
MessageDigest messageDigest;
try {
messageDigest = MessageDigest.getInstance("MD5");
} catch (NoSuchAlgorithmException e) {
throw UncheckedException.asUncheckedException(e);
}
messageDigest.update(scriptText.getBytes());
return new BigInteger(1, messageDigest.digest()).toString(16);
}
public static byte[] createHash(File file) {
MessageDigest messageDigest;
try {
messageDigest = MessageDigest.getInstance("MD5");
} catch (NoSuchAlgorithmException e) {
throw UncheckedException.asUncheckedException(e);
}
try {
byte[] buffer = new byte[4096];
InputStream instr = new FileInputStream(file);
try {
while (true) {
int nread = instr.read(buffer);
if (nread < 0) {
break;
}
messageDigest.update(buffer, 0, nread);
}
} finally {
instr.close();
}
} catch (IOException e) {
throw new UncheckedIOException(e);
}
return messageDigest.digest();
}
}