Refactoring

pull/4/head
Ogoun 2 years ago
parent 99f11f87b6
commit b0a1f5ba54

@ -451,7 +451,7 @@ namespace PartitionFileStorageTest
}
catch (Exception ex)
{
Log.Error(ex, "FaultUncompressedReadTest");
}
}
}

@ -1,5 +1,6 @@
using System;
using System.Collections.Concurrent;
using ZeroLevel.Services.Network.Utils;
namespace TestApp
{
@ -7,9 +8,7 @@ namespace TestApp
{
private static void Main(string[] args)
{
var test = new ConcurrentDictionary<string, int>();
var v = test.GetOrAdd("sss", 1);
Console.ReadKey();
Console.WriteLine(Network.ExternalIP);
}
}
}

@ -50,16 +50,6 @@ namespace ZeroLevel.SqlServer
private void Initialize()
{
try
{
var perm = new SqlClientPermission(PermissionState.Unrestricted);
perm.Demand();
perm = null;
}
catch
{
throw new ApplicationException("No permission for access to SqlClient");
}
dbConnectionString = new SqlConnectionStringBuilder(_currentConnectionString);
dbConnectionString.Pooling = true;
}

@ -7,9 +7,9 @@ namespace ZeroLevel.Services.Cache
internal sealed class TimerCachee<T>
: IDisposable
{
private sealed class CacheeItem<T>
private sealed class CacheeItem<I>
{
public T Value { get; set; }
public I Value { get; set; }
public DateTime LastAcessTime { get; set; }
}

@ -4,7 +4,7 @@ using System.Security.Cryptography;
namespace ZeroLevel.Services.Encryption
{
public class RijndaelEncryptor
public class AesEncryptor
: IDisposable
{
protected const int DEFAULT_STREAM_BUFFER_SIZE = 16384;
@ -12,20 +12,20 @@ namespace ZeroLevel.Services.Encryption
#region Crypt fields
private Rijndael _rijndael;
private Aes _aes;
private CryptoStream _stream;
#endregion Crypt fields
public RijndaelEncryptor(Stream stream, string password, byte[] salt = null)
public AesEncryptor(Stream stream, string password, byte[] salt = null)
{
_rijndael = Rijndael.Create();
_aes = Aes.Create();
using (var pdb = new Rfc2898DeriveBytes(password, SALT))
{
_rijndael.Key = pdb.GetBytes(32);
_rijndael.IV = pdb.GetBytes(16);
_aes.Key = pdb.GetBytes(32);
_aes.IV = pdb.GetBytes(16);
}
_stream = new CryptoStream(stream, _rijndael.CreateEncryptor(), CryptoStreamMode.Write);
_stream = new CryptoStream(stream, _aes.CreateEncryptor(), CryptoStreamMode.Write);
}
public void Write(byte[] data)
@ -56,15 +56,15 @@ namespace ZeroLevel.Services.Encryption
public static byte[] Encrypt(byte[] plain, string password, byte[] salt = null)
{
using (var rijndael = Rijndael.Create())
using (var aes = Aes.Create())
{
using (var pdb = new Rfc2898DeriveBytes(password, salt ?? SALT))
{
rijndael.Key = pdb.GetBytes(32);
rijndael.IV = pdb.GetBytes(16);
aes.Key = pdb.GetBytes(32);
aes.IV = pdb.GetBytes(16);
using (var memoryStream = new MemoryStream())
{
using (var cryptoStream = new CryptoStream(memoryStream, rijndael.CreateEncryptor(), CryptoStreamMode.Write))
using (var cryptoStream = new CryptoStream(memoryStream, aes.CreateEncryptor(), CryptoStreamMode.Write))
{
cryptoStream.Write(plain, 0, plain.Length);
cryptoStream.Close();
@ -77,15 +77,15 @@ namespace ZeroLevel.Services.Encryption
public static byte[] Encrypt(Stream stream, string password, byte[] salt = null)
{
using (var rijndael = Rijndael.Create())
using (var aes = Aes.Create())
{
using (var pdb = new Rfc2898DeriveBytes(password, salt ?? SALT))
{
rijndael.Key = pdb.GetBytes(32);
rijndael.IV = pdb.GetBytes(16);
aes.Key = pdb.GetBytes(32);
aes.IV = pdb.GetBytes(16);
using (var memoryStream = new MemoryStream())
{
using (var cryptoStream = new CryptoStream(memoryStream, rijndael.CreateEncryptor(), CryptoStreamMode.Write))
using (var cryptoStream = new CryptoStream(memoryStream, aes.CreateEncryptor(), CryptoStreamMode.Write))
{
Transfer(stream, cryptoStream);
cryptoStream.Close();
@ -98,13 +98,13 @@ namespace ZeroLevel.Services.Encryption
public static void Encrypt(Stream inputStream, Stream outputStream, string password, byte[] salt = null)
{
using (var rijndael = Rijndael.Create())
using (var aes = Aes.Create())
{
using (var pdb = new Rfc2898DeriveBytes(password, salt ?? SALT))
{
rijndael.Key = pdb.GetBytes(32);
rijndael.IV = pdb.GetBytes(16);
using (var cryptoStream = new CryptoStream(outputStream, rijndael.CreateEncryptor(), CryptoStreamMode.Write))
aes.Key = pdb.GetBytes(32);
aes.IV = pdb.GetBytes(16);
using (var cryptoStream = new CryptoStream(outputStream, aes.CreateEncryptor(), CryptoStreamMode.Write))
{
Transfer(inputStream, cryptoStream);
cryptoStream.Close();
@ -115,15 +115,15 @@ namespace ZeroLevel.Services.Encryption
public static byte[] Decrypt(byte[] cipher, string password, byte[] salt = null)
{
using (var rijndael = Rijndael.Create())
using (var aes = Aes.Create())
{
using (var pdb = new Rfc2898DeriveBytes(password, salt ?? SALT))
{
rijndael.Key = pdb.GetBytes(32);
rijndael.IV = pdb.GetBytes(16);
aes.Key = pdb.GetBytes(32);
aes.IV = pdb.GetBytes(16);
using (var memoryStream = new MemoryStream())
{
using (var cryptoStream = new CryptoStream(memoryStream, rijndael.CreateDecryptor(), CryptoStreamMode.Write))
using (var cryptoStream = new CryptoStream(memoryStream, aes.CreateDecryptor(), CryptoStreamMode.Write))
{
cryptoStream.Write(cipher, 0, cipher.Length);
cryptoStream.Close();
@ -136,15 +136,15 @@ namespace ZeroLevel.Services.Encryption
public static byte[] Decrypt(Stream stream, string password, byte[] salt = null)
{
using (var rijndael = Rijndael.Create())
using (var aes = Aes.Create())
{
using (var pdb = new Rfc2898DeriveBytes(password, salt ?? SALT))
{
rijndael.Key = pdb.GetBytes(32);
rijndael.IV = pdb.GetBytes(16);
aes.Key = pdb.GetBytes(32);
aes.IV = pdb.GetBytes(16);
using (var memoryStream = new MemoryStream())
{
using (var cryptoStream = new CryptoStream(memoryStream, rijndael.CreateDecryptor(), CryptoStreamMode.Write))
using (var cryptoStream = new CryptoStream(memoryStream, aes.CreateDecryptor(), CryptoStreamMode.Write))
{
Transfer(stream, cryptoStream);
cryptoStream.Close();
@ -157,13 +157,13 @@ namespace ZeroLevel.Services.Encryption
public static void Decrypt(Stream inputStream, Stream outputStream, string password, byte[] salt = null)
{
using (var rijndael = Rijndael.Create())
using (var aes = Aes.Create())
{
using (var pdb = new Rfc2898DeriveBytes(password, salt ?? SALT))
{
rijndael.Key = pdb.GetBytes(32);
rijndael.IV = pdb.GetBytes(16);
using (var cryptoStream = new CryptoStream(outputStream, rijndael.CreateDecryptor(), CryptoStreamMode.Write))
aes.Key = pdb.GetBytes(32);
aes.IV = pdb.GetBytes(16);
using (var cryptoStream = new CryptoStream(outputStream, aes.CreateDecryptor(), CryptoStreamMode.Write))
{
Transfer(inputStream, cryptoStream);
cryptoStream.Close();
@ -183,8 +183,8 @@ namespace ZeroLevel.Services.Encryption
_stream.Dispose();
}
catch { }
_rijndael.Clear();
_rijndael.Dispose();
_aes.Clear();
_aes.Dispose();
}
}
}

@ -126,7 +126,6 @@ namespace MemoryPools.Collections.Specialized
if (ReferenceEquals(_other, LengthIs2))
{
var done = false;
if (ItemComparer.Equals(_items.Item2, item))
{
_items.Item2 = default;

@ -1,37 +1,46 @@
using System;
using System.IO;
using System.Net;
using System.Net.Http;
namespace ZeroLevel.Services.Network.Utils
{
/// <summary>
/// Methods related to Network.
/// </summary>
public static class Network
{
/// <summary>
/// Gets the external IP Address.
/// </summary>
/// <value>The external IP Address.</value>
public static string ExternalIP
{
get
{
try
{
WebRequest request = WebRequest.Create("http://ipv4.icanhazip.com");
using (var response = request.GetResponse())
using (var sr = new StreamReader(response.GetResponseStream()))
{
return sr.ReadLine();
}
}
catch (Exception e)
{
Console.WriteLine("Error: " + e.Message);
return "";
}
}
}
}
/// <summary>
/// Methods related to Network.
/// </summary>
public static class Network
{
/// <summary>
/// Gets the external IP Address.
/// </summary>
/// <value>The external IP Address.</value>
public static string ExternalIP
{
get
{
try
{
using (var clientHandler = new HttpClientHandler())
{
clientHandler.ServerCertificateCustomValidationCallback = (_, _, _, _) => true;
clientHandler.UseCookies = true;
clientHandler.CookieContainer = new CookieContainer();
using (var client = new HttpClient(clientHandler))
{
var message = new HttpRequestMessage(HttpMethod.Get, "http://ipv4.icanhazip.com");
using (var response = client.Send(message))
{
return response.Content.ReadAsStringAsync().Result;
}
}
}
}
catch (Exception e)
{
Console.WriteLine("Error: " + e.Message);
return "";
}
}
}
}
}

@ -29,6 +29,7 @@
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<Deterministic>False</Deterministic>
<DebugType>portable</DebugType>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x86'">
@ -62,6 +63,7 @@
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<Deterministic>False</Deterministic>
<DebugType>none</DebugType>
</PropertyGroup>
<ItemGroup>

Loading…
Cancel
Save

Powered by TurnKey Linux.