/* * Created on 13-3-29 * * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except * in compliance with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software distributed under the License * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express * or implied. See the License for the specific language governing permissions and limitations under * the License. * * Copyright @2013 the original author or authors. */ package ch18io.file; /** * Description of this file. * * @author XiongNeng * @version 1.0 * @since 13-3-29 */ import java.io.FileInputStream; import java.security.MessageDigest; public class GenerateFileChecksum { public static void main(String args[]) throws Exception { String datafile = "c:\\INSTLOG.TXT"; MessageDigest md = MessageDigest.getInstance("SHA1"); FileInputStream fis = new FileInputStream(datafile); byte[] dataBytes = new byte[1024]; int nread; while ((nread = fis.read(dataBytes)) != -1) { md.update(dataBytes, 0, nread); } byte[] mdbytes = md.digest(); //convert the byte to hex format StringBuilder sb = new StringBuilder(""); for (byte mdbyte : mdbytes) { sb.append(Integer.toString((mdbyte & 0xff) + 0x100, 16).substring(1)); } System.out.println("Digest(in hex format):: " + sb.toString()); } }