Skip to content

Commit 1d24688

Browse files
committed
Convert to nF
Re-comment out un-necessary classes
1 parent 98feb4b commit 1d24688

15 files changed

+860
-195
lines changed

Tests/Aws.IoTCore.Devices.UnitTests.nfproj

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
<Compile Include="ShadowTests.cs" />
3333
<Compile Include="Properties\AssemblyInfo.cs" />
3434
<Compile Include="ShadowJsonMessageExamples.cs" />
35+
<Compile Include="SignatureVersion4Tests.cs" />
36+
<Compile Include="SortExtensionTests.cs" />
3537
</ItemGroup>
3638
<ItemGroup>
3739
<None Include="packages.config" />
@@ -69,6 +71,9 @@
6971
<Private>True</Private>
7072
</Reference>
7173
</ItemGroup>
74+
<ItemGroup>
75+
<ProjectReference Include="..\nanoFramework.Aws.IoTCore.Devices\nanoFramework.Aws.IoTCore.Devices.nfproj" />
76+
</ItemGroup>
7277
<Import Project="$(NanoFrameworkProjectSystemPath)NFProjectSystem.CSharp.targets" Condition="Exists('$(NanoFrameworkProjectSystemPath)NFProjectSystem.CSharp.targets')" />
7378
<!-- MANUAL UPDATE HERE -->
7479
<ProjectExtensions>

Tests/SignatureVersion4Tests.cs

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
//
2+
// Copyright (c) .NET Foundation and Contributors
3+
// See LICENSE file in the project root for full license information.
4+
//
5+
6+
using nanoFramework.TestFramework;
7+
using System;
8+
using System.Collections;
9+
using nanoFramework.Aws.SignatureVersion4;
10+
11+
namespace nanoFramework.Aws.SignatureVersion4.Tests
12+
{
13+
// https://github.com/inspiration/uniface-aws-sigv4
14+
// https://github.com/FantasticFiasco/aws-signature-version-4
15+
// https://stackoverflow.com/questions/28966075/creating-a-hmac-signature-for-aws-rest-query-in-c-sharp
16+
[TestClass]
17+
public class SignatureVersion4Tests
18+
{
19+
[TestMethod]
20+
public void check_CanonicalizeHeaderNames_when_order_is_canonical_already() // using an already ordered list
21+
{
22+
//// Given that: SigV4 seems to require a header that is canonicalized
23+
//var headers = new Hashtable();
24+
//headers.Add("aTest", "atestval");
25+
//headers.Add("Atest2", "Atest2val");
26+
//headers.Add("btest", "btestval");
27+
//headers.Add("ctest", "ctestval");
28+
29+
//Console.WriteLine(SignerBase.CanonicalizeHeaderNames(headers));
30+
31+
// Expect "atest;atest2;btest;ctest"
32+
}
33+
34+
[TestMethod]
35+
public void check_CanonicalizeHeaderNames_when_order_is_not_sorted()
36+
{
37+
//var headers = new Hashtable();
38+
//headers.Add("aTest", "atestval");
39+
//headers.Add("ctest", "ctestval");
40+
//headers.Add("btest", "btestval");
41+
//headers.Add("Atest2", "Atest2val");
42+
43+
//Console.WriteLine(CanonicalizeHeaderNames(headers));
44+
45+
// Expect "atest;atest2;btest;ctest"
46+
}
47+
48+
49+
[TestMethod]
50+
public void check_CanonicalizeHeaders_when_order_is_not_sorted()
51+
{
52+
//var headers = new Hashtable();
53+
//headers.Add("aTest", "atestval");
54+
//headers.Add("ctest", "ctestval");
55+
//headers.Add("btest", "btestval");
56+
//headers.Add("Atest2", "Atest2val");
57+
//Console.WriteLine(CanonicalizeHeaders(headers));
58+
59+
// Expect "atest:atestval\natest2:Atest2val\nbtest:btestval\nctest:ctestval\n"
60+
}
61+
62+
}
63+
}

Tests/SortExtensionTests.cs

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
//
2+
// Copyright (c) .NET Foundation and Contributors
3+
// See LICENSE file in the project root for full license information.
4+
//
5+
6+
using nanoFramework.TestFramework;
7+
using System;
8+
using System.Collections;
9+
10+
namespace nanoFramework.Aws.SignatureVersion4.Tests
11+
{
12+
[TestClass]
13+
public class SortExtensionTests
14+
{
15+
[TestMethod]
16+
public void check_ArrayList_Sort_Ordinal_when_order_is_canonical_already()
17+
{
18+
var list = new ArrayList();
19+
list.Add("aTest");
20+
list.Add("Atest2");
21+
list.Add("btest");
22+
list.Add("ctest");
23+
24+
list.Sort(StringComparer.Ordinal);
25+
26+
foreach (var item in list)
27+
{
28+
Console.WriteLine(item.ToString());
29+
}
30+
31+
32+
33+
// Expect "atest;atest2;btest;ctest"
34+
}
35+
36+
[TestMethod]
37+
public void check_check_ArrayList_Sort_Ordinal_when_order_is_not_sorted()
38+
{
39+
var list = new ArrayList();
40+
list.Add("aTest");
41+
list.Add("ctest");
42+
list.Add("btest");
43+
list.Add("Atest2");
44+
45+
list.Sort(StringComparer.Ordinal);
46+
47+
foreach (var item in list)
48+
{
49+
Console.WriteLine(item.ToString());
50+
}
51+
52+
// Expect "atest;atest2;btest;ctest"
53+
}
54+
55+
56+
[TestMethod]
57+
public void check_ArrayList_Sort_OrdinalIgnoreCase_when_order_is_canonical_already()
58+
{
59+
var list = new ArrayList();
60+
list.Add("aTest");
61+
list.Add("Atest2");
62+
list.Add("btest");
63+
list.Add("ctest");
64+
65+
list.Sort(StringComparer.OrdinalIgnoreCase);
66+
67+
foreach (var item in list)
68+
{
69+
Console.WriteLine(item.ToString());
70+
}
71+
72+
73+
74+
// Expect "atest;atest2;btest;ctest"
75+
}
76+
77+
[TestMethod]
78+
public void check_check_ArrayList_Sort_OrdinalIgnoreCase_when_order_is_not_sorted()
79+
{
80+
var list = new ArrayList();
81+
list.Add("aTest");
82+
list.Add("ctest");
83+
list.Add("btest");
84+
list.Add("Atest2");
85+
86+
list.Sort(StringComparer.OrdinalIgnoreCase);
87+
88+
foreach (var item in list)
89+
{
90+
Console.WriteLine(item.ToString());
91+
}
92+
93+
// Expect "atest;atest2;btest;ctest"
94+
}
95+
96+
}
97+
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
//
2+
// Copyright (c) .NET Foundation and Contributors
3+
// See LICENSE file in the project root for full license information.
4+
//
5+
6+
//
7+
// Originally by Elze Kool see http://www.microframework.nl/2009/09/05/shahmac-digest-class/
8+
// Adjustments by Laurent Ellerbach [email protected] 2021/05/31
9+
//
10+
11+
namespace System.Security.Cryptography
12+
{
13+
using System;
14+
15+
/// <summary>
16+
/// Computes a Hash-based Message Authentication Code (HMAC) by using the System.Security.Cryptography.SHA256
17+
/// hash function.
18+
/// </summary>
19+
public class HMACSHA256
20+
{
21+
/// <summary>
22+
/// Gets or sets the key to use in the HMAC calculation.
23+
/// </summary>
24+
public byte[] Key { get; set; }
25+
26+
/// <summary>
27+
/// Initializes a new instance of the System.Security.Cryptography.HMACSHA256 class
28+
/// with the specified key data.
29+
/// </summary>
30+
/// <param name="key">The secret key for System.Security.Cryptography.HMACSHA256 encryption. The key
31+
/// can be any length. However, the recommended size is 64 bytes max.</param>
32+
public HMACSHA256(byte[] key)
33+
{
34+
Key = key;
35+
}
36+
37+
/// <summary>
38+
/// Computes the hash value for the specified byte array.
39+
/// </summary>
40+
/// <param name="buffer">The input to compute the hash code for.</param>
41+
/// <returns>The computed hash code.</returns>
42+
/// <exception cref="ArgumentNullException">buffer is null.</exception>
43+
public byte[] ComputeHash(byte[] buffer)
44+
{
45+
if (buffer == null)
46+
{
47+
throw new ArgumentNullException();
48+
}
49+
50+
return ComputeHMACSHA256(Key, buffer);
51+
}
52+
53+
/// <summary>
54+
/// Compute HMAC SHA-256
55+
/// </summary>
56+
/// <param name = "secret">Secret</param>
57+
/// <param name = "value">Password</param>
58+
/// <returns>32 byte HMAC_SHA256</returns>
59+
private static byte[] ComputeHMACSHA256(byte[] secret, byte[] value)
60+
{
61+
// Create two arrays, bi and bo
62+
var bi = new byte[64 + value.Length];
63+
var bo = new byte[64 + 32];
64+
65+
// Copy secret to both arrays
66+
Array.Copy(secret, bi, secret.Length);
67+
Array.Copy(secret, bo, secret.Length);
68+
69+
for (var i = 0; i < 64; i++)
70+
{
71+
// Xor bi with 0x36
72+
bi[i] = (byte)(bi[i] ^ 0x36);
73+
74+
// Xor bo with 0x5c
75+
bo[i] = (byte)(bo[i] ^ 0x5c);
76+
}
77+
78+
// Append value to bi
79+
Array.Copy(value, 0, bi, 64, value.Length);
80+
81+
var sha256 = SHA256.Create();
82+
// Append SHA256(bi) to bo
83+
var sha_bi = sha256.ComputeHash(bi);
84+
Array.Copy(sha_bi, 0, bo, 64, 32);
85+
86+
// Return SHA256(bo)
87+
return sha256.ComputeHash(bo);
88+
}
89+
}
90+
}

0 commit comments

Comments
 (0)