-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added support for Sha256 hasing and Base64 conversion of RowKey
- Loading branch information
Showing
16 changed files
with
220 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
CoreHelpers.WindowsAzure.Storage.Table.Abstractions/nVirtualValueEncoding.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
using System; | ||
namespace CoreHelpers.WindowsAzure.Storage.Table | ||
{ | ||
public enum nVirtualValueEncoding | ||
{ | ||
None, | ||
Base64, | ||
Sha256 | ||
} | ||
} | ||
|
113 changes: 113 additions & 0 deletions
113
CoreHelpers.WindowsAzure.Storage.Table.Tests/ITS24VirtualRowKey.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
using System; | ||
using CoreHelpers.WindowsAzure.Storage.Table.Attributes; | ||
using CoreHelpers.WindowsAzure.Storage.Table.Serialization; | ||
using CoreHelpers.WindowsAzure.Storage.Table.Tests.Extensions; | ||
using CoreHelpers.WindowsAzure.Storage.Table.Tests.Models; | ||
|
||
namespace CoreHelpers.WindowsAzure.Storage.Table.Tests | ||
{ | ||
[Storable(Tablename = "VirtualRowKeyNone")] | ||
[VirtualRowKey("{{RK}}")] | ||
public class VirtualRowKeyNone | ||
{ | ||
[PartitionKey] | ||
public string PK { get; set; } = String.Empty; | ||
|
||
public string RK { get; set; } = String.Empty; | ||
} | ||
|
||
[Storable(Tablename = "VirtualRowKeyNone")] | ||
[VirtualRowKey("{{RK}}-{{RK2}}")] | ||
public class VirtualRowKeyCombinedNone: VirtualRowKeyNone | ||
{ | ||
public string RK2 { get; set; } = String.Empty; | ||
} | ||
|
||
[Storable(Tablename = "VirtualRowKeyNone")] | ||
[VirtualRowKey("{{RK}}", nVirtualValueEncoding.Base64)] | ||
public class VirtualRowKeyBase64 : VirtualRowKeyNone | ||
{} | ||
|
||
[Storable(Tablename = "VirtualRowKeyNone")] | ||
[VirtualRowKey("{{RK}}", nVirtualValueEncoding.Sha256)] | ||
public class VirtualRowKeySha256: VirtualRowKeyNone | ||
{ } | ||
|
||
public class ITS24VirtualRowKey | ||
{ | ||
private readonly IStorageContext _rootContext; | ||
|
||
public ITS24VirtualRowKey(IStorageContext context) | ||
{ | ||
_rootContext = context; | ||
} | ||
|
||
[Fact] | ||
public void VerifyVirtualRowKeyNoneEncoding() | ||
{ | ||
using (var scp = _rootContext.CreateChildContext()) | ||
{ | ||
// set the tablename context | ||
scp.SetTableContext(); | ||
|
||
// configure the entity mapper | ||
scp.AddAttributeMapper<VirtualRowKeyNone>(); | ||
|
||
// check the entity | ||
var entity = TableEntityDynamic.ToEntity<VirtualRowKeyNone>(new VirtualRowKeyNone() { PK = "P01", RK = "R01" }, scp); | ||
Assert.Equal("R01", entity.RowKey); | ||
} | ||
} | ||
|
||
[Fact] | ||
public void VerifyVirtualRowKeyNoneEncodingCombined() | ||
{ | ||
using (var scp = _rootContext.CreateChildContext()) | ||
{ | ||
// set the tablename context | ||
scp.SetTableContext(); | ||
|
||
// configure the entity mapper | ||
scp.AddAttributeMapper<VirtualRowKeyCombinedNone>(); | ||
|
||
// check the entity | ||
var entity = TableEntityDynamic.ToEntity<VirtualRowKeyCombinedNone>(new VirtualRowKeyCombinedNone() { PK = "P01", RK = "R01", RK2 = "CMB" }, scp); | ||
Assert.Equal("R01-CMB", entity.RowKey); | ||
} | ||
} | ||
|
||
[Fact] | ||
public void VerifyVirtualRowKeyBase64Encoding() | ||
{ | ||
using (var scp = _rootContext.CreateChildContext()) | ||
{ | ||
// set the tablename context | ||
scp.SetTableContext(); | ||
|
||
// configure the entity mapper | ||
scp.AddAttributeMapper<VirtualRowKeyBase64>(); | ||
|
||
// check the entity | ||
var entity = TableEntityDynamic.ToEntity<VirtualRowKeyBase64>(new VirtualRowKeyBase64() { PK = "P01", RK = "R01" }, scp); | ||
Assert.Equal("UjAx", entity.RowKey); | ||
} | ||
} | ||
|
||
[Fact] | ||
public void VerifyVirtualRowKeySha256Encoding() | ||
{ | ||
using (var scp = _rootContext.CreateChildContext()) | ||
{ | ||
// set the tablename context | ||
scp.SetTableContext(); | ||
|
||
// configure the entity mapper | ||
scp.AddAttributeMapper<VirtualRowKeySha256>(); | ||
|
||
// check the entity | ||
var entity = TableEntityDynamic.ToEntity<VirtualRowKeySha256>(new VirtualRowKeySha256() { PK = "P01", RK = "R01" }, scp); | ||
Assert.Equal("e0a64b0b6d837fa4edc328ab9ddea0e3e7e0e4f715304c1d6bf3d0adc9d5292a", entity.RowKey); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 7 additions & 3 deletions
10
CoreHelpers.WindowsAzure.Storage.Table/Attributes/VirtualRowKeyAttribute.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,18 @@ | ||
using System; | ||
using CoreHelpers.WindowsAzure.Storage.Table; | ||
|
||
namespace CoreHelpers.WindowsAzure.Storage.Table.Attributes | ||
{ | ||
{ | ||
[AttributeUsage(AttributeTargets.Class)] | ||
public class VirtualRowKeyAttribute : Attribute | ||
{ | ||
public string RowKeyFormat { get; set; } | ||
|
||
public VirtualRowKeyAttribute(string RowKeyFormat) { | ||
|
||
public nVirtualValueEncoding Encoding { get; set; } | ||
|
||
public VirtualRowKeyAttribute(string RowKeyFormat, nVirtualValueEncoding Encoding = nVirtualValueEncoding.None) { | ||
this.RowKeyFormat = RowKeyFormat; | ||
this.Encoding = Encoding; | ||
} | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
CoreHelpers.WindowsAzure.Storage.Table/Extensions/StringExternsions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
using System; | ||
using System.Text; | ||
|
||
namespace CoreHelpers.WindowsAzure.Storage.Table.Extensions | ||
{ | ||
public static class StringExternsions | ||
{ | ||
public static string ToBase64(this string plainText) | ||
{ | ||
var plainTextBytes = System.Text.Encoding.UTF8.GetBytes(plainText); | ||
return System.Convert.ToBase64String(plainTextBytes); | ||
} | ||
|
||
public static string ToSha256(this string value) | ||
{ | ||
// convert the string | ||
byte[] toBytes = Encoding.ASCII.GetBytes(value); | ||
|
||
// generate the SHA1 key | ||
var sha256 = global::System.Security.Cryptography.SHA256.Create(); | ||
var hash = sha256.ComputeHash(toBytes); | ||
|
||
// done | ||
return GenerateHexStringFromBytes(hash); | ||
} | ||
|
||
private static string GenerateHexStringFromBytes(byte[] bytes) | ||
{ | ||
var sb = new StringBuilder(); | ||
foreach (byte b in bytes) | ||
{ | ||
var hex = b.ToString("x2"); | ||
sb.Append(hex); | ||
} | ||
return sb.ToString(); | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 0 additions & 1 deletion
1
CoreHelpers.WindowsAzure.Storage.Table/Internal/StorageContextQueryWithFilter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 0 additions & 1 deletion
1
CoreHelpers.WindowsAzure.Storage.Table/Internal/StorageContextQueryWithPartitionKey.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 0 additions & 1 deletion
1
CoreHelpers.WindowsAzure.Storage.Table/Internal/StorageContextQueryWithRowKey.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters