Possible to calculate MD5 (or other) hash with buffered reads?

后端 未结 5 1691
长情又很酷
长情又很酷 2020-11-27 03:37

I need to calculate checksums of quite large files (gigabytes). This can be accomplished using the following method:

    private byte[] calcHash(string file         


        
5条回答
  •  囚心锁ツ
    2020-11-27 04:14

    You use the TransformBlock and TransformFinalBlock methods to process the data in chunks.

    // Init
    MD5 md5 = MD5.Create();
    int offset = 0;
    
    // For each block:
    offset += md5.TransformBlock(block, 0, block.Length, block, 0);
    
    // For last block:
    md5.TransformFinalBlock(block, 0, block.Length);
    
    // Get the has code
    byte[] hash = md5.Hash;
    

    Note: It works (at least with the MD5 provider) to send all blocks to TransformBlock and then send an empty block to TransformFinalBlock to finalise the process.

提交回复
热议问题