- import java.io.*;
- import java.security.*;
- import java.util.*;
-
- class ReturnDigest extends Thread
- {
- private File input;
- public ReturnDigest(File input)
- {
- this.input =input;
- }
-
- public void run()
- {
- try
- {
- FileInputStream in = new FileInputStream(input);
- MessageDigest sha = MessageDigest.getInstance ("SHA");
- DigestInputStream din = new DigestInputStream(in,sha);
- int b;
- while((b = din.read())!=-1);
- din.close();
- byte[] digest = sha.digest();
-
- digest = sha.digest();
- //System.out.println( digest );
- }
- catch (IOException ex)
- {
- System.err.println(ex);
- }
- catch (NoSuchAlgorithmException ex)
- {
- System.err.println(ex);
- }
- }
-
- public byte[] getDigest()
- {
- return digest;
- }
- }
-
- public class ReturnDigestUserInterface
- {
- public static void main(String[] args)
- {
- for (int i=0 ; i<args.length ; i++ )
- {
- File f = new File(args[i]);
- ReturnDigest dr = new Return Digest(f);
- dr.start();
-
- //显示结果
- StringBuffer result = new StringBuffer (f.toString());
- result.append(": ");
- byte[] digest = dr.getDigest();
- for( int j =0 ;j <digest.length; j++)
- {
- result.append (digest[j]+" ");
- }
- System.out.println(result);
- }
- }
- }
复制代码 为什么dr.start()开始的计算可能会在main()方法运行到dr.getDigest()调用之前结束?
|