-
Notifications
You must be signed in to change notification settings - Fork 0
/
ApiAppREST.cs
241 lines (198 loc) · 11.2 KB
/
ApiAppREST.cs
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Security.Cryptography;
using System.Text;
namespace Chigusa.AzureHelper
{
/// <summary>
/// REST 向けユーティリティー
/// </summary>
public class ApiAppREST
{
/// <summary>
/// サーバーと共通のキー(自作)
/// </summary>
readonly string HashKey = "";
/// <summary>
/// エンドポイント
/// </summary>
readonly public string EndPoint = "";
/// <summary>
/// バージョン
/// </summary>
public const string ChigusaVersion = "2019-08-21";
/// <summary>
/// キーサイズ
/// </summary>
const int KeySize = 2048;
/// <summary>
/// XML形式の公開鍵
/// </summary>
const string accessPublicKey = "<RSAKeyValue><Modulus>qC+Br1QZDbrScpITTya4vBKDzDkmNR9ud5ZpL9xkn2M6x989Ffh7MKTZPh+qpKux2obJ8UFfTUKK/rhtWTwCV4BdwAeqdPKyHNKETV/xCNa7TuFZ+WHjXa7V5it3HQL372Okb1se5Gq6x0aQ96QQ64I+SEsa+ZdIFO5gQ3R9+G9Hutv/WWOnBgAdkFV/mMkLw7hoSHgZJG6vHO6PvVJRQraAMSVtMnfoD9bdPXyFylBzMHAYda3Y0KFk3ZXAysFkGb92Z63LtUbHqtlfVwSBYt/s0vEphZFNgafVyCnzr/KJzOgTAqPXcyBouPEl3On2a45Bk7YY2G5OtIXOU0dHWw==</Modulus><Exponent>AQAB</Exponent></RSAKeyValue>";
/// <summary>
/// XML形式の公開鍵
/// <para/>送受信用
/// </summary>
const string clientPublicKey = "<RSAKeyValue><Modulus>6PWU2ZkwP7QGIPCA3tYbzTWYOe4bkfScwdQxqNrrQVZ5QOLZlXGpBIhYoSyeOD8inH3KsTILsfjBKMovFYmunvq6g1vHdkDB6Eyl4N1SIa/nrL1c8QR6GAAfbipHpFOwLxoC292k6+VshzoM2xiZllxGw/p4PMvDoA2pFq3jV3gl0RFNKKUbKFZKPvmqie8Z2LKe0n3uw2FXFHWqcJUMrgv28bUlu7VNt+Ou4kTTBNNMPsfOhlZqg/fb0fHGoLGffLW3ZTWSNNH8qiceAP3IVLJRSPHmyzDUE92rqxdCPhrmCiapm9z0rleTH1ZJi1ox1uWg87KfJMQJMQcpvxP44Q==</Modulus><Exponent>AQAB</Exponent></RSAKeyValue>";
/// <summary>
/// XML形式の秘密鍵
/// <para/>送受信用
/// </summary>
const string clientPrivateKey = "<RSAKeyValue><Modulus>6PWU2ZkwP7QGIPCA3tYbzTWYOe4bkfScwdQxqNrrQVZ5QOLZlXGpBIhYoSyeOD8inH3KsTILsfjBKMovFYmunvq6g1vHdkDB6Eyl4N1SIa/nrL1c8QR6GAAfbipHpFOwLxoC292k6+VshzoM2xiZllxGw/p4PMvDoA2pFq3jV3gl0RFNKKUbKFZKPvmqie8Z2LKe0n3uw2FXFHWqcJUMrgv28bUlu7VNt+Ou4kTTBNNMPsfOhlZqg/fb0fHGoLGffLW3ZTWSNNH8qiceAP3IVLJRSPHmyzDUE92rqxdCPhrmCiapm9z0rleTH1ZJi1ox1uWg87KfJMQJMQcp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M9Y4qFOW2Dkww09zRyhMaoVXgr0cXeSEgvZLRoDMoaWn5YOjP+x5EKMy15UkCMRF0xVfL8LBe56QLBHMYGtRUxrpJCq2E/O5pJnDi+cVJh+7+MHEPndrR6SjkLM3H6+yF+gWVJ/sI9I1eNC4XiWt4zlwsR6sw35TxqIK34N8+vA8dSwzuYN0p/Yowl6VEe4ymV6tvKQ==</D></RSAKeyValue>";
/// <summary>
/// コンストラクタ
/// </summary>
/// <param name="hashKey">サーバーとのハッシュキー</param>
/// <param name="endPoint">サーバーのエンドポイント</param>
public ApiAppREST(string hashKey, string endPoint)
{
HashKey = hashKey;
EndPoint = endPoint;
}
/// <summary>
/// 権限付与ヘッダーの作成
/// </summary>
/// <param name="canonicalizedString">正規化された文字列</param>
/// <returns>ヘッダー文字列</returns>
private String CreateAuthorizationHeader(String canonicalizedString, string commonId)
{
if (String.IsNullOrEmpty(canonicalizedString))
{
throw new ArgumentNullException("canonicalizedString");
}
String signature = CreateHmacSignature(canonicalizedString, Convert.FromBase64String(HashKey));
String authorizationHeader = String.Format(CultureInfo.InvariantCulture, "{0} {1}", commonId, signature);
return authorizationHeader;
}
/// <summary>
/// ハッシュキーで署名文字列を作成する
/// </summary>
/// <param name="unsignedString">署名前の文字列</param>
/// <param name="key">ハッシュキー</param>
/// <returns>署名済み文字列</returns>
private static String CreateHmacSignature(String unsignedString, Byte[] key)
{
if (String.IsNullOrEmpty(unsignedString))
{
throw new ArgumentNullException("unsignedString");
}
if (key == null)
{
throw new ArgumentNullException("key");
}
Byte[] dataToHmac = System.Text.Encoding.UTF8.GetBytes(unsignedString);
using (HMACSHA256 hmacSha256 = new HMACSHA256(key))
{
return Convert.ToBase64String(hmacSha256.ComputeHash(dataToHmac));
}
}
/// <summary>
/// 要求する情報から権限付与ヘッダーの作成
/// </summary>
/// <param name="requestMethod">メソッド</param>
/// <param name="requestDate">日付</param>
/// <param name="requestVersion">バージョン</param>
/// <param name="requestAccountName">アカウント名</param>
/// <returns>権限付与ヘッダー</returns>
public string GetAuthorizationParameter(string requestMethod, string requestDate, string requestVersion, string requestAccountName, string addCanonicalizedHeaders = "")
{
if (requestMethod == null || requestDate == null || requestVersion == null || requestAccountName == null)
throw new ArgumentNullException("args");
String canonicalizedHeaders = String.Format("x-chigusa-date:{0}\nx-chigusa-version:{1}", requestDate, requestVersion);
if (!String.IsNullOrEmpty(addCanonicalizedHeaders))
canonicalizedHeaders += "\n" + addCanonicalizedHeaders;
String canonicalizedResource = String.Format("/{0}", requestAccountName);
String stringToSign = String.Format("{0}\n{1}\n{2}", requestMethod, canonicalizedHeaders, canonicalizedResource);
String authorizationHeader = CreateAuthorizationHeader(stringToSign, requestAccountName);
return authorizationHeader;
}
#region Encrypt and Decrypt
/// <summary>
/// 公開鍵を使って文字列を暗号化する
/// </summary>
/// <param name="str">暗号化する文字列</param>
/// <returns>暗号化された文字列</returns>
public static string Encrypt(string str)
{
try
{
//RSACryptoServiceProviderオブジェクトの作成
var rsa = new System.Security.Cryptography.RSACryptoServiceProvider(KeySize);
//公開鍵を指定
rsa.FromXmlString(accessPublicKey);
//暗号化する文字列をバイト配列に
byte[] data = System.Text.Encoding.UTF8.GetBytes(str);
// データサイズのチェック
if (data.Length > rsa.KeySize / 8 - 11)
throw new Exception();
//暗号化する
//(XP以降の場合のみ2項目にTrueを指定し、OAEPパディングを使用できる)
// RSACryptoServiceProvider.KeySize プロパティで取得できるキーサイズが1024ビットだとすると、暗号化されるデータの最大長は、 1024/8-11=117バイト となります。
byte[] encryptedData = rsa.Encrypt(data, false);
//Base64で結果を文字列に変換
return System.Convert.ToBase64String(encryptedData);
}
catch (Exception)
{
}
return null;
}
/// <summary>
/// 公開鍵を使って文字列を暗号化する
/// </summary>
/// <param name="str">暗号化する文字列</param>
/// <returns>暗号化された文字列</returns>
public static string ClientEncrypt(string str)
{
try
{
//RSACryptoServiceProviderオブジェクトの作成
var rsa = new System.Security.Cryptography.RSACryptoServiceProvider(KeySize);
//公開鍵を指定
rsa.FromXmlString(clientPublicKey);
//暗号化する文字列をバイト配列に
byte[] data = System.Text.Encoding.UTF8.GetBytes(str);
// データサイズのチェック
if (data.Length > rsa.KeySize / 8 - 11)
throw new Exception();
//暗号化する
//(XP以降の場合のみ2項目にTrueを指定し、OAEPパディングを使用できる)
// RSACryptoServiceProvider.KeySize プロパティで取得できるキーサイズが1024ビットだとすると、暗号化されるデータの最大長は、 1024/8-11=117バイト となります。
byte[] encryptedData = rsa.Encrypt(data, false);
//Base64で結果を文字列に変換
return System.Convert.ToBase64String(encryptedData);
}
catch (Exception)
{
}
return null;
}
/// <summary>
/// 秘密鍵を使って文字列を復号化する
/// </summary>
/// <param name="str">Encryptメソッドにより暗号化された文字列</param>
/// <returns>復号化された文字列</returns>
public static string ClientDecrypt(string str)
{
try
{
//RSACryptoServiceProviderオブジェクトの作成
var rsa = new System.Security.Cryptography.RSACryptoServiceProvider(KeySize);
//秘密鍵を指定
rsa.FromXmlString(clientPrivateKey);
//復号化する文字列をバイト配列に
byte[] data = System.Convert.FromBase64String(str);
//復号化する
byte[] decryptedData = rsa.Decrypt(data, false);
//結果を文字列に変換
return System.Text.Encoding.UTF8.GetString(decryptedData);
}
catch (Exception)
{
}
return null;
}
#endregion
}
}