FrmCoder: Use TextCoderFactory for all, instead of hardcoded values.

This commit is contained in:
2018-03-10 23:30:18 +01:00
parent 99afc29d07
commit 609de77d34
8 changed files with 54 additions and 164 deletions

View File

@@ -2,6 +2,8 @@
{ {
public interface ITextCoder public interface ITextCoder
{ {
bool NeedsKey { get; }
string Encode(string input, string key); string Encode(string input, string key);
string Decode(string input, string key); string Decode(string input, string key);
} }

View File

@@ -5,21 +5,19 @@ namespace VAR.Toolbox.Code
{ {
public class TextCoderBase64Ascii : ITextCoder public class TextCoderBase64Ascii : ITextCoder
{ {
public bool NeedsKey { get { return false; } }
public string Decode(string input, string key) public string Decode(string input, string key)
{ {
byte[] encodedDataAsBytes byte[] encodedDataAsBytes = Convert.FromBase64String(input);
= Convert.FromBase64String(input); string returnValue = Encoding.ASCII.GetString(encodedDataAsBytes);
string returnValue =
Encoding.ASCII.GetString(encodedDataAsBytes);
return returnValue; return returnValue;
} }
public string Encode(string input, string key) public string Encode(string input, string key)
{ {
byte[] toEncodeAsBytes byte[] toEncodeAsBytes = Encoding.ASCII.GetBytes(input);
= Encoding.ASCII.GetBytes(input); string returnValue = Convert.ToBase64String(toEncodeAsBytes);
string returnValue
= Convert.ToBase64String(toEncodeAsBytes);
return returnValue; return returnValue;
} }
} }

View File

@@ -5,21 +5,19 @@ namespace VAR.Toolbox.Code
{ {
public class TextCoderBase64Utf8 : ITextCoder public class TextCoderBase64Utf8 : ITextCoder
{ {
public bool NeedsKey { get { return false; } }
public string Decode(string input, string key) public string Decode(string input, string key)
{ {
byte[] encodedDataAsBytes byte[] encodedDataAsBytes = Convert.FromBase64String(input);
= Convert.FromBase64String(input); string returnValue = Encoding.UTF8.GetString(encodedDataAsBytes);
string returnValue =
Encoding.UTF8.GetString(encodedDataAsBytes);
return returnValue; return returnValue;
} }
public string Encode(string input, string key) public string Encode(string input, string key)
{ {
byte[] toEncodeAsBytes byte[] toEncodeAsBytes = Encoding.UTF8.GetBytes(input);
= Encoding.UTF8.GetBytes(input); string returnValue = Convert.ToBase64String(toEncodeAsBytes);
string returnValue
= Convert.ToBase64String(toEncodeAsBytes);
return returnValue; return returnValue;
} }
} }

View File

@@ -0,0 +1,28 @@
using System;
namespace VAR.Toolbox.Code
{
public class TextCoderFactory
{
public static string[] GetSupportedCoders()
{
return new string[] {
"Base64Ascii",
"Base64Utf8",
};
}
public static ITextCoder CreateFromName(string name)
{
if (name == "Base64Ascii")
{
return new TextCoderBase64Ascii();
}
if (name == "Base64Utf8")
{
return new TextCoderBase64Utf8();
}
throw new NotImplementedException(string.Format("Cant create ITextCoder with this name: {0}", name));
}
}
}

View File

@@ -57,7 +57,7 @@
// btnDecode // btnDecode
// //
this.btnDecode.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left))); this.btnDecode.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.btnDecode.Location = new System.Drawing.Point(146, 393); this.btnDecode.Location = new System.Drawing.Point(147, 390);
this.btnDecode.Name = "btnDecode"; this.btnDecode.Name = "btnDecode";
this.btnDecode.Size = new System.Drawing.Size(63, 23); this.btnDecode.Size = new System.Drawing.Size(63, 23);
this.btnDecode.TabIndex = 1; this.btnDecode.TabIndex = 1;
@@ -118,8 +118,8 @@
this.cboCode.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left))); this.cboCode.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.cboCode.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; this.cboCode.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.cboCode.FormattingEnabled = true; this.cboCode.FormattingEnabled = true;
this.cboCode.Location = new System.Drawing.Point(12, 396); this.cboCode.Location = new System.Drawing.Point(11, 393);
this.cboCode.Margin = new System.Windows.Forms.Padding(2, 2, 2, 2); this.cboCode.Margin = new System.Windows.Forms.Padding(2);
this.cboCode.Name = "cboCode"; this.cboCode.Name = "cboCode";
this.cboCode.Size = new System.Drawing.Size(131, 21); this.cboCode.Size = new System.Drawing.Size(131, 21);
this.cboCode.TabIndex = 4; this.cboCode.TabIndex = 4;
@@ -128,7 +128,7 @@
// btnEncode // btnEncode
// //
this.btnEncode.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left))); this.btnEncode.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.btnEncode.Location = new System.Drawing.Point(214, 393); this.btnEncode.Location = new System.Drawing.Point(216, 390);
this.btnEncode.Name = "btnEncode"; this.btnEncode.Name = "btnEncode";
this.btnEncode.Size = new System.Drawing.Size(63, 23); this.btnEncode.Size = new System.Drawing.Size(63, 23);
this.btnEncode.TabIndex = 3; this.btnEncode.TabIndex = 3;
@@ -139,7 +139,7 @@
// btnSwap // btnSwap
// //
this.btnSwap.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left))); this.btnSwap.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.btnSwap.Location = new System.Drawing.Point(3, 393); this.btnSwap.Location = new System.Drawing.Point(7, 390);
this.btnSwap.Name = "btnSwap"; this.btnSwap.Name = "btnSwap";
this.btnSwap.Size = new System.Drawing.Size(63, 23); this.btnSwap.Size = new System.Drawing.Size(63, 23);
this.btnSwap.TabIndex = 4; this.btnSwap.TabIndex = 4;

View File

@@ -10,15 +10,13 @@ namespace VAR.Toolbox.UI
{ {
InitializeComponent(); InitializeComponent();
cboCode.Items.Add("Base64Ascii"); cboCode.Items.AddRange(TextCoderFactory.GetSupportedCoders());
cboCode.Items.Add("Base64Utf8"); cboCode.SelectedIndex = 1;
cboCode.SelectedItem = "Base64Ascii";
} }
private ITextCoder _coder = null; private ITextCoder _coder = null;
private void btnDecodeBase64_Click(object sender, EventArgs e) private void btnDecodeBase64_Click(object sender, EventArgs e)
{ {
string output = string.Empty; string output = string.Empty;
@@ -32,7 +30,7 @@ namespace VAR.Toolbox.UI
} }
txtOutput.Text = output; txtOutput.Text = output;
} }
private void btnEncodeBase64_Click(object sender, EventArgs e) private void btnEncodeBase64_Click(object sender, EventArgs e)
{ {
string output = string.Empty; string output = string.Empty;
@@ -57,18 +55,8 @@ namespace VAR.Toolbox.UI
private void cboCode_SelectedIndexChanged(object sender, EventArgs e) private void cboCode_SelectedIndexChanged(object sender, EventArgs e)
{ {
string code = (string)cboCode.SelectedItem; string code = (string)cboCode.SelectedItem;
if(code == "Base64Ascii") _coder = TextCoderFactory.CreateFromName(code);
{ txtKey.Enabled = _coder.NeedsKey;
txtKey.Enabled = false;
_coder = new TextCoderBase64Ascii();
return;
}
if (code == "Base64Utf8")
{
txtKey.Enabled = false;
_coder = new TextCoderBase64Utf8();
return;
}
} }
} }
} }

View File

@@ -1,120 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
Version 2.0
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
<value>[base64 mime encoded serialized .NET Framework object]</value>
</data>
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>
There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="metadata">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="name" use="required" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="assembly">
<xsd:complexType>
<xsd:attribute name="alias" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
</root>

View File

@@ -95,6 +95,7 @@
<Compile Include="Code\ProxyCmdExecutorDummy.cs" /> <Compile Include="Code\ProxyCmdExecutorDummy.cs" />
<Compile Include="Code\ProxyCmdExecutorThroughSQLServer.cs" /> <Compile Include="Code\ProxyCmdExecutorThroughSQLServer.cs" />
<Compile Include="Code\Screenshooter.cs" /> <Compile Include="Code\Screenshooter.cs" />
<Compile Include="Code\TextCoderFactory.cs" />
<Compile Include="Code\User32.cs" /> <Compile Include="Code\User32.cs" />
<Compile Include="Code\Webcam.cs" /> <Compile Include="Code\Webcam.cs" />
<Compile Include="Code\Win32API.cs" /> <Compile Include="Code\Win32API.cs" />
@@ -185,11 +186,6 @@
<Content Include="Images\toolbox.svg" /> <Content Include="Images\toolbox.svg" />
<Content Include="Toolbox.ico" /> <Content Include="Toolbox.ico" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<EmbeddedResource Include="UI\FrmCoder.resx">
<DependentUpon>FrmCoder.cs</DependentUpon>
</EmbeddedResource>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it. <!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets. Other similar extension points exist, see Microsoft.Common.targets.