Browse Source

Common: Add XmlNodeExt, DisposableObject, Secure -> nuget v.1.0.0.3

Dalibor Votruba 6 years ago
parent
commit
354e4be196

+ 1 - 0
.gitignore

@@ -36,3 +36,4 @@ packages/
 *.7z
 *.xsb
 *.pjb
+*.nupkg

+ 6 - 1
Common/Common.csproj

@@ -41,9 +41,12 @@
     <Reference Include="System.Xml" />
   </ItemGroup>
   <ItemGroup>
+    <Compile Include="Extensions\XmlNodeExt.cs" />
     <Compile Include="Extensions\XmlReaderExt.cs" />
     <Compile Include="Extensions\XPathNavigatorExt.cs" />
+    <Compile Include="Object\DisposableObject.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />
+    <Compile Include="Security\Secure.cs" />
   </ItemGroup>
   <ItemGroup>
     <Folder Include="Helpers\" />
@@ -52,7 +55,9 @@
     <Content Include="ReleaseNote.txt" />
   </ItemGroup>
   <ItemGroup>
-    <None Include="QDR.FND.Common.nuspec" />
+    <None Include="QDR.FND.Common.nuspec">
+      <SubType>Designer</SubType>
+    </None>
   </ItemGroup>
   <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
 </Project>

+ 3 - 0
Common/Common.sln.DotSettings

@@ -0,0 +1,3 @@
+<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
+	<s:Boolean x:Key="/Default/UserDictionary/Words/=encryptor/@EntryIndexedValue">True</s:Boolean>
+	<s:Boolean x:Key="/Default/UserDictionary/Words/=Quadarax/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>

+ 35 - 0
Common/Extensions/XmlNodeExt.cs

@@ -0,0 +1,35 @@
+using System;
+using System.ComponentModel;
+using System.Xml;
+
+namespace Quadarax.Foundation.Common.Extensions
+{
+    public static class XmlNodeExt
+    {
+        public static T GetAttribute<T>(this XmlNode node, string sAttributeName, bool bMandatory, T oDefaultValue)
+        {
+            if (string.IsNullOrEmpty(node.GetAttribute(sAttributeName)))
+            {
+                if (bMandatory)
+                    throw new Exception(
+                        $"Missing mandatory attribute '{sAttributeName}' in element '{node.Name}'");
+                return oDefaultValue;
+            }
+            return (T) TypeDescriptor.GetConverter(typeof(T)).ConvertFromString(node.GetAttribute(sAttributeName));
+        }
+
+        public static string GetAttribute(this XmlNode node, string attributeName)
+        {
+            if (node?.Attributes == null)
+                return string.Empty;
+
+            foreach (XmlAttribute attr in node.Attributes)
+            {
+                if (attr.Name == attributeName)
+                    return attr.Value;
+            }
+
+            return string.Empty;
+        }
+    }
+}

+ 19 - 0
Common/Object/DisposableObject.cs

@@ -0,0 +1,19 @@
+using System;
+
+namespace Quadarax.Foundation.Common.Object
+{
+    public abstract class DisposableObject : IDisposable
+    {
+        private bool _isDisposing;
+
+        public void Dispose()
+        {
+            if (_isDisposing)
+                return;
+            _isDisposing = true;
+            OnDisposing();
+        }
+
+        protected abstract void OnDisposing();
+    }
+}

+ 2 - 2
Common/QDR.FND.Common.nuspec

@@ -2,7 +2,7 @@
 <package>
   <metadata>
     <id>Quadarax.Foundation.Common</id>
-    <version>1.0.0.1</version>
+    <version>1.0.0.3</version>
     <title>Quadarax.Foundation.Common</title>
     <authors>Dalibor Votruba, Quadarax</authors>
     <owners>Dalibor Votruba</owners>
@@ -11,7 +11,7 @@
     <requireLicenseAcceptance>false</requireLicenseAcceptance>
     <description>Implementations of simple patterns and misc. extensions.</description>
     <releaseNotes>
-      12.1.2019 v.1.0.0.1 - New: Add XpathNavigator, XmlReader extensions
+      2.12.2019 v.1.0.0.3 - New: Add XmlNode extension
     </releaseNotes>
     <copyright>Copyright (c) 2019 Quadarax</copyright>
     <tags>quadarax, foundarion, common, qdr, fnd, library, extensions</tags>

BIN
Common/Quadarax.Foundation.Common.1.0.0.nupkg


+ 9 - 1
Common/ReleaseNote.txt

@@ -1,3 +1,11 @@
-12.1.2019 v.1.0.0.1
+2.12.2019 v.1.0.0.3
+New
+ - Add XmlNode extension
+
+2.12.2019 v.1.0.0.2
+New
+ - Add DisposableObject, Secure (to generate crypted strings)
+
+1.12.2019 v.1.0.0.1
 New
  - Add XpathNavigator, XmlReader extensions

+ 63 - 0
Common/Security/Secure.cs

@@ -0,0 +1,63 @@
+using System;
+using System.IO;
+using System.Security.Cryptography;
+using System.Text;
+
+namespace Quadarax.Foundation.Common.Security
+{
+    public static class Secure
+    {
+        static string _passwordHash = "OmMC:7i%+o.MlIeg`kRcv:R3haPyx|#vuM,G,oB1n=IBUG'!M6";
+        static string _saltKey = "vo:Y:pWYcS\"*m@^6R\\O#$eSg!,23H\\R8KfIi\\u1IED91TE0J#~";
+        static string _vIKey = "@!PYn,b=t4\\%`Am`z!ubwE1H2Ek1?*R&,6cF:f1mZJ1dxB&DZH";
+
+
+        public static void SetProperties(string passwordHash, string saltKey, string vIKey)
+        {
+            _passwordHash = passwordHash;
+            _saltKey = saltKey;
+            _vIKey = vIKey;
+        }
+
+        public static string Encrypt(string plainText)
+        {
+            var plainTextBytes = Encoding.UTF8.GetBytes(plainText);
+
+            var keyBytes = new Rfc2898DeriveBytes(_passwordHash, Encoding.ASCII.GetBytes(_saltKey)).GetBytes(256 / 8);
+            var symmetricKey = new RijndaelManaged() { Mode = CipherMode.CBC, Padding = PaddingMode.Zeros };
+            var encryptor = symmetricKey.CreateEncryptor(keyBytes, Encoding.ASCII.GetBytes(_vIKey));
+			
+            byte[] cipherTextBytes;
+
+            using (var memoryStream = new MemoryStream())
+            {
+                using (var cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write))
+                {
+                    cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length);
+                    cryptoStream.FlushFinalBlock();
+                    cipherTextBytes = memoryStream.ToArray();
+                    cryptoStream.Close();
+                }
+                memoryStream.Close();
+            }
+            return Convert.ToBase64String(cipherTextBytes);
+        }
+
+        public static string Decrypt(string encryptedText)
+        {
+            var cipherTextBytes = Convert.FromBase64String(encryptedText);
+            var keyBytes = new Rfc2898DeriveBytes(_passwordHash, Encoding.ASCII.GetBytes(_saltKey)).GetBytes(256 / 8);
+            var symmetricKey = new RijndaelManaged() { Mode = CipherMode.CBC, Padding = PaddingMode.None };
+
+            var decryptor = symmetricKey.CreateDecryptor(keyBytes, Encoding.ASCII.GetBytes(_vIKey));
+            var memoryStream = new MemoryStream(cipherTextBytes);
+            var cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read);
+            byte[] plainTextBytes = new byte[cipherTextBytes.Length];
+
+            int decryptedByteCount = cryptoStream.Read(plainTextBytes, 0, plainTextBytes.Length);
+            memoryStream.Close();
+            cryptoStream.Close();
+            return Encoding.UTF8.GetString(plainTextBytes, 0, decryptedByteCount).TrimEnd("\0".ToCharArray());
+        }
+    }
+}