0

ファイルの MD5 ハッシュをチェックしようとすると、エラーが発生します。

ファイル notice.txt の内容は次のとおりです。

My name is sanjay yadav . i am in btech computer science .>>

onlineMD5.comでオンラインで確認したところ、MD5 は次のようになりました90F450C33FAC09630D344CBA9BF80471

私のプログラムの出力は次のとおりです。

My name is sanjay yadav . i am in btech computer science .
Read 58 bytes
d41d8cd98f00b204e9800998ecf8427e

これが私のコードです:

import java.io.*;
import java.math.BigInteger;
import java.security.DigestException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class MsgDgt {
    public static void main(String[] args) throws IOException, DigestException, NoSuchAlgorithmException {

        FileInputStream inputstream = null;
        byte[] mybyte = new byte[1024];

        inputstream = new FileInputStream("e://notice.txt");
        int total = 0;
        int nRead = 0;
        MessageDigest md = MessageDigest.getInstance("MD5");
        while ((nRead = inputstream.read(mybyte)) != -1) {
            System.out.println(new String(mybyte));
            total += nRead;
            md.update(mybyte, 0, nRead);
        }

        System.out.println("Read " + total + " bytes");
        md.digest();
        System.out.println(new BigInteger(1, md.digest()).toString(16));
    }
}
4

2 に答える 2

0

私はこの機能を使用します:

public static String md5Hash(File file) {
    try {
        MessageDigest md = MessageDigest.getInstance("MD5");
        InputStream is = new FileInputStream(file);
        byte[] buffer = new byte[1024];

        try {
            is = new DigestInputStream(is, md);

            while (is.read(buffer) != -1) { }
        } finally {
            is.close();
        }

        byte[] digest = md.digest();

        BigInteger bigInt = new BigInteger(1, digest);
        String output = bigInt.toString(16);
        while (output.length() < 32) {
            output = "0" + output;
        }

        return output;
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return null;
}
于 2013-07-02T06:06:10.160 に答える