Fix config, append TokenEncryptor

pull/4/head
Ogoun 1 year ago
parent 48f3916af0
commit dcc8875661

@ -1,6 +1,4 @@
using System;
using System.Collections.Concurrent;
using ZeroLevel.Services.Network.Utils;
namespace TestApp
{
@ -8,7 +6,16 @@ namespace TestApp
{
private static void Main(string[] args)
{
Console.WriteLine(Network.ExternalIP);
AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
}
private static System.Reflection.Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs e)
{
if (e.Name.StartsWith("Microsoft.Build."))
{
// искать в локальной папке
}
return null;
}
}
}

@ -0,0 +1,68 @@
using System;
using Xunit;
using ZeroLevel.Services;
using ZeroLevel.Services.Serialization;
namespace ZeroLevel.UnitTests
{
public class TokenEncryptorTest
{
public class TestClaims
: IBinarySerializable, IEquatable<TestClaims>
{
public string Login;
public string Name;
public void Deserialize(IBinaryReader reader)
{
this.Login = reader.ReadString();
this.Name = reader.ReadString();
}
public override bool Equals(object obj)
{
return this.Equals(obj as TestClaims);
}
public bool Equals(TestClaims other)
{
if (other == null) return false;
if (string.Compare(this.Login, other.Login, StringComparison.OrdinalIgnoreCase) != 0) return false;
if (string.Compare(this.Name, other.Name, StringComparison.OrdinalIgnoreCase) != 0) return false;
return true;
}
public void Serialize(IBinaryWriter writer)
{
writer.WriteString(this.Login);
writer.WriteString(this.Name);
}
}
[Fact]
public void TokenEncryptorEncryptDecryptTest()
{
// Arrange
var a1 = new TestClaims { Login = null, Name = "name" };
var a2 = new TestClaims { Login = "login", Name = "name" };
var a3 = new TestClaims { Login = null, Name = null };
var key = "testkey";
var salt = "testsalt";
var tokenEncryptor = new TokenEncryptor(key, salt);
// Act
var a1_token = tokenEncryptor.WriteToToken(a1);
var a2_token = tokenEncryptor.WriteToToken(a2);
var a3_token = tokenEncryptor.WriteToToken(a3);
var a1_decrypted = tokenEncryptor.ReadFromToken<TestClaims>(a1_token);
var a2_decrypted = tokenEncryptor.ReadFromToken<TestClaims>(a2_token);
var a3_decrypted = tokenEncryptor.ReadFromToken<TestClaims>(a3_token);
// Assert
Assert.True(a1.Equals(a1_decrypted));
Assert.True(a2.Equals(a2_decrypted));
Assert.True(a3.Equals(a3_decrypted));
}
}
}

@ -527,8 +527,8 @@ namespace ZeroLevel.Services.Config
else
{
var elements = values.Select(v => parser.Parse(v)).ToArray();
var arrayBuilder = CollectionFactory.CreateArray(itemType, elements.Length);
foreach (var item in elements)
var arrayBuilder = CollectionFactory.CreateArray(itemType, (elements[0] as Array).Length);
foreach (var item in (elements[0] as Array))
{
arrayBuilder.Set(item, index);
index++;

@ -401,8 +401,6 @@ namespace ZeroLevel
}
#endregion Read configuration
public static IConfiguration Merge(ConfigurationRecordExistBehavior existRecordBehavior, params IConfiguration[] configurations)
{
var result = Configuration.Create();

@ -6,7 +6,7 @@ namespace ZeroLevel.Services.Shedulling
/// <summary>
/// A wrapper around an Action that stores the time at which an action should be performed, as well as a link to the next action.
/// </summary>
internal class ExpiredObject
internal sealed class ExpiredObject
{
private static long _counter = 0;

@ -0,0 +1,97 @@
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using ZeroLevel.Services.Serialization;
namespace ZeroLevel.Services
{
public class TokenEncryptor
{
private sealed class Cryptor
{
private const int Rfc2898KeygenIterations = 100;
private const int AesKeySizeInBits = 128;
private readonly byte[] _password;
private readonly byte[] _salt;
public Cryptor(string pwd, string salt)
{
_password = Encoding.ASCII.GetBytes(pwd);
_salt = Encoding.ASCII.GetBytes(salt);
}
public byte[] Encrypt(byte[] data)
{
using (Aes aes = new AesManaged())
{
aes.Padding = PaddingMode.PKCS7;
aes.KeySize = AesKeySizeInBits;
int KeyStrengthInBytes = aes.KeySize / 8;
System.Security.Cryptography.Rfc2898DeriveBytes rfc2898 =
new System.Security.Cryptography.Rfc2898DeriveBytes(_password, _salt, Rfc2898KeygenIterations);
aes.Key = rfc2898.GetBytes(KeyStrengthInBytes);
aes.IV = rfc2898.GetBytes(KeyStrengthInBytes);
using (MemoryStream ms = new MemoryStream())
{
using (CryptoStream cs = new CryptoStream(ms, aes.CreateEncryptor(), CryptoStreamMode.Write))
{
cs.Write(data, 0, data.Length);
}
return ms.ToArray();
}
}
}
public byte[] Decrypt(byte[] data)
{
using (Aes aes = new AesManaged())
{
aes.Padding = PaddingMode.PKCS7;
aes.KeySize = AesKeySizeInBits;
int KeyStrengthInBytes = aes.KeySize / 8;
System.Security.Cryptography.Rfc2898DeriveBytes rfc2898 =
new System.Security.Cryptography.Rfc2898DeriveBytes(_password, _salt, Rfc2898KeygenIterations);
aes.Key = rfc2898.GetBytes(KeyStrengthInBytes);
aes.IV = rfc2898.GetBytes(KeyStrengthInBytes);
using (MemoryStream ms = new MemoryStream())
{
using (CryptoStream cs = new CryptoStream(ms, aes.CreateDecryptor(), CryptoStreamMode.Write))
{
cs.Write(data, 0, data.Length);
}
return ms.ToArray();
}
}
}
}
private Cryptor _cryptor;
public TokenEncryptor(string key, string salt)
{
_cryptor = new Cryptor(key, salt);
}
public T ReadFromToken<T>(string token)
where T : IBinarySerializable
{
if (string.IsNullOrWhiteSpace(token) == false && string.CompareOrdinal(token, "null") != 0)
{
var data = Convert.FromBase64String(token);
var decryptedBytes = _cryptor.Decrypt(data);
var decryptedValue = MessageSerializer.Deserialize<T>(decryptedBytes);
return decryptedValue;
}
return default!;
}
public string WriteToToken<T>(T value)
where T : IBinarySerializable
{
var decryptedBytes = MessageSerializer.Serialize<T>(value);
var encryptedBytes = _cryptor.Encrypt(decryptedBytes);
var token = Convert.ToBase64String(encryptedBytes);
return token;
}
}
}

@ -6,16 +6,16 @@
</Description>
<Authors>ogoun</Authors>
<Company>ogoun</Company>
<AssemblyVersion>3.3.9.8</AssemblyVersion>
<PackageReleaseNotes>Getting the end date of the task in the sheduler</PackageReleaseNotes>
<AssemblyVersion>3.3.9.9</AssemblyVersion>
<PackageReleaseNotes>Token encryptor</PackageReleaseNotes>
<PackageProjectUrl>https://github.com/ogoun/Zero/wiki</PackageProjectUrl>
<Copyright>Copyright Ogoun 2023</Copyright>
<PackageLicenseUrl></PackageLicenseUrl>
<PackageIconUrl></PackageIconUrl>
<RepositoryUrl>https://github.com/ogoun/Zero</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<Version>3.3.9.8</Version>
<FileVersion>3.3.9.8</FileVersion>
<Version>3.3.9.9</Version>
<FileVersion>3.3.9.9</FileVersion>
<Platforms>AnyCPU;x64;x86</Platforms>
<PackageIcon>zero.png</PackageIcon>
<DebugType>full</DebugType>

Loading…
Cancel
Save

Powered by TurnKey Linux.