Calculate MD5 checksum for a file

前端 未结 6 911
囚心锁ツ
囚心锁ツ 2020-11-22 08:03

I\'m using iTextSharp to read the text from a PDF file. However, there are times I cannot extract text, because the PDF file is only containing images. I download the same P

6条回答
  •  谎友^
    谎友^ (楼主)
    2020-11-22 08:19

    I know this question was already answered, but this is what I use:

    using (FileStream fStream = File.OpenRead(filename)) {
        return GetHash(fStream)
    }
    

    Where GetHash:

    public static String GetHash(Stream stream) where T : HashAlgorithm {
        StringBuilder sb = new StringBuilder();
    
        MethodInfo create = typeof(T).GetMethod("Create", new Type[] {});
        using (T crypt = (T) create.Invoke(null, null)) {
            byte[] hashBytes = crypt.ComputeHash(stream);
            foreach (byte bt in hashBytes) {
                sb.Append(bt.ToString("x2"));
            }
        }
        return sb.ToString();
    }
    

    Probably not the best way, but it can be handy.

提交回复
热议问题