OSDN Git Service

20651d67e6d5c28a11b0cff23d60011819463375
[applistation/AppliStation.git] / na-get-lib / NaGet.Packages / HashValue.cs
1 using System;\r
2 using System.IO;\r
3 using System.Xml.Serialization;\r
4 using System.Security.Cryptography;\r
5 \r
6 namespace NaGet.Packages\r
7 {\r
8         /// <summary>\r
9         /// ハッシュ値の種類(計算法)を表す。\r
10         /// </summary>\r
11         public enum HashValueType\r
12         {\r
13                 [XmlEnum(Name="size")]\r
14                 SIZE,\r
15                 [XmlEnum(Name="md5")]\r
16                 MD5SUM,\r
17                 [XmlEnum(Name="sha1")]\r
18                 SHA1SUM,\r
19                 [XmlEnum(Name="sha256")]\r
20                 SHA256SUM\r
21         }\r
22         \r
23         public class HashValue\r
24         {\r
25                 /// <summary>\r
26                 /// ハッシュ値の種類(計算法)\r
27                 /// </summary>\r
28                 [XmlAttribute]\r
29                 public HashValueType Type;\r
30                 \r
31                 /// <summary>\r
32                 /// ハッシュ値そのものをあらわす\r
33                 /// </summary>\r
34                 [XmlText]\r
35                 public string Value;\r
36                 \r
37                 /// <summary>\r
38                 /// コンストラクタ\r
39                 /// </summary>\r
40                 public HashValue()\r
41                 {\r
42                 }\r
43                 \r
44                 /// <summary>\r
45                 /// 与えられたファイルのハッシュ値が同一であるか検証する\r
46                 /// </summary>\r
47                 /// <param name="path">ハッシュ計算を行う対象のファイル</param>\r
48                 /// <returns>ファイルのハッシュ値が妥当な場合true。</returns>\r
49                 public bool Validate(string path)\r
50                 {\r
51                         return string.Compare(this.Value, HashValueFor(path, this.Type), true) == 0;\r
52                 }\r
53                 \r
54                 /// <summary>\r
55                 /// ファイルのハッシュを計算する\r
56                 /// </summary>\r
57                 /// <param name="file">計算対象のファイル</param>\r
58                 /// <param name="type">ハッシュの種類(計算法)</param>\r
59                 /// <returns></returns>\r
60                 public static string HashValueFor(string file, HashValueType type)\r
61                 {\r
62                         using (FileStream fs = new FileStream(file, FileMode.Open)) {\r
63                                 return HashValue.HashValueFor(fs, type);\r
64                         }\r
65                 }\r
66                 \r
67                 /// <summary>\r
68                 /// ストリーム入力からハッシュを計算する\r
69                 /// </summary>\r
70                 /// <param name="stream">ストリーム入力</param>\r
71                 /// <param name="type">ハッシュの種類(計算法)</param>\r
72                 /// <returns></returns>\r
73                 public static string HashValueFor(Stream stream, HashValueType type)\r
74                 {\r
75                         byte[] hash;\r
76                         \r
77                         switch (type) {\r
78                                 case HashValueType.SIZE:\r
79                                         return stream.Length.ToString();\r
80                                 case HashValueType.MD5SUM:\r
81                                         hash = MD5.Create().ComputeHash(stream);\r
82                                         break;\r
83                                 case HashValueType.SHA1SUM:\r
84                                         hash = SHA1.Create().ComputeHash(stream);\r
85                                         break;\r
86                                 case HashValueType.SHA256SUM:\r
87                                         hash = SHA256.Create().ComputeHash(stream);\r
88                                         break;\r
89                                 default:\r
90                                         throw new NotSupportedException(string.Format("Hash type {0} does not supported", type));\r
91                         }\r
92                         \r
93                         return BitConverter.ToString(hash).Replace("-",string.Empty);\r
94                 }\r
95         }\r
96 }\r