chr
2025-01-03 31a636e735a0addc56e4f4527f500b7aa0874eb5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
using PdmSwPlugin.Common.Setting;
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
 
namespace PdmSwPlugin.Common.Util
{
    public class MD5Util
    {
        public static string Md5SHA1(string filePath)
        {
            if (string.IsNullOrEmpty(filePath))
            {
                return null;
            }
            try
            {
                using (FileStream fsRead = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                {
                    SHA1CryptoServiceProvider oSHA1Hasher = new SHA1CryptoServiceProvider();
                    byte[] arrayHashValue = oSHA1Hasher.ComputeHash(fsRead);
                    string sha1ReturnString = BitConverter.ToString(arrayHashValue).Replace("-", "");
                    return sha1ReturnString;
                }
            }
            catch (Exception e)
            {
                return null;
            }
        }
 
        public static string Md5PDM(string filePath)
        {
            if (string.IsNullOrEmpty(filePath))
            {
                return null;
            }
            try
            {
                using (FileStream fsRead = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                {
                    MD5 md5 = new MD5CryptoServiceProvider();
                    byte[] retVal = md5.ComputeHash(fsRead);
                    fsRead.Close();
                    StringBuilder sb = new StringBuilder();
                    for (int i = 0; i < retVal.Length; i++)
                    {
                        sb.Append(retVal[i].ToString("x2"));
                    }
                    return sb.ToString();
                }
            }
            catch (Exception e)
            {
                return null;
            }
        }
 
        public static string Get(string filePath)
        {
            /// YWT就用SHA1
            if (PluginSetting.Instance.Customer == "YWT") return Md5SHA1(filePath);
            // 否则用PDM的
            return Md5PDM(filePath);
        }
 
        public static bool FileEquals(string path1, string path2)
        {
            return Get(path1) == Get(path2);
        }
 
        public static bool IsFile(string path1, string md5)
        {
            return Get(path1) == md5;
        }
 
    }
}