-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathG2OAlgorithms.cs
More file actions
71 lines (61 loc) · 2.97 KB
/
G2OAlgorithms.cs
File metadata and controls
71 lines (61 loc) · 2.97 KB
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
namespace G2O
{
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using SignAlgorithm = System.Func<byte[], byte[], byte[], byte[]>;
public static class G2OAlgorithms
{
public static string ComputeSignatureValue(int version, string key, string data, string urlpath)
{
if (version < 1 || 5 < version) { throw new ArgumentException(string.Format("Cannot find signature algorithm for version {0}", version)); }
byte[] keyBytes = key.EncodedAsASCII();
byte[] dataBytes = data.EncodedAsASCII();
byte[] urlpathBytes = urlpath.EncodedAsASCII();
SignAlgorithm algorithm = algorithms[version];
byte[] signatureBytes = algorithm(keyBytes, dataBytes, urlpathBytes);
string signature = Convert.ToBase64String(signatureBytes);
return signature;
}
private static readonly ReadOnlyDictionary<int, SignAlgorithm> algorithms = new ReadOnlyDictionary<int, SignAlgorithm>(new Dictionary<int, SignAlgorithm>
{
//{ 1, (key, data, urlpath) => MD5(msg: new[] { key, data, urlpath }) },
//{ 2, (key, data, urlpath) => MD5(msg: new[] { key, MD5(new[] { key, data, urlpath }) }) },
//{ 3, (key, data, urlpath) => HMAC_Sign(alg: "HMACMD5", key: key, msg: new[] { data, urlpath }) },
//{ 4, (key, data, urlpath) => HMAC_Sign(alg: "HMACSHA1", key: key, msg: new[] { data, urlpath }) },
//{ 5, (key, data, urlpath) => HMAC_Sign(alg: "HMACSHA256", key: key, msg: new[] { data, urlpath }) }
{ 1, (key, data, urlpath) => MD5(key, data, urlpath) },
{ 2, (key, data, urlpath) => MD5(key, MD5(key, data, urlpath)) },
{ 3, (key, data, urlpath) => HMAC_Sign("HMACMD5", key, data, urlpath) },
{ 4, (key, data, urlpath) => HMAC_Sign("HMACSHA1", key, data, urlpath) },
{ 5, (key, data, urlpath) => HMAC_Sign("HMACSHA256", key, data, urlpath) }
});
private static byte[] EncodedAsASCII(this string str)
{
return global::System.Text.Encoding.ASCII.GetBytes(str);
}
private static byte[] HMAC_Sign(string alg, byte[] key, params byte[][] msg)
{
var f = global::System.Security.Cryptography.HMAC.Create(alg);
f.Key = key;
return f.ComputeHash(Concat(msg));
}
private static byte[] MD5(params byte[][] msg)
{
var f = System.Security.Cryptography.HashAlgorithm.Create("MD5");
return f.ComputeHash(Concat(msg));
}
private static byte[] Concat(params byte[][] m)
{
byte[] rv = new byte[m.Sum(a => a.Length)];
int offset = 0;
foreach (byte[] array in m)
{
System.Buffer.BlockCopy(array, 0, rv, offset, array.Length);
offset += array.Length;
}
return rv;
}
}
}