Toggle navigation
VA伐木累
社区
VA伐木累
Proxy
JSON
BASE64
MyGit
登录
注册
×
登录
邮箱
密码
忘记密码?
文件哈希
•发布于
•作者
liuzy
•879 次浏览
•来自
资讯
``` package com.liuzy; import java.io.File; import java.io.FileInputStream; import java.math.BigInteger; import java.security.MessageDigest; import java.text.SimpleDateFormat; import java.util.zip.CRC32; public class FileHash { public static void main(String[] args) { String f = "D:\\360极速浏览器下载\\SQLyog-11.2.4-0.x86.reg"; File file = new File(f); System.out.println("文件:\t" + file.getPath()); System.out.println("文件大小:\t" + fileLength(file)); System.out.println("最后修改:\t" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(file.lastModified())); System.out.println("MD5:\t" + MD5(file)); System.out.println("SHA1:\t" + SHA1(file)); System.out.println("SHA256: " + SHA256(file)); System.out.println("CRC32:\t" + CRC32(file)); } public static String fileLength(File file) { long len = file.length(); if (len > 1073741824) { return String.format("%.2fGB", len / 1073741824 + len % 1073741824 / 1073741824f); } else if (len > 1048576) { return String.format("%.2fMB", len / 1048576 + len % 1048576 / 1048576f); } else if (len > 1024) { return String.format("%.2fKB", len / 1024 + len % 1024 / 1024f); } else { return len + "B"; } } public static String MD5(File file) { return hash(file, "MD5"); } public static String SHA1(File file) { return hash(file, "SHA-1"); } public static String SHA256(File file) { return hash(file, "SHA-256"); } public static String CRC32(File file) { String hex = null; try { FileInputStream in = new FileInputStream(file); CRC32 crc = new CRC32(); byte[] buffer = new byte[1024 * 1024 * 1]; int len = 0; while ((len = in.read(buffer)) > 0) { crc.update(buffer, 0, len); } in.close(); hex = Long.toHexString(crc.getValue()); } catch (Exception e) { e.printStackTrace(); } return hex; } public static String hash(File file, String algorithm) { String hex = null; try { FileInputStream in = new FileInputStream(file); MessageDigest md = MessageDigest.getInstance(algorithm); byte[] buffer = new byte[1024 * 1024 * 10]; int len = 0; while ((len = in.read(buffer)) > 0) { md.update(buffer, 0, len); } in.close(); hex = toHex(md.digest()); } catch (Exception e) { e.printStackTrace(); } return hex; } public static String toHex(byte[] bytes) { BigInteger bi = new BigInteger(1, bytes); String hex = bi.toString(16); int paddingLength = (bytes.length * 2) - hex.length(); if (paddingLength > 0) return String.format("%0" + paddingLength + "d", 0) + hex; else return hex; } } ```
0 回复
作者
liuzy
积分: 841
“ 黑眼圈圈男 ”
无人回复话题
SonarQube 9.4 + PostgreSQL
shell倒计时
日常网络巧技
使用ssh创建socks5代理服务
NodeJS集群demo
作者其他话题
SonarQube 9.4 + PostgreSQL
shell倒计时
日常网络巧技
使用ssh创建socks5代理服务
NodeJS集群demo
回到顶部
友情链接:
JFinal
©2015 Powered by
jfinalbbs
沪ICP备15012258号