OSDN Git Service

na-get-lib,新構造タスクのキャンセル処理を修正。
[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                 [XmlEnum(Name="sha512")]\r
25                 SHA512SUM,\r
26         }\r
27         \r
28         public class HashValue\r
29         {\r
30                 /// <summary>\r
31                 /// ハッシュ値の種類(計算法)\r
32                 /// </summary>\r
33                 [XmlAttribute]\r
34                 public HashValueType Type;\r
35                 \r
36                 /// <summary>\r
37                 /// ハッシュ値そのものをあらわす\r
38                 /// </summary>\r
39                 [XmlText]\r
40                 public string Value;\r
41                 \r
42                 /// <summary>\r
43                 /// コンストラクタ\r
44                 /// </summary>\r
45                 public HashValue()\r
46                 {\r
47                 }\r
48                 \r
49                 /// <summary>\r
50                 /// 与えられたファイルのハッシュ値が同一であるか検証する\r
51                 /// </summary>\r
52                 /// <param name="path">ハッシュ計算を行う対象のファイル</param>\r
53                 /// <returns>ファイルのハッシュ値が妥当な場合true。</returns>\r
54                 public bool Validate(string path)\r
55                 {\r
56                         return string.Compare(this.Value, HashValueFor(path, this.Type), true) == 0;\r
57                 }\r
58                 \r
59                 /// <summary>\r
60                 /// ファイルのハッシュを計算する\r
61                 /// </summary>\r
62                 /// <param name="file">計算対象のファイル</param>\r
63                 /// <param name="type">ハッシュの種類(計算法)</param>\r
64                 /// <returns></returns>\r
65                 public static string HashValueFor(string file, HashValueType type)\r
66                 {\r
67                         using (FileStream fs = new FileStream(file, FileMode.Open)) {\r
68                                 return HashValue.HashValueFor(fs, type);\r
69                         }\r
70                 }\r
71                 \r
72                 /// <summary>\r
73                 /// ストリーム入力からハッシュを計算する\r
74                 /// </summary>\r
75                 /// <param name="stream">ストリーム入力</param>\r
76                 /// <param name="type">ハッシュの種類(計算法)</param>\r
77                 /// <returns>ハッシュ値</returns>\r
78                 public static string HashValueFor(Stream stream, HashValueType type)\r
79                 {\r
80                         byte[] hash;\r
81                         \r
82                         switch (type) {\r
83                                 case HashValueType.SIZE:\r
84                                         return stream.Length.ToString();\r
85                                 case HashValueType.MD5SUM:\r
86                                         hash = MD5.Create().ComputeHash(stream);\r
87                                         break;\r
88                                 case HashValueType.SHA1SUM:\r
89                                         hash = SHA1.Create().ComputeHash(stream);\r
90                                         break;\r
91                                 case HashValueType.SHA256SUM:\r
92                                         hash = SHA256.Create().ComputeHash(stream);\r
93                                         break;\r
94                                 case HashValueType.SHA512SUM:\r
95                                         hash = SHA512.Create().ComputeHash(stream);\r
96                                         break;\r
97                                 default:\r
98                                         throw new NotSupportedException(string.Format("Hash type {0} does not supported", type));\r
99                         }\r
100                         \r
101                         return BitConverter.ToString(hash).Replace("-",string.Empty);\r
102                 }\r
103         }\r
104 }\r