Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ru.ifmo.ctddev.startsev.walk;
- import java.io.IOException;
- import java.io.InputStream;
- import java.nio.charset.StandardCharsets;
- import java.nio.file.Files;
- import java.nio.file.Path;
- import java.nio.file.Paths;
- import java.nio.file.StandardOpenOption;
- import java.security.MessageDigest;
- import java.security.NoSuchAlgorithmException;
- import java.util.List;
- /**
- * Created by sandwwraith(@gmail.com)
- * ITMO University, 2016
- **/
- public class Utils {
- public static String MD5(Path filepath) throws NoSuchAlgorithmException, IOException {
- MessageDigest digest = MessageDigest.getInstance("MD5");
- try (InputStream stream = Files.newInputStream(filepath, StandardOpenOption.READ)) {
- byte[] buffer = new byte[1024];
- int c;
- while ((c = stream.read(buffer)) >= 0) {
- digest.update(buffer, 0, c);
- }
- StringBuilder builder = new StringBuilder();
- for (byte b : digest.digest()) {
- builder.append(String.format("%02X", b));
- }
- return builder.toString();
- }
- }
- public static String MD5FormatIgnoreIOError(Path filepath) {
- String s;
- try {
- s = Utils.MD5(filepath);
- } catch (IOException e) {
- s = "00000000000000000000000000000000";
- } catch (NoSuchAlgorithmException e) {
- System.err.println("MD5 is not supported on this machine");
- System.exit(-1);
- s = "UNREACHABLE CODE";
- }
- s += " " + filepath.toString();
- return s;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement