Commit 7cebd219 authored by Michael Herndon's avatar Michael Herndon

PROTO: update solution, add prototype value converters based on EF Core

parent 9520abae
......@@ -4,44 +4,18 @@ VisualStudioVersion = 15.0.26124.0
MinimumVisualStudioVersion = 15.0.26124.0
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{DB042499-08A3-4C34-9231-1372043915F2}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NerdyMishka.Akashic", "src\Akashic\NerdyMishka.Akashic.csproj", "{805C4596-CFAF-4E06-B4D0-EF228A75EEBF}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NerdyMishka.KeePass.Core", "src\KeePass.Core\NerdyMishka.KeePass.Core.csproj", "{D441CDAD-3F47-4DC2-8B61-C731E458B89F}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NerdyMishka.KeePass.Xml", "src\KeePass.Xml\NerdyMishka.KeePass.Xml.csproj", "{CE4A7015-CF9F-4FCD-8AF4-75FA3C5A5B39}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Extensions", "Extensions", "{6444BF0F-2EED-404A-AC00-CC756F4F4ED0}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NerdyMishka.Extensions.Hosting.Console", "src\Extensions\Hosting.Console\NerdyMishka.Extensions.Hosting.Console.csproj", "{523ED03C-C365-4363-8429-CC33A24CAD0A}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Bcl", "Bcl", "{7ACBB370-996C-43C7-A01B-CC5A562B2133}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NerdyMishka.Bits", "src\Bcl\Bits\NerdyMishka.Bits.csproj", "{8C25561B-BCE1-4746-B732-EF70856B6664}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NerdyMishka.GoDark", "src\Bcl\GoDark\NerdyMishka.GoDark.csproj", "{BD89CEC1-050D-478F-9A0D-A888D4F916D4}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NerdyMishka.Proto", "src\Bcl\Proto\NerdyMishka.Proto.csproj", "{4D85D802-8434-4976-9D82-C106371C3A46}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NerdyMishka.MagicSatchel", "src\Bcl\MagicSatchel\NerdyMishka.MagicSatchel.csproj", "{794FA5B5-628B-4A64-A071-63A914BA3991}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{DA32C16D-7292-4EEC-90C5-59EC66CC018D}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Extensions", "Extensions", "{9F8D3C61-2523-47EC-8B4C-F4CDD1A615BD}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NerdyMishka.Extensions.Hosting.Console.Tests", "test\Extensions\Hosting.Console.Tests\NerdyMishka.Extensions.Hosting.Console.Tests.csproj", "{A569086E-AA35-4F74-ABC6-27B5F0F25417}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Apps", "Apps", "{9DDAA835-01A8-4B41-AA9B-B8A17D30BC1A}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DankInventory.Data", "src\Apps\DankInventory.Data\DankInventory.Data.csproj", "{1DA7100D-EF90-4946-B089-44BC5F0516D6}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DankInventory", "src\Apps\DankInventory\DankInventory.csproj", "{7F47EC8E-D1AF-4E7C-A981-9F3E34269627}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Bcl", "Bcl", "{60D01D6E-9F34-4289-8142-934780802BF6}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NerdyMishka.Proto.Tests", "test\Bcl\Proto.Tests\NerdyMishka.Proto.Tests.csproj", "{16B7581C-A756-4F25-89E2-9516C72F118B}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NerdyMishka.Flex", "src\Extensions\Flex\NerdyMishka.Flex.csproj", "{9AF0788A-2589-4E68-BBA3-DB65D178F97A}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NerdyMishka.Extensions.Proto.Flex", "src\Extensions\Proto.Flex\NerdyMishka.Extensions.Proto.Flex.csproj", "{AC4EF416-86E3-4B93-8A74-2FAF08C69DA9}"
EndProject
Global
......@@ -50,58 +24,14 @@ Global
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{805C4596-CFAF-4E06-B4D0-EF228A75EEBF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{805C4596-CFAF-4E06-B4D0-EF228A75EEBF}.Debug|Any CPU.Build.0 = Debug|Any CPU
{805C4596-CFAF-4E06-B4D0-EF228A75EEBF}.Release|Any CPU.ActiveCfg = Release|Any CPU
{805C4596-CFAF-4E06-B4D0-EF228A75EEBF}.Release|Any CPU.Build.0 = Release|Any CPU
{D441CDAD-3F47-4DC2-8B61-C731E458B89F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D441CDAD-3F47-4DC2-8B61-C731E458B89F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D441CDAD-3F47-4DC2-8B61-C731E458B89F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D441CDAD-3F47-4DC2-8B61-C731E458B89F}.Release|Any CPU.Build.0 = Release|Any CPU
{CE4A7015-CF9F-4FCD-8AF4-75FA3C5A5B39}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{CE4A7015-CF9F-4FCD-8AF4-75FA3C5A5B39}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CE4A7015-CF9F-4FCD-8AF4-75FA3C5A5B39}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CE4A7015-CF9F-4FCD-8AF4-75FA3C5A5B39}.Release|Any CPU.Build.0 = Release|Any CPU
{523ED03C-C365-4363-8429-CC33A24CAD0A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{523ED03C-C365-4363-8429-CC33A24CAD0A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{523ED03C-C365-4363-8429-CC33A24CAD0A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{523ED03C-C365-4363-8429-CC33A24CAD0A}.Release|Any CPU.Build.0 = Release|Any CPU
{8C25561B-BCE1-4746-B732-EF70856B6664}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8C25561B-BCE1-4746-B732-EF70856B6664}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8C25561B-BCE1-4746-B732-EF70856B6664}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8C25561B-BCE1-4746-B732-EF70856B6664}.Release|Any CPU.Build.0 = Release|Any CPU
{BD89CEC1-050D-478F-9A0D-A888D4F916D4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{BD89CEC1-050D-478F-9A0D-A888D4F916D4}.Debug|Any CPU.Build.0 = Debug|Any CPU
{BD89CEC1-050D-478F-9A0D-A888D4F916D4}.Release|Any CPU.ActiveCfg = Release|Any CPU
{BD89CEC1-050D-478F-9A0D-A888D4F916D4}.Release|Any CPU.Build.0 = Release|Any CPU
{4D85D802-8434-4976-9D82-C106371C3A46}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4D85D802-8434-4976-9D82-C106371C3A46}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4D85D802-8434-4976-9D82-C106371C3A46}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4D85D802-8434-4976-9D82-C106371C3A46}.Release|Any CPU.Build.0 = Release|Any CPU
{794FA5B5-628B-4A64-A071-63A914BA3991}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{794FA5B5-628B-4A64-A071-63A914BA3991}.Debug|Any CPU.Build.0 = Debug|Any CPU
{794FA5B5-628B-4A64-A071-63A914BA3991}.Release|Any CPU.ActiveCfg = Release|Any CPU
{794FA5B5-628B-4A64-A071-63A914BA3991}.Release|Any CPU.Build.0 = Release|Any CPU
{A569086E-AA35-4F74-ABC6-27B5F0F25417}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A569086E-AA35-4F74-ABC6-27B5F0F25417}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A569086E-AA35-4F74-ABC6-27B5F0F25417}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A569086E-AA35-4F74-ABC6-27B5F0F25417}.Release|Any CPU.Build.0 = Release|Any CPU
{1DA7100D-EF90-4946-B089-44BC5F0516D6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1DA7100D-EF90-4946-B089-44BC5F0516D6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1DA7100D-EF90-4946-B089-44BC5F0516D6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1DA7100D-EF90-4946-B089-44BC5F0516D6}.Release|Any CPU.Build.0 = Release|Any CPU
{7F47EC8E-D1AF-4E7C-A981-9F3E34269627}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7F47EC8E-D1AF-4E7C-A981-9F3E34269627}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7F47EC8E-D1AF-4E7C-A981-9F3E34269627}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7F47EC8E-D1AF-4E7C-A981-9F3E34269627}.Release|Any CPU.Build.0 = Release|Any CPU
{16B7581C-A756-4F25-89E2-9516C72F118B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{16B7581C-A756-4F25-89E2-9516C72F118B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{16B7581C-A756-4F25-89E2-9516C72F118B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{16B7581C-A756-4F25-89E2-9516C72F118B}.Release|Any CPU.Build.0 = Release|Any CPU
{9AF0788A-2589-4E68-BBA3-DB65D178F97A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{9AF0788A-2589-4E68-BBA3-DB65D178F97A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9AF0788A-2589-4E68-BBA3-DB65D178F97A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9AF0788A-2589-4E68-BBA3-DB65D178F97A}.Release|Any CPU.Build.0 = Release|Any CPU
{AC4EF416-86E3-4B93-8A74-2FAF08C69DA9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{AC4EF416-86E3-4B93-8A74-2FAF08C69DA9}.Debug|Any CPU.Build.0 = Debug|Any CPU
{AC4EF416-86E3-4B93-8A74-2FAF08C69DA9}.Release|Any CPU.ActiveCfg = Release|Any CPU
......@@ -111,24 +41,11 @@ Global
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{805C4596-CFAF-4E06-B4D0-EF228A75EEBF} = {DB042499-08A3-4C34-9231-1372043915F2}
{D441CDAD-3F47-4DC2-8B61-C731E458B89F} = {DB042499-08A3-4C34-9231-1372043915F2}
{CE4A7015-CF9F-4FCD-8AF4-75FA3C5A5B39} = {DB042499-08A3-4C34-9231-1372043915F2}
{6444BF0F-2EED-404A-AC00-CC756F4F4ED0} = {DB042499-08A3-4C34-9231-1372043915F2}
{523ED03C-C365-4363-8429-CC33A24CAD0A} = {6444BF0F-2EED-404A-AC00-CC756F4F4ED0}
{7ACBB370-996C-43C7-A01B-CC5A562B2133} = {DB042499-08A3-4C34-9231-1372043915F2}
{8C25561B-BCE1-4746-B732-EF70856B6664} = {7ACBB370-996C-43C7-A01B-CC5A562B2133}
{BD89CEC1-050D-478F-9A0D-A888D4F916D4} = {7ACBB370-996C-43C7-A01B-CC5A562B2133}
{4D85D802-8434-4976-9D82-C106371C3A46} = {7ACBB370-996C-43C7-A01B-CC5A562B2133}
{794FA5B5-628B-4A64-A071-63A914BA3991} = {7ACBB370-996C-43C7-A01B-CC5A562B2133}
{9F8D3C61-2523-47EC-8B4C-F4CDD1A615BD} = {DA32C16D-7292-4EEC-90C5-59EC66CC018D}
{A569086E-AA35-4F74-ABC6-27B5F0F25417} = {9F8D3C61-2523-47EC-8B4C-F4CDD1A615BD}
{9DDAA835-01A8-4B41-AA9B-B8A17D30BC1A} = {DB042499-08A3-4C34-9231-1372043915F2}
{1DA7100D-EF90-4946-B089-44BC5F0516D6} = {9DDAA835-01A8-4B41-AA9B-B8A17D30BC1A}
{7F47EC8E-D1AF-4E7C-A981-9F3E34269627} = {9DDAA835-01A8-4B41-AA9B-B8A17D30BC1A}
{60D01D6E-9F34-4289-8142-934780802BF6} = {DA32C16D-7292-4EEC-90C5-59EC66CC018D}
{16B7581C-A756-4F25-89E2-9516C72F118B} = {60D01D6E-9F34-4289-8142-934780802BF6}
{9AF0788A-2589-4E68-BBA3-DB65D178F97A} = {6444BF0F-2EED-404A-AC00-CC756F4F4ED0}
{AC4EF416-86E3-4B93-8A74-2FAF08C69DA9} = {6444BF0F-2EED-404A-AC00-CC756F4F4ED0}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
......
## Gainz
## Notes
The project is in the process of being consolidated into fewer projects
to make it easier to test and work on under the "Proto" or prototype banner.
- ./src/Bcl/Proto
- ./src/Extensions/Proto.Flex
- ./test/Bcl/Proto.Tests
- ./text/Extensions/Proto.Flex.Tests
using System;
using System.Linq.Expressions;
namespace NerdyMishka.ComponentModel.ValueConversion
{
public class BoolToTwoValuesConverter<TTo> : ValueConverter<bool, TTo>
{
public BoolToTwoValuesConverter(
TTo falseValue,
TTo trueValue,
Expression<Func<TTo, bool>> fromProvider = null)
: base(ToProvider(falseValue, trueValue), fromProvider ?? ToBool(trueValue))
{
}
private static Expression<Func<bool, TTo>> ToProvider(TTo falseValue, TTo trueValue)
{
var param = Expression.Parameter(typeof(bool), "v");
return Expression.Lambda<Func<bool, TTo>>(
Expression.Condition(
param,
Expression.Constant(trueValue, typeof(TTo)),
Expression.Constant(falseValue, typeof(TTo))),
param);
}
private static Expression<Func<TTo, bool>> ToBool(TTo trueValue)
{
var param = Expression.Parameter(typeof(TTo), "v");
return Expression.Lambda<Func<TTo, bool>>(
Expression.Equal(
param,
Expression.Constant(trueValue, typeof(TTo))),
param);
}
}
}
\ No newline at end of file
using System;
namespace NerdyMishka.ComponentModel.ValueConversion
{
public class DateTimeToTicksConverter : ValueConverter<DateTime, long>
{
public DateTimeToTicksConverter() :base((from) => from.Ticks, (to) => new DateTime(to))
{
}
}
}
\ No newline at end of file
using System;
namespace NerdyMishka.ComponentModel.ValueConversion
{
public class DateTimeToUnixTimestampConverter : ValueConverter<DateTime, long>
{
public DateTimeToUnixTimestampConverter() :
base((from) => from.ToUnixTimeStamp(), (to) => to.FromUnixTimeStamp())
{
}
}
}
\ No newline at end of file
using System;
using System.Linq.Expressions;
namespace NerdyMishka.ComponentModel.ValueConversion
{
public class StringToGuidConverter: GuidAndStringConverter<string, Guid>
{
public StringToGuidConverter() : base(ToGuid(), ToString())
{
}
}
public class GuidToStringConverter : GuidAndStringConverter<Guid, string>
{
public GuidToStringConverter() : base(ToString(), ToGuid())
{
}
}
public abstract class GuidAndStringConverter<TFrom, TTo> : ValueConverter<TFrom, TTo>
{
public GuidAndStringConverter(
Expression<Func<TFrom, TTo>> convertToProviderExpression, Expression<Func<TTo, TFrom>> convertFromProviderExpression) : base(convertToProviderExpression, convertFromProviderExpression)
{
}
public static Expression<Func<string, Guid>> ToGuid() => (v) => v == null ? default : new Guid(v);
public static new Expression<Func<Guid, string>> ToString() => (v) => v.ToString("D");
}
}
\ No newline at end of file
using System;
namespace NerdyMishka.ComponentModel.ValueConversion
{
public class TimeSpanToTicksConverter : ValueConverter<TimeSpan, long>
{
public TimeSpanToTicksConverter() :base((from) => from.Ticks, (to) => new TimeSpan(to))
{
}
}
}
\ No newline at end of file
using System;
using System.ComponentModel;
using System.Linq.Expressions;
namespace NerdyMishka.ComponentModel.ValueConversion
{
public abstract class ValueConverter<TFrom, TTo> : ValueConverter
{
private Func<TFrom, TTo> convertTo;
private Func<TTo, TFrom> convertFrom;
public override Type FromType => typeof(TFrom);
public override Type ToType => typeof(TTo);
public ValueConverter(
Expression<Func<TFrom, TTo>> convertToProviderExpression,
Expression<Func<TTo, TFrom>> convertFromProviderExpression
)
{
this.convertTo = convertToProviderExpression.Compile();
this.convertFrom = convertFromProviderExpression.Compile();
}
public override bool CanConvertFrom(Type type)
{
return typeof(TFrom) == type;
}
public override bool CanConvertTo(Type type)
{
return typeof(TTo) == type;
}
public override object ConvertFrom(object value)
{
return this.convertFrom((TTo)value);
}
public override object ConvertTo(object value)
{
return this.convertTo((TFrom)value);
}
}
public abstract class ValueConverter
{
public virtual Type FromType { get; }
public virtual Type ToType { get; }
public abstract bool CanConvertFrom(Type type);
public abstract bool CanConvertTo(Type type);
public abstract object ConvertFrom(object value);
public abstract object ConvertTo(object value);
}
}
\ No newline at end of file
using System;
using System.Linq.Expressions;
using System.Security;
using System.Text;
using NerdyMishka.Security.Cryptography;
using NerdyMishka.Text;
namespace NerdyMishka.ComponentModel.ValueConversion
{
public class SecureStringEncryptionConverter : ValueEncryptionConverter<SecureString, string>
{
private Encoding encoding;
public SecureStringEncryptionConverter(
ISymmetricEncryptionProvider encryptionProvider = null,
Encoding encoding = null) : base(encryptionProvider, encoding)
{
this.encoding = encoding ?? Encodings.Utf8NoBom;
}
// string
public override object ConvertFrom(object value)
{
if(value == null)
return null;
var ss = (SecureString)value;
var bytes = ss.ToBytes(this.encoding);
return this.EncryptString((byte[])bytes);
}
// byte
public override object ConvertTo(object value)
{
if(value == null)
return null;
var encryptedValue = (string)value;
var bytes = this.Decrypt(encryptedValue);
var chars = this.encoding.GetChars(bytes);
var ss = new SecureString();
foreach(var c in chars)
ss.AppendChar(c);
bytes.Clear();
chars.Clear();
return ss;
}
}
public class StringEncryptionConverter : ValueEncryptionConverter<string, string>
{
public StringEncryptionConverter(
ISymmetricEncryptionProvider encryptionProvider = null,
Encoding encoding = null) : base(encryptionProvider, encoding)
{
}
// string
public override object ConvertFrom(object value)
{
if(value == null)
return null;
return this.EncryptString((string)value);
}
// byte
public override object ConvertTo(object value)
{
if(value == null)
return null;
return this.DecryptString((string)value);
}
}
public class StringToBytesEncryptionConverter : ValueEncryptionConverter<string, byte[]>
{
public StringToBytesEncryptionConverter(
ISymmetricEncryptionProvider encryptionProvider = null,
Encoding encoding = null) : base(encryptionProvider, encoding)
{
}
public override object ConvertFrom(object value)
{
if(value == null)
return null;
return this.Encrypt((string) value);
}
// byte
public override object ConvertTo(object value)
{
if(value == null)
return null;
return this.DecryptString((byte[])value);
}
}
public class BytesToStringEncryptionConverter : ValueEncryptionConverter<byte[], string>
{
public BytesToStringEncryptionConverter(
ISymmetricEncryptionProvider encryptionProvider = null,
Encoding encoding = null) : base(encryptionProvider, encoding)
{
}
public override object ConvertFrom(object value)
{
if(value == null)
return null;
return this.EncryptString((byte[])value);
}
// byte
public override object ConvertTo(object value)
{
if(value == null)
return null;
return this.Decrypt((string)value);
}
}
public class ByteEncryptionConverter : ValueEncryptionConverter<byte[], byte[]>
{
public ByteEncryptionConverter(
ISymmetricEncryptionProvider encryptionProvider = null,
Encoding encoding = null) : base(encryptionProvider, encoding)
{
}
// string
public override object ConvertFrom(object value)
{
if(value == null)
return null;
return this.Encrypt((byte[])value);
}
// byte
public override object ConvertTo(object value)
{
if(value == null)
return null;
return this.Decrypt((byte[])value);
}
}
public class ValueEncryptionConverter<TFrom, TTo> : ValueConverter
{
/// <summary>
/// Gets or sets the encryption provider for the
/// </summary>
/// <value></value>
public static ISymmetricEncryptionProvider EncryptionProvider { get; set; }
private ISymmetricEncryptionProvider encryptionProvider;
private Encoding encoding;
static ValueEncryptionConverter()
{
// default for testing.
var key = NerdyMishka.Text.Encodings.Utf8NoBom.GetBytes("[email protected]#dfa2-0daa 2 daax23 xwcxXcwe2");
EncryptionProvider = new SymmetricEncryptionProvider(new SymmetricEncryptionProviderOptions(){
Key = key,
Iterations = 20000
});
}
public ValueEncryptionConverter(
ISymmetricEncryptionProvider encryptionProvider = null,
Encoding encoding = null)
{
this.encryptionProvider = encryptionProvider ?? EncryptionProvider;
this.encoding = encoding ?? NerdyMishka.Text.Encodings.Utf8NoBom;
}
public string EncryptString(byte[] value)
{
return Convert.ToBase64String(
this.Encrypt(value));
}
public string EncryptString(string value)
{
return Convert.ToBase64String(
this.Encrypt(value));
}
public virtual byte[] Encrypt(byte[] value)
{
return EncryptionProvider.Encrypt(value);
}
public virtual byte[] Encrypt(string value)
{
var bytes = this.encoding.GetBytes(value);
return EncryptionProvider.Encrypt(bytes);
}
public virtual string DecryptString(string value)
{
var bytes = this.Decrypt(value);
return this.encoding.GetString(bytes);
}
public virtual string DecryptString(byte[] value)
{
var bytes = this.Decrypt(value);
return this.encoding.GetString(bytes);
}
public virtual byte[] Decrypt(byte[] value)
{
return EncryptionProvider.Decrypt(value);
}
public virtual byte[] Decrypt(string value)
{
var bytes = System.Convert.FromBase64String(value);
return EncryptionProvider.Decrypt(bytes);
}
public override bool CanConvertFrom(Type type)
{
return type == typeof(TFrom);
}
public override bool CanConvertTo(Type type)
{
return type == typeof(TTo);
}
public override object ConvertFrom(object value)
{
return null;
}
public override object ConvertTo(object value)
{
return null;
}
}
}
\ No newline at end of file
using System;
using System.Security;
using System.Text;
using NerdyMishka.Security.Cryptography;
namespace NerdyMishka.ComponentModel.ValueConversion
{
public class SecureStringHashConverter: ValueHashConverter<SecureString, string>
{
private Encoding encoding;
public SecureStringHashConverter(
IPasswordAuthenticator authenticator = null,
Encoding encoding = null)
:base(authenticator, encoding)
{
this.encoding = encoding ?? NerdyMishka.Text.Encodings.Utf8NoBom;
}
public override object ConvertFrom(object value)
{
if(value == null)
return null;
var ss = (SecureString)value;
var bytes = ss.ToBytes(this.encoding);
var hash = this.ComputeHashAsString((char[])value);
bytes.Clear();
return hash;
}
public override object ConvertTo(object value)
{
return value;
}
}
public class CharsHashConverter: ValueHashConverter<char[], string>
{
public CharsHashConverter(
IPasswordAuthenticator authenticator = null,
Encoding encoding = null)
:base(authenticator, encoding)
{
}
public override object ConvertFrom(object value)
{
if(value == null)
return null;
return this.ComputeHashAsString((char[])value);
}
public override object ConvertTo(object value)
{
return value;
}
}
public class BytesHashConverter: ValueHashConverter<byte[], string>
{
public BytesHashConverter(
IPasswordAuthenticator authenticator = null,
Encoding encoding = null)
:base(authenticator, encoding)
{