Skip to content

Commit 7d841bb

Browse files
stevenawnormj
authored andcommitted
pooled buffers for hmac signing and a test
1 parent adacf39 commit 7d841bb

File tree

5 files changed

+47
-7
lines changed

5 files changed

+47
-7
lines changed

sdk/src/Core/AWSSDK.Core.NetStandard.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,5 +74,6 @@
7474

7575
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
7676
<PackageReference Include="Microsoft.Bcl.AsyncInterfaces" Version="1.1.0" />
77+
<PackageReference Include="System.Buffers" Version="4.3.0" />
7778
</ItemGroup>
7879
</Project>

sdk/src/Core/Amazon.Util/CryptoUtil.cs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
using Amazon.Runtime.Internal.Util;
1717
using Amazon.Util.Internal;
1818
using AWSSDK.Runtime.Internal.Util;
19+
using System;
20+
using System.Buffers;
1921
using System.IO;
2022
using System.Security.Cryptography;
2123
using System.Text;
@@ -98,8 +100,21 @@ internal CryptoUtil()
98100
/// <returns>Computed hash code</returns>
99101
public string HMACSign(string data, string key, SigningAlgorithm algorithmName)
100102
{
101-
var binaryData = Encoding.UTF8.GetBytes(data);
102-
return HMACSign(binaryData, key, algorithmName);
103+
Encoding encoding = Encoding.UTF8;
104+
int maxSize = encoding.GetMaxByteCount(data.Length);
105+
byte[] buffer = ArrayPool<byte>.Shared.Rent(maxSize);
106+
107+
try
108+
{
109+
int size = encoding.GetBytes(data, buffer);
110+
ArraySegment<byte> segment = new ArraySegment<byte>(buffer, 0, size);
111+
112+
return HMACSign(segment, key, algorithmName);
113+
}
114+
finally
115+
{
116+
ArrayPool<byte>.Shared.Return(buffer);
117+
}
103118
}
104119

105120
/// <summary>

sdk/src/Core/Amazon.Util/_netstandard/CryptoUtil.netstandard.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,14 @@ public static partial class CryptoUtilFactory
3030
partial class CryptoUtil : ICryptoUtil
3131
{
3232
public string HMACSign(byte[] data, string key, SigningAlgorithm algorithmName)
33+
=> HMACSign(new ArraySegment<byte>(data, 0, data.Length), key, algorithmName);
34+
35+
private string HMACSign(ArraySegment<byte> data, string key, SigningAlgorithm algorithmName)
3336
{
3437
if (String.IsNullOrEmpty(key))
3538
throw new ArgumentNullException("key", "Please specify a Secret Signing Key.");
3639

37-
if (data == null || data.Length == 0)
40+
if (data == null || data.Count == 0)
3841
throw new ArgumentNullException("data", "Please specify data to sign.");
3942

4043
KeyedHashAlgorithm algorithm = CreateKeyedHashAlgorithm(algorithmName);
@@ -44,7 +47,7 @@ public string HMACSign(byte[] data, string key, SigningAlgorithm algorithmName)
4447
try
4548
{
4649
algorithm.Key = Encoding.UTF8.GetBytes(key);
47-
byte[] bytes = algorithm.ComputeHash(data);
50+
byte[] bytes = algorithm.ComputeHash(data.Array, data.Offset, data.Count);
4851
return Convert.ToBase64String(bytes);
4952
}
5053
finally

sdk/src/Core/Amazon.Util/_netstandard/Extensions.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
using System;
2-
using System.Collections.Generic;
32
using System.Globalization;
4-
using System.Linq;
5-
using System.Threading.Tasks;
3+
using System.Text;
64

75
namespace Amazon.Util
86
{
@@ -14,5 +12,10 @@ internal static string ToUpper(this String str, CultureInfo culture)
1412
throw new ArgumentException("The extension method ToUpper only works for invariant culture");
1513
return str.ToUpperInvariant();
1614
}
15+
16+
#if !NETCOREAPP3_1_OR_GREATER
17+
internal static int GetBytes(this Encoding encoding, string chars, byte[] bytes)
18+
=> encoding.GetBytes(chars, 0, chars.Length, bytes, 0);
19+
#endif
1720
}
1821
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using Amazon.Runtime;
2+
using Amazon.Util;
3+
using Xunit;
4+
5+
namespace UnitTests.NetStandard.Core
6+
{
7+
public class CryptoUtilTests
8+
{
9+
[Fact]
10+
[Trait("Category", "Core")]
11+
public void HMACSignFromString()
12+
{
13+
var actual = CryptoUtilFactory.CryptoInstance.HMACSign("HELLOWORLD", "HELLOKEY", SigningAlgorithm.HmacSHA256);
14+
15+
Assert.Equal("LH+6qR+N6WU91evFKfjq/6NqiVES5BD6z6EHYsDwOUE=", actual);
16+
}
17+
}
18+
}

0 commit comments

Comments
 (0)