Skip to content

Commit adc3ff0

Browse files
committed
Added support for Sha256 hasing and Base64 conversion of RowKey
1 parent 67478eb commit adc3ff0

File tree

16 files changed

+220
-17
lines changed

16 files changed

+220
-17
lines changed

CoreHelpers.WindowsAzure.Storage.Table.Abstractions/IStorageContext.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using System.Linq;
55
using System.Reflection;
66
using System.Threading.Tasks;
7-
using CoreHelpers.WindowsAzure.Storage.Table.Abstractions;
87

98
namespace CoreHelpers.WindowsAzure.Storage.Table
109
{
@@ -27,6 +26,8 @@ public interface IStorageContext : IDisposable
2726

2827
void AddEntityMapper(Type entityType, String partitionKeyFormat, String rowKeyFormat, String tableName);
2928

29+
void AddEntityMapper(Type entityType, String partitionKeyFormat, String rowKeyFormat, nVirtualValueEncoding rowKeyEncoding, String tableName);
30+
3031
IStorageContext CreateChildContext();
3132

3233
IStorageContext EnableAutoCreateTable();

CoreHelpers.WindowsAzure.Storage.Table.Abstractions/IStorageContextQuery.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
using System.Linq;
44
using System.Threading.Tasks;
55

6-
namespace CoreHelpers.WindowsAzure.Storage.Table.Abstractions
6+
namespace CoreHelpers.WindowsAzure.Storage.Table
77
{
88
public interface IStorageContextQueryNow<T>
99
{
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using System;
2+
namespace CoreHelpers.WindowsAzure.Storage.Table
3+
{
4+
public enum nVirtualValueEncoding
5+
{
6+
None,
7+
Base64,
8+
Sha256
9+
}
10+
}
11+
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
using System;
2+
using CoreHelpers.WindowsAzure.Storage.Table.Attributes;
3+
using CoreHelpers.WindowsAzure.Storage.Table.Serialization;
4+
using CoreHelpers.WindowsAzure.Storage.Table.Tests.Extensions;
5+
using CoreHelpers.WindowsAzure.Storage.Table.Tests.Models;
6+
7+
namespace CoreHelpers.WindowsAzure.Storage.Table.Tests
8+
{
9+
[Storable(Tablename = "VirtualRowKeyNone")]
10+
[VirtualRowKey("{{RK}}")]
11+
public class VirtualRowKeyNone
12+
{
13+
[PartitionKey]
14+
public string PK { get; set; } = String.Empty;
15+
16+
public string RK { get; set; } = String.Empty;
17+
}
18+
19+
[Storable(Tablename = "VirtualRowKeyNone")]
20+
[VirtualRowKey("{{RK}}-{{RK2}}")]
21+
public class VirtualRowKeyCombinedNone: VirtualRowKeyNone
22+
{
23+
public string RK2 { get; set; } = String.Empty;
24+
}
25+
26+
[Storable(Tablename = "VirtualRowKeyNone")]
27+
[VirtualRowKey("{{RK}}", nVirtualValueEncoding.Base64)]
28+
public class VirtualRowKeyBase64 : VirtualRowKeyNone
29+
{}
30+
31+
[Storable(Tablename = "VirtualRowKeyNone")]
32+
[VirtualRowKey("{{RK}}", nVirtualValueEncoding.Sha256)]
33+
public class VirtualRowKeySha256: VirtualRowKeyNone
34+
{ }
35+
36+
public class ITS24VirtualRowKey
37+
{
38+
private readonly IStorageContext _rootContext;
39+
40+
public ITS24VirtualRowKey(IStorageContext context)
41+
{
42+
_rootContext = context;
43+
}
44+
45+
[Fact]
46+
public void VerifyVirtualRowKeyNoneEncoding()
47+
{
48+
using (var scp = _rootContext.CreateChildContext())
49+
{
50+
// set the tablename context
51+
scp.SetTableContext();
52+
53+
// configure the entity mapper
54+
scp.AddAttributeMapper<VirtualRowKeyNone>();
55+
56+
// check the entity
57+
var entity = TableEntityDynamic.ToEntity<VirtualRowKeyNone>(new VirtualRowKeyNone() { PK = "P01", RK = "R01" }, scp);
58+
Assert.Equal("R01", entity.RowKey);
59+
}
60+
}
61+
62+
[Fact]
63+
public void VerifyVirtualRowKeyNoneEncodingCombined()
64+
{
65+
using (var scp = _rootContext.CreateChildContext())
66+
{
67+
// set the tablename context
68+
scp.SetTableContext();
69+
70+
// configure the entity mapper
71+
scp.AddAttributeMapper<VirtualRowKeyCombinedNone>();
72+
73+
// check the entity
74+
var entity = TableEntityDynamic.ToEntity<VirtualRowKeyCombinedNone>(new VirtualRowKeyCombinedNone() { PK = "P01", RK = "R01", RK2 = "CMB" }, scp);
75+
Assert.Equal("R01-CMB", entity.RowKey);
76+
}
77+
}
78+
79+
[Fact]
80+
public void VerifyVirtualRowKeyBase64Encoding()
81+
{
82+
using (var scp = _rootContext.CreateChildContext())
83+
{
84+
// set the tablename context
85+
scp.SetTableContext();
86+
87+
// configure the entity mapper
88+
scp.AddAttributeMapper<VirtualRowKeyBase64>();
89+
90+
// check the entity
91+
var entity = TableEntityDynamic.ToEntity<VirtualRowKeyBase64>(new VirtualRowKeyBase64() { PK = "P01", RK = "R01" }, scp);
92+
Assert.Equal("UjAx", entity.RowKey);
93+
}
94+
}
95+
96+
[Fact]
97+
public void VerifyVirtualRowKeySha256Encoding()
98+
{
99+
using (var scp = _rootContext.CreateChildContext())
100+
{
101+
// set the tablename context
102+
scp.SetTableContext();
103+
104+
// configure the entity mapper
105+
scp.AddAttributeMapper<VirtualRowKeySha256>();
106+
107+
// check the entity
108+
var entity = TableEntityDynamic.ToEntity<VirtualRowKeySha256>(new VirtualRowKeySha256() { PK = "P01", RK = "R01" }, scp);
109+
Assert.Equal("e0a64b0b6d837fa4edc328ab9ddea0e3e7e0e4f715304c1d6bf3d0adc9d5292a", entity.RowKey);
110+
}
111+
}
112+
}
113+
}

CoreHelpers.WindowsAzure.Storage.Table/AssemblyInfo.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Reflection;
3+
using System.Runtime.CompilerServices;
34

45
[assembly: AssemblyCompany("Core Helpers")]
56
[assembly: AssemblyProduct("WindowsAzure.Storage.Table")]
@@ -12,4 +13,6 @@
1213
[assembly: AssemblyConfiguration("Debug")]
1314
#else
1415
[assembly: AssemblyConfiguration("Release")]
15-
#endif
16+
#endif
17+
18+
[assembly: InternalsVisibleTo("CoreHelpers.WindowsAzure.Storage.Table.Tests")]
Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
using System;
2+
using CoreHelpers.WindowsAzure.Storage.Table;
23

34
namespace CoreHelpers.WindowsAzure.Storage.Table.Attributes
4-
{
5+
{
56
[AttributeUsage(AttributeTargets.Class)]
67
public class VirtualRowKeyAttribute : Attribute
78
{
89
public string RowKeyFormat { get; set; }
9-
10-
public VirtualRowKeyAttribute(string RowKeyFormat) {
10+
11+
public nVirtualValueEncoding Encoding { get; set; }
12+
13+
public VirtualRowKeyAttribute(string RowKeyFormat, nVirtualValueEncoding Encoding = nVirtualValueEncoding.None) {
1114
this.RowKeyFormat = RowKeyFormat;
15+
this.Encoding = Encoding;
1216
}
1317
}
1418
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using System;
2+
using System.Text;
3+
4+
namespace CoreHelpers.WindowsAzure.Storage.Table.Extensions
5+
{
6+
public static class StringExternsions
7+
{
8+
public static string ToBase64(this string plainText)
9+
{
10+
var plainTextBytes = System.Text.Encoding.UTF8.GetBytes(plainText);
11+
return System.Convert.ToBase64String(plainTextBytes);
12+
}
13+
14+
public static string ToSha256(this string value)
15+
{
16+
// convert the string
17+
byte[] toBytes = Encoding.ASCII.GetBytes(value);
18+
19+
// generate the SHA1 key
20+
var sha256 = global::System.Security.Cryptography.SHA256.Create();
21+
var hash = sha256.ComputeHash(toBytes);
22+
23+
// done
24+
return GenerateHexStringFromBytes(hash);
25+
}
26+
27+
private static string GenerateHexStringFromBytes(byte[] bytes)
28+
{
29+
var sb = new StringBuilder();
30+
foreach (byte b in bytes)
31+
{
32+
var hex = b.ToString("x2");
33+
sb.Append(hex);
34+
}
35+
return sb.ToString();
36+
}
37+
}
38+
}
39+

CoreHelpers.WindowsAzure.Storage.Table/Internal/StorageContextQueryNow.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
using System.Threading;
88
using System.Threading.Tasks;
99
using Azure.Data.Tables;
10-
using CoreHelpers.WindowsAzure.Storage.Table.Abstractions;
1110
using CoreHelpers.WindowsAzure.Storage.Table.Serialization;
1211

1312
namespace CoreHelpers.WindowsAzure.Storage.Table.Internal

CoreHelpers.WindowsAzure.Storage.Table/Internal/StorageContextQueryWithFilter.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System;
22
using System.Collections.Generic;
3-
using CoreHelpers.WindowsAzure.Storage.Table.Abstractions;
43
using CoreHelpers.WindowsAzure.Storage.Table.Serialization;
54

65
namespace CoreHelpers.WindowsAzure.Storage.Table.Internal

CoreHelpers.WindowsAzure.Storage.Table/Internal/StorageContextQueryWithPartitionKey.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System;
2-
using CoreHelpers.WindowsAzure.Storage.Table.Abstractions;
32

43
namespace CoreHelpers.WindowsAzure.Storage.Table.Internal
54
{

0 commit comments

Comments
 (0)