对于一些大文件,有时会需要计算部分内容的Hash,下面的函数计算了 文件头尾各1M,中间跳跃100M取10K 以及文件大小的Hash值
public static String CalHash(String path) throws IOException { File file = new File(path); if (!file.canRead()) return ""; if (file.length() < 150 * 1024 * 1024) { return ""; } //FileInputStream in = new FileInputStream(file); RandomAccessFile in = new RandomAccessFile(file, "r"); MessageDigest messagedigest; try { messagedigest = MessageDigest.getInstance("SHA1"); int szRead = 0; int size_1M = 1024*1024; byte[] buffer = new byte[size_1M]; // 文件头尾各1M,中间跳跃100M取10K算Hash // 开头 szRead = in.read(buffer); messagedigest.update(buffer); // 结尾 in.seek(file.length() - size_1M); szRead = in.read(buffer); messagedigest.update(buffer); int BUFFER_SIZE = (10 * 1024); buffer = new byte[(int) BUFFER_SIZE]; long endPos = file.length() - 1024 * 1024 - BUFFER_SIZE; long skipSize = (long) ( ((float) (file.length() / (float) (100.0 * 1024.0 * 1024.0) - (float) 1.0) * BUFFER_SIZE) ); long currentPos = 1024 * 1024; in.seek(currentPos); while (in.getFilePointer() < endPos && in.read(buffer) > 0) { in.seek(in.getFilePointer() + skipSize); messagedigest.update(buffer); } // 计算文件size的hash long fsize = file.length(); byte[] fSizeBytes = getBytes(fsize); messagedigest.update(fSizeBytes); return byte2hex(messagedigest.digest()); } catch (NoSuchAlgorithmException e) { } catch (OutOfMemoryError e) { } finally { in.close(); } return null; }
时间: 2024-10-07 14:05:00