Issues with SHA1 hash implementation in Android

前端 未结 4 665
自闭症患者
自闭症患者 2020-12-13 11:33

I have two small snippets for calculating SHA1.

One is very fast but it seems that it isn\'t correct and the other is very slow but correct.
I think the F

4条回答
  •  时光说笑
    2020-12-13 12:16

    Do this:

    MessageDigest md = MessageDigest.getInstance("SHA1");
    InputStream in = new FileInputStream("hereyourinputfilename");
    byte[] buf = new byte[8192];
    for (;;) {
        int len = in.read(buf);
        if (len < 0)
            break;
        md.update(buf, 0, len);
    }
    in.close();
    byte[] hash = md.digest();
    

    Performance comes from handling data by blocks. An 8 kB buffer, as here, ought to be blocky enough. You do not have to use a BufferedInputStream since the 8 kB buffer also serves as I/O buffer.

提交回复
热议问题