-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathSignatureHelper.cs
32 lines (29 loc) · 1.09 KB
/
SignatureHelper.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
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
#if NETFX_CORE
using Windows.Security.Cryptography.Core;
using Windows.Security.Cryptography;
using System.Runtime.InteropServices.WindowsRuntime;
#endif
namespace RESTClient {
public static class SignatureHelper {
public static string Sign(byte[] key, string stringToSign) {
#if NETFX_CORE
MacAlgorithmProvider provider = MacAlgorithmProvider.OpenAlgorithm(MacAlgorithmNames.HmacSha256);
CryptographicHash hash = provider.CreateHash(key.AsBuffer());
hash.Append(CryptographicBuffer.ConvertStringToBinary(stringToSign, BinaryStringEncoding.Utf8));
return CryptographicBuffer.EncodeToBase64String( hash.GetValueAndReset() );
#else
var hmac = new HMACSHA256();
hmac.Key = key;
byte[] sig = hmac.ComputeHash(Encoding.UTF8.GetBytes(stringToSign));
return Convert.ToBase64String(sig);
#endif
}
}
}