use of java.security.MessageDigest in project gitblit by gitblit.
the class PluginManager method download.
/**
* Downloads a file with optional checksum verification.
*
* @param url
* @param verifyChecksum
* @return
* @throws IOException
*/
protected File download(String url, boolean verifyChecksum) throws IOException {
File file = downloadFile(url);
if (!verifyChecksum) {
return file;
}
File sha1File = null;
try {
sha1File = downloadFile(url + ".sha1");
} catch (IOException e) {
}
File md5File = null;
try {
md5File = downloadFile(url + ".md5");
} catch (IOException e) {
}
if (sha1File == null && md5File == null) {
throw new IOException("Missing SHA1 and MD5 checksums for " + url);
}
String expected;
MessageDigest md = null;
if (sha1File != null && sha1File.exists()) {
// prefer SHA1 to MD5
expected = FileUtils.readContent(sha1File, "\n").split(" ")[0].trim();
try {
md = MessageDigest.getInstance("SHA-1");
} catch (NoSuchAlgorithmException e) {
logger.error(null, e);
}
} else {
expected = FileUtils.readContent(md5File, "\n").split(" ")[0].trim();
try {
md = MessageDigest.getInstance("MD5");
} catch (Exception e) {
logger.error(null, e);
}
}
// calculate the checksum
FileInputStream is = null;
try {
is = new FileInputStream(file);
DigestInputStream dis = new DigestInputStream(is, md);
byte[] buffer = new byte[1024];
while ((dis.read(buffer)) > -1) {
// read
}
dis.close();
byte[] digest = md.digest();
String calculated = StringUtils.toHex(digest).trim();
if (!expected.equals(calculated)) {
String msg = String.format("Invalid checksum for %s\nAlgorithm: %s\nExpected: %s\nCalculated: %s", file.getAbsolutePath(), md.getAlgorithm(), expected, calculated);
file.delete();
throw new IOException(msg);
}
} finally {
if (is != null) {
is.close();
}
}
return file;
}
use of java.security.MessageDigest in project gitblit by gitblit.
the class TicketModel method getSHA1.
/**
* Calculates the SHA1 of the byte array.
*
* @param bytes
* @return sha1 of the byte array
*/
static String getSHA1(byte[] bytes) {
try {
MessageDigest md = MessageDigest.getInstance("SHA-1");
md.update(bytes, 0, bytes.length);
byte[] digest = md.digest();
return toHex(digest);
} catch (NoSuchAlgorithmException t) {
throw new RuntimeException(t);
}
}
use of java.security.MessageDigest in project fresco by facebook.
the class SecureHashUtil method makeSHA1HashBase64.
public static String makeSHA1HashBase64(byte[] bytes) {
try {
MessageDigest md = MessageDigest.getInstance("SHA-1");
md.update(bytes, 0, bytes.length);
byte[] sha1hash = md.digest();
return Base64.encodeToString(sha1hash, Base64.URL_SAFE | Base64.NO_PADDING | Base64.NO_WRAP);
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
}
use of java.security.MessageDigest in project fqrouter by fqrouter.
the class IOUtils method copy.
public static String copy(InputStream inputStream, OutputStream outputStream, ChunkCopied callback) throws Exception {
MessageDigest md5 = MessageDigest.getInstance("MD5");
byte[] buffer = new byte[1024 * 32];
int length;
while ((length = inputStream.read(buffer)) > 0) {
md5.update(buffer, 0, length);
if (null != outputStream) {
outputStream.write(buffer, 0, length);
}
if (null != callback) {
callback.onChunkCopied(buffer, length);
}
}
return new BigInteger(1, md5.digest()).toString(16);
}
use of java.security.MessageDigest in project stetho by facebook.
the class WebSocketHandler method generateServerKey.
private static String generateServerKey(String clientKey) {
try {
String serverKey = clientKey + SERVER_KEY_GUID;
MessageDigest sha1 = MessageDigest.getInstance("SHA-1");
sha1.update(Utf8Charset.encodeUTF8(serverKey));
return Base64.encodeToString(sha1.digest(), Base64.NO_WRAP);
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
}
Aggregations