Java Code Examples for java.security.DigestInputStream#close()

The following examples show how to use java.security.DigestInputStream#close() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Utils.java    From java-scanner-access-twain with GNU Affero General Public License v3.0 7 votes vote down vote up
static String getMd5(InputStream input) {
    if(input == null) {
        return null;
    }
    try {
        MessageDigest md = MessageDigest.getInstance("MD5");
        DigestInputStream dis = new DigestInputStream(input, md);
        byte[] buffer = new byte[1024 * 8]; 
        while(dis.read(buffer) != -1) {
            ;
        }
        dis.close();
        byte[] raw = md.digest();

        BigInteger bigInt = new BigInteger(1, raw);
        StringBuilder hash = new StringBuilder(bigInt.toString(16));

        while(hash.length() < 32 ){
            hash.insert(0, '0');
        }
        return hash.toString();
    } catch (Throwable t) {
        return null;
    }
}
 
Example 2
Source File: Rd5DiffManager.java    From brouter with MIT License 6 votes vote down vote up
public static String getMD5( File f ) throws Exception
{
  MessageDigest md = MessageDigest.getInstance("MD5");
  BufferedInputStream bis = new BufferedInputStream( new FileInputStream( f ) );
  DigestInputStream dis = new DigestInputStream(bis, md);
  byte[] buf = new byte[8192];
  for(;;)
  {
    int len = dis.read( buf );
    if ( len <= 0 )
    {
      break;
    }
  }
  dis.close();
  byte[] bytes = md.digest();

  StringBuilder sb = new StringBuilder();
  for (int j = 0; j < bytes.length; j++)
  {
    int v = bytes[j] & 0xff;
    sb.append( hexChar( v >>> 4 ) ).append( hexChar( v & 0xf ) );
  }
  return sb.toString();
}
 
Example 3
Source File: Utils.java    From sheepit-client with GNU General Public License v2.0 6 votes vote down vote up
public static String md5(String path_of_file_) {
	try {
		MessageDigest md = MessageDigest.getInstance("MD5");
		InputStream is = Files.newInputStream(Paths.get(path_of_file_));
		DigestInputStream dis = new DigestInputStream(is, md);
		byte[] buffer = new byte[8192];
		while (dis.read(buffer) > 0)
			; // process the entire file
		String data = DatatypeConverter.printHexBinary(md.digest()).toLowerCase();
		dis.close();
		is.close();
		return data;
	}
	catch (NoSuchAlgorithmException | IOException e) {
		return "";
	}
}
 
Example 4
Source File: Utils.java    From java-ocr-api with GNU Affero General Public License v3.0 6 votes vote down vote up
static String getMd5(InputStream input) {
    if(input == null) {
        return null;
    }
    try {
        MessageDigest md = MessageDigest.getInstance("MD5");
        DigestInputStream dis = new DigestInputStream(input, md);
        byte[] buffer = new byte[1024 * 8]; 
        while(dis.read(buffer) != -1) {
            ;
        }
        dis.close();
        byte[] raw = md.digest();

        BigInteger bigInt = new BigInteger(1, raw);
        StringBuilder hash = new StringBuilder(bigInt.toString(16));

        while(hash.length() < 32 ){
            hash.insert(0, '0');
        }
        return hash.toString();
    } catch (Throwable t) {
        return null;
    }
}
 
Example 5
Source File: TestIOUtils.java    From aws-encryption-sdk-java with Apache License 2.0 6 votes vote down vote up
public static byte[] computeFileDigest(final String fileName) throws IOException {
    try {
        final FileInputStream fis = new FileInputStream(fileName);
        final MessageDigest md = MessageDigest.getInstance("SHA-256");
        final DigestInputStream dis = new DigestInputStream(fis, md);

        final int readLen = 128;
        final byte[] readBytes = new byte[readLen];
        while (dis.read(readBytes) != -1) {
        }
        dis.close();

        return md.digest();
    } catch (NoSuchAlgorithmException e) {
        // shouldn't get here since we hardcode the algorithm.
    }

    return null;
}
 
Example 6
Source File: MD5Utils.java    From okhttp-OkGo with Apache License 2.0 6 votes vote down vote up
/**
 * 获取文件的 MD5
 */
public static String encode(File file) {
    try {
        MessageDigest messageDigest = MessageDigest.getInstance("MD5");
        FileInputStream inputStream = new FileInputStream(file);
        DigestInputStream digestInputStream = new DigestInputStream(inputStream, messageDigest);
        //必须把文件读取完毕才能拿到md5
        byte[] buffer = new byte[4096];
        while (digestInputStream.read(buffer) > -1) {
        }
        MessageDigest digest = digestInputStream.getMessageDigest();
        digestInputStream.close();
        byte[] md5 = digest.digest();
        StringBuilder sb = new StringBuilder();
        for (byte b : md5) {
            sb.append(String.format("%02X", b));
        }
        return sb.toString().toLowerCase();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}
 
Example 7
Source File: FileScan.java    From fileserver with Apache License 2.0 6 votes vote down vote up
public String getMd5(File f) throws IOException {

		FileInputStream in = new FileInputStream(f);
		DigestInputStream di = new DigestInputStream(in, md);

		byte[] buffer = new byte[bufferSize];
		while (di.read(buffer) > 0)
			;
		md = di.getMessageDigest();

		di.close();
		in.close();

		byte[] digest = md.digest();
		return byteArrayToHex(digest);
	}
 
Example 8
Source File: HttpProxyTask.java    From open-rmbt with Apache License 2.0 6 votes vote down vote up
/**
 * 
 * @param inputStream
 * @return
 * @throws NoSuchAlgorithmException
 * @throws IOException
 */
public static Md5Result generateChecksum(InputStream inputStream) throws NoSuchAlgorithmException, IOException {
	Md5Result md5 = new Md5Result();
	MessageDigest md = MessageDigest.getInstance("MD5");
	DigestInputStream dis = new DigestInputStream(inputStream, md);
	
	byte[] dataBytes = new byte[4096];
      
       int nread = 0; 
       while ((nread = dis.read(dataBytes)) != -1) {
       	md5.contentLength += nread;
       };
       
       dis.close();
       
       long startNs = System.nanoTime();
       md5.md5 = generateChecksumFromDigest(md.digest());
       md5.generatingTimeNs = System.nanoTime() - startNs;
       
       return md5;
}
 
Example 9
Source File: FileSnippet.java    From mobly-bundled-snippets with Apache License 2.0 6 votes vote down vote up
@Rpc(description = "Compute MD5 hash on a content URI. Return the MD5 has has a hex string.")
public String fileMd5Hash(String uri) throws IOException, NoSuchAlgorithmException {
    Uri uri_ = Uri.parse(uri);
    ParcelFileDescriptor pfd = mContext.getContentResolver().openFileDescriptor(uri_, "r");
    MessageDigest md = MessageDigest.getInstance("MD5");
    int length = (int) pfd.getStatSize();
    byte[] buf = new byte[length];
    ParcelFileDescriptor.AutoCloseInputStream stream =
            new ParcelFileDescriptor.AutoCloseInputStream(pfd);
    DigestInputStream dis = new DigestInputStream(stream, md);
    try {
        dis.read(buf, 0, length);
        return Utils.bytesToHexString(md.digest());
    } finally {
        dis.close();
        stream.close();
    }
}
 
Example 10
Source File: FileUtils.java    From sofa-ark with Apache License 2.0 6 votes vote down vote up
/**
 * Generate a SHA.1 Hash for a given file.
 * @param file the file to hash
 * @return the hash value as a String
 * @throws IOException if the file cannot be read
 */
public static String sha1Hash(File file) throws IOException {
    try {
        DigestInputStream inputStream = new DigestInputStream(new FileInputStream(file),
            MessageDigest.getInstance("SHA-1"));
        try {
            byte[] buffer = new byte[4098];
            while (inputStream.read(buffer) != -1) { //NOPMD
                // Read the entire stream
            }
            return bytesToHex(inputStream.getMessageDigest().digest());
        } finally {
            inputStream.close();
        }
    } catch (NoSuchAlgorithmException ex) {
        throw new IllegalStateException(ex);
    }
}
 
Example 11
Source File: Utils.java    From SecuritySample with Apache License 2.0 5 votes vote down vote up
private static byte[] getDigest(InputStream in, String algorithm) throws Throwable {
    int BUFFER_SIZE = 2048;
    MessageDigest md = MessageDigest.getInstance(algorithm);
    try {
        DigestInputStream dis = new DigestInputStream(in, md);
        byte[] buffer = new byte[BUFFER_SIZE];
        while (dis.read(buffer) != -1) {
            //
        }
        dis.close();
    } finally {
        in.close();
    }
    return md.digest();
}
 
Example 12
Source File: Main.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
/** Display the location and checksum of a class. */
void showClass(String className) {
    PrintWriter pw = log.getWriter(WriterKind.NOTICE);
    pw.println("javac: show class: " + className);
    URL url = getClass().getResource('/' + className.replace('.', '/') + ".class");
    if (url == null)
        pw.println("  class not found");
    else {
        pw.println("  " + url);
        try {
            final String algorithm = "MD5";
            byte[] digest;
            MessageDigest md = MessageDigest.getInstance(algorithm);
            DigestInputStream in = new DigestInputStream(url.openStream(), md);
            try {
                byte[] buf = new byte[8192];
                int n;
                do { n = in.read(buf); } while (n > 0);
                digest = md.digest();
            } finally {
                in.close();
            }
            StringBuilder sb = new StringBuilder();
            for (byte b: digest)
                sb.append(String.format("%02x", b));
            pw.println("  " + algorithm + " checksum: " + sb);
        } catch (Exception e) {
            pw.println("  cannot compute digest: " + e);
        }
    }
}
 
Example 13
Source File: Main.java    From javaide with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Display the location and checksum of a class.
 */
void showClass(String className) {
    out.println("javac: show class: " + className);
    URL url = getClass().getResource('/' + className.replace('.', '/') + ".class");
    if (url == null)
        out.println("  class not found");
    else {
        out.println("  " + url);
        try {
            final String algorithm = "MD5";
            byte[] digest;
            MessageDigest md = MessageDigest.getInstance(algorithm);
            DigestInputStream in = new DigestInputStream(url.openStream(), md);
            try {
                byte[] buf = new byte[8192];
                int n;
                do {
                    n = in.read(buf);
                } while (n > 0);
                digest = md.digest();
            } finally {
                in.close();
            }
            StringBuilder sb = new StringBuilder();
            for (byte b : digest)
                sb.append(String.format("%02x", b));
            out.println("  " + algorithm + " checksum: " + sb);
        } catch (Exception e) {
            out.println("  cannot compute digest: " + e);
        }
    }
}
 
Example 14
Source File: Main.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
/** Display the location and checksum of a class. */
void showClass(String className) {
    PrintWriter pw = log.getWriter(WriterKind.NOTICE);
    pw.println("javac: show class: " + className);
    URL url = getClass().getResource('/' + className.replace('.', '/') + ".class");
    if (url == null)
        pw.println("  class not found");
    else {
        pw.println("  " + url);
        try {
            final String algorithm = "MD5";
            byte[] digest;
            MessageDigest md = MessageDigest.getInstance(algorithm);
            DigestInputStream in = new DigestInputStream(url.openStream(), md);
            try {
                byte[] buf = new byte[8192];
                int n;
                do { n = in.read(buf); } while (n > 0);
                digest = md.digest();
            } finally {
                in.close();
            }
            StringBuilder sb = new StringBuilder();
            for (byte b: digest)
                sb.append(String.format("%02x", b));
            pw.println("  " + algorithm + " checksum: " + sb);
        } catch (Exception e) {
            pw.println("  cannot compute digest: " + e);
        }
    }
}
 
Example 15
Source File: Main.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
/** Display the location and checksum of a class. */
void showClass(String className) {
    PrintWriter pw = log.getWriter(WriterKind.NOTICE);
    pw.println("javac: show class: " + className);
    URL url = getClass().getResource('/' + className.replace('.', '/') + ".class");
    if (url == null)
        pw.println("  class not found");
    else {
        pw.println("  " + url);
        try {
            final String algorithm = "MD5";
            byte[] digest;
            MessageDigest md = MessageDigest.getInstance(algorithm);
            DigestInputStream in = new DigestInputStream(url.openStream(), md);
            try {
                byte[] buf = new byte[8192];
                int n;
                do { n = in.read(buf); } while (n > 0);
                digest = md.digest();
            } finally {
                in.close();
            }
            StringBuilder sb = new StringBuilder();
            for (byte b: digest)
                sb.append(String.format("%02x", b));
            pw.println("  " + algorithm + " checksum: " + sb);
        } catch (Exception e) {
            pw.println("  cannot compute digest: " + e);
        }
    }
}
 
Example 16
Source File: Main.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
/** Display the location and checksum of a class. */
void showClass(String className) {
    PrintWriter pw = log.getWriter(WriterKind.NOTICE);
    pw.println("javac: show class: " + className);
    URL url = getClass().getResource('/' + className.replace('.', '/') + ".class");
    if (url == null)
        pw.println("  class not found");
    else {
        pw.println("  " + url);
        try {
            final String algorithm = "MD5";
            byte[] digest;
            MessageDigest md = MessageDigest.getInstance(algorithm);
            DigestInputStream in = new DigestInputStream(url.openStream(), md);
            try {
                byte[] buf = new byte[8192];
                int n;
                do { n = in.read(buf); } while (n > 0);
                digest = md.digest();
            } finally {
                in.close();
            }
            StringBuilder sb = new StringBuilder();
            for (byte b: digest)
                sb.append(String.format("%02x", b));
            pw.println("  " + algorithm + " checksum: " + sb);
        } catch (Exception e) {
            pw.println("  cannot compute digest: " + e);
        }
    }
}
 
Example 17
Source File: Main.java    From java-n-IDE-for-Android with Apache License 2.0 5 votes vote down vote up
/**
 * Display the location and checksum of a class.
 */
void showClass(String className) {
    out.println("javac: show class: " + className);
    URL url = getClass().getResource('/' + className.replace('.', '/') + ".class");
    if (url == null)
        out.println("  class not found");
    else {
        out.println("  " + url);
        try {
            final String algorithm = "MD5";
            byte[] digest;
            MessageDigest md = MessageDigest.getInstance(algorithm);
            DigestInputStream in = new DigestInputStream(url.openStream(), md);
            try {
                byte[] buf = new byte[8192];
                int n;
                do {
                    n = in.read(buf);
                } while (n > 0);
                digest = md.digest();
            } finally {
                in.close();
            }
            StringBuilder sb = new StringBuilder();
            for (byte b : digest)
                sb.append(String.format("%02x", b));
            out.println("  " + algorithm + " checksum: " + sb);
        } catch (Exception e) {
            out.println("  cannot compute digest: " + e);
        }
    }
}
 
Example 18
Source File: Main.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
/** Display the location and checksum of a class. */
void showClass(String className) {
    PrintWriter pw = log.getWriter(WriterKind.NOTICE);
    pw.println("javac: show class: " + className);
    URL url = getClass().getResource('/' + className.replace('.', '/') + ".class");
    if (url == null)
        pw.println("  class not found");
    else {
        pw.println("  " + url);
        try {
            final String algorithm = "MD5";
            byte[] digest;
            MessageDigest md = MessageDigest.getInstance(algorithm);
            DigestInputStream in = new DigestInputStream(url.openStream(), md);
            try {
                byte[] buf = new byte[8192];
                int n;
                do { n = in.read(buf); } while (n > 0);
                digest = md.digest();
            } finally {
                in.close();
            }
            StringBuilder sb = new StringBuilder();
            for (byte b: digest)
                sb.append(String.format("%02x", b));
            pw.println("  " + algorithm + " checksum: " + sb);
        } catch (Exception e) {
            pw.println("  cannot compute digest: " + e);
        }
    }
}
 
Example 19
Source File: f.java    From MiBandDecompiled with Apache License 2.0 4 votes vote down vote up
public void a()
{
    if (j == null || !(new File(j)).exists())
    {
        Message message = l.obtainMessage();
        message.what = -5;
        message.obj = new String("");
        l.sendMessage(message);
        return;
    }
    k.onPrepareStart();
    File file = new File(j);
    m = file.length();
    try
    {
        MessageDigest messagedigest = MessageDigest.getInstance("SHA-1");
        FileInputStream fileinputstream = new FileInputStream(file);
        DigestInputStream digestinputstream = new DigestInputStream(fileinputstream, messagedigest);
        for (byte abyte0[] = new byte[0x80000]; digestinputstream.read(abyte0) > 0;) { }
        MessageDigest messagedigest1 = digestinputstream.getMessageDigest();
        n = DataConvert.toHexString(messagedigest1.digest());
        messagedigest1.reset();
        fileinputstream.close();
        digestinputstream.close();
    }
    catch (Exception exception)
    {
        Message message1 = l.obtainMessage();
        message1.what = -2;
        message1.obj = new String("");
        l.sendMessage(message1);
        return;
    }
    try
    {
        MessageDigest messagedigest2 = MessageDigest.getInstance("MD5");
        FileInputStream fileinputstream1 = new FileInputStream(file);
        DigestInputStream digestinputstream1 = new DigestInputStream(fileinputstream1, messagedigest2);
        for (byte abyte1[] = new byte[0x80000]; digestinputstream1.read(abyte1) > 0;) { }
        MessageDigest messagedigest3 = digestinputstream1.getMessageDigest();
        o = DataConvert.toHexString(messagedigest3.digest());
        messagedigest3.reset();
        fileinputstream1.close();
        digestinputstream1.close();
    }
    catch (Exception exception1)
    {
        Message message2 = l.obtainMessage();
        message2.what = -2;
        message2.obj = new String("");
        l.sendMessage(message2);
        return;
    }
    b();
}