public static void main(String args[]){ try{ String myInfo = "我的测试摘要校验码"; MessageDigest md = MessageDigest.getInstance("SHA-1");//SHA-256 SHA-512 md.update(myInfo.getBytes(), 0, myInfo.getBytes().length); byte[] hashValue = md.digest(); String hexValue = DigestUtils.sha1Hex(hashValue); System.out.println(hexValue); } catch (Exception e) { e.printStackTrace(); } }
@Test public void test2() throws IOException { InputStream in = null; try{ MessageDigest md = MessageDigest.getInstance("SHA1"); in = new FileInputStream("/usr/loacl/aa.txt"); int blockSize = 131072; byte[] inBytes = new byte[blockSize]; while (true){ int len = in.read(inBytes); if (len == -1) { break; } md.update(inBytes, 0, len); } byte[] hashValue = md.digest(); String hexValue = DigestUtils.sha1Hex(hashValue); System.out.println(hexValue); } catch (Exception e) { e.printStackTrace(); }finally { in.close(); } }