OSDN Git Service

AppliStation,バージョンを1.1.0.*に変更。ソースファイルへのコメントの加筆
[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                 /// <summary>\r
14                 /// ファイルのサイズ(容量)をバイト単位で返す\r
15                 /// </summary>\r
16                 [XmlEnum(Name="size")]\r
17                 SIZE,\r
18                 [XmlEnum(Name="md5")]\r
19                 MD5SUM,\r
20                 [XmlEnum(Name="sha1")]\r
21                 SHA1SUM,\r
22                 [XmlEnum(Name="sha256")]\r
23                 SHA256SUM,\r
24         }\r
25         \r
26         public class HashValue\r
27         {\r
28                 /// <summary>\r
29                 /// ハッシュ値の種類(計算法)\r
30                 /// </summary>\r
31                 [XmlAttribute]\r
32                 public HashValueType Type;\r
33                 \r
34                 /// <summary>\r
35                 /// ハッシュ値そのものをあらわす\r
36                 /// </summary>\r
37                 [XmlText]\r
38                 public string Value;\r
39                 \r
40                 /// <summary>\r
41                 /// コンストラクタ\r
42                 /// </summary>\r
43                 public HashValue()\r
44                 {\r
45                 }\r
46                 \r
47                 /// <summary>\r
48                 /// 与えられたファイルのハッシュ値が同一であるか検証する\r
49                 /// </summary>\r
50                 /// <param name="path">ハッシュ計算を行う対象のファイル</param>\r
51                 /// <returns>ファイルのハッシュ値が妥当な場合true。</returns>\r
52                 public bool Validate(string path)\r
53                 {\r
54                         return string.Compare(this.Value, HashValueFor(path, this.Type), true) == 0;\r
55                 }\r
56                 \r
57                 /// <summary>\r
58                 /// ファイルのハッシュを計算する\r
59                 /// </summary>\r
60                 /// <param name="file">計算対象のファイル</param>\r
61                 /// <param name="type">ハッシュの種類(計算法)</param>\r
62                 /// <returns></returns>\r
63                 public static string HashValueFor(string file, HashValueType type)\r
64                 {\r
65                         using (FileStream fs = new FileStream(file, FileMode.Open)) {\r
66                                 return HashValue.HashValueFor(fs, type);\r
67                         }\r
68                 }\r
69                 \r
70                 /// <summary>\r
71                 /// ストリーム入力からハッシュを計算する\r
72                 /// </summary>\r
73                 /// <param name="stream">ストリーム入力</param>\r
74                 /// <param name="type">ハッシュの種類(計算法)</param>\r
75                 /// <returns>ハッシュ値</returns>\r
76                 public static string HashValueFor(Stream stream, HashValueType type)\r
77                 {\r
78                         byte[] hash;\r
79                         \r
80                         switch (type) {\r
81                                 case HashValueType.SIZE:\r
82                                         return stream.Length.ToString();\r
83                                 case HashValueType.MD5SUM:\r
84                                         hash = MD5.Create().ComputeHash(stream);\r
85                                         break;\r
86                                 case HashValueType.SHA1SUM:\r
87                                         hash = SHA1.Create().ComputeHash(stream);\r
88                                         break;\r
89                                 case HashValueType.SHA256SUM:\r
90                                         hash = SHA256.Create().ComputeHash(stream);\r
91                                         break;\r
92                                 default:\r
93                                         throw new NotSupportedException(string.Format("Hash type {0} does not supported", type));\r
94                         }\r
95                         \r
96                         return BitConverter.ToString(hash).Replace("-",string.Empty);\r
97                 }\r
98         }\r
99 }\r