-
Notifications
You must be signed in to change notification settings - Fork 69
/
SharedKeySignature.cs
51 lines (46 loc) · 2.2 KB
/
SharedKeySignature.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
// Copyright (c) Barry Dorrans. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Net.Http;
using System.Security.Cryptography;
using System.Text;
using Microsoft.AspNetCore.Http;
namespace idunno.Authentication.SharedKey
{
internal static class SharedKeySignature
{
/// <summary>
/// Calculates a SHA256 HMAC for the <seealso cref="request"/>.
/// </summary>
/// <param name="request">The request to calculate a hash for.</param>
/// <param name="key">The shared key used to sign the request.</param>
/// <returns>A SHA256 HMAC of the canonicalized request.</returns>
internal static byte[] Calculate(HttpRequestMessage request, byte[] key)
{
var canonicalizedRequest = request.CanonicalizeHeaders() + request.CanonicalizeResource();
return CalculateHmac256(key, canonicalizedRequest);
}
/// <summary>
/// Calculates a SHA256 HMAC for the <seealso cref="request"/>.
/// </summary>
/// <param name="request">The request to calculate a hash for.</param>
/// <param name="key">The shared key used to sign the request.</param>
/// <returns>A SHA256 HMAC of the canonicalized request.</returns>
internal static byte[] Calculate(HttpRequest request, byte[] key)
{
var canonicalizedRequest = request.CanonicalizeHeaders() + request.CanonicalizeResource();
return CalculateHmac256(key, canonicalizedRequest);
}
/// <summary>
/// Calculates a SHA256 HMAC for the plain text, using the specified key.
/// </summary>
/// <param name="key">The key to use in the HMAC.</param>
/// <param name="plainText">The plain text to calculate the HMAC over.</param>
/// <returns>A SHA256 HMAC</returns>
private static byte[] CalculateHmac256(byte[] key, string plainText)
{
using HashAlgorithm hashAlgorithm = new HMACSHA256(key);
byte[] messageBuffer = new UTF8Encoding(false).GetBytes(plainText);
return hashAlgorithm.ComputeHash(messageBuffer);
}
}
}