Use C# 8.0 features, naming convention fixes
This commit is contained in:
parent
3d5000ee64
commit
37bfd158ec
4 changed files with 33 additions and 53 deletions
|
@ -1,7 +1,4 @@
|
|||
using System;
|
||||
using Wulkanowy;
|
||||
|
||||
namespace Wulkanowy.Example
|
||||
namespace WulkanowyQr.Example
|
||||
{
|
||||
class Program
|
||||
{
|
||||
|
|
|
@ -1,26 +1,25 @@
|
|||
using System;
|
||||
using Xunit;
|
||||
|
||||
namespace Wulkanowy.Test
|
||||
namespace WulkanowyQr.Test
|
||||
{
|
||||
public class QrTest
|
||||
{
|
||||
private const string _password = "0123456789ABCDEF";
|
||||
private const string _content = "CERT#https://api.fakelog.cf/Default/mobile-api#FK100000#ENDCERT";
|
||||
private const string _base64Content = "27Grk6d8ZDely5eeF7j2ngWrWV9eZa5Dz9ZmuiBavysDp74TCr6EHJOs6TaIXFh3HsROWSM11pv3cPvRGSi7Nw==";
|
||||
private const string Password = "0123456789ABCDEF";
|
||||
private const string Content = "CERT#https://api.fakelog.cf/Default/mobile-api#FK100000#ENDCERT";
|
||||
private const string Base64Content = "27Grk6d8ZDely5eeF7j2ngWrWV9eZa5Dz9ZmuiBavysDp74TCr6EHJOs6TaIXFh3HsROWSM11pv3cPvRGSi7Nw==";
|
||||
|
||||
[Fact]
|
||||
public void EncodeQr()
|
||||
{
|
||||
string result = Qr.Encode(_password, _content);
|
||||
Assert.Equal(_base64Content, result);
|
||||
var result = Qr.Encode(Password, Content);
|
||||
Assert.Equal(Base64Content, result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DecodeQr()
|
||||
{
|
||||
string result = Qr.Decode(_password, _base64Content);
|
||||
Assert.Equal(_content, result);
|
||||
var result = Qr.Decode(Password, Base64Content);
|
||||
Assert.Equal(Content, result);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -3,7 +3,7 @@ using System.IO;
|
|||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
|
||||
namespace Wulkanowy
|
||||
namespace WulkanowyQr
|
||||
{
|
||||
//See https://docs.microsoft.com/en-gb/dotnet/api/system.security.cryptography.aes?view=net-5.0
|
||||
|
||||
|
@ -11,56 +11,39 @@ namespace Wulkanowy
|
|||
{
|
||||
public static string Encode(string key, string content)
|
||||
{
|
||||
byte[] encrypted;
|
||||
using var aes = Aes.Create();
|
||||
aes.Key = Encoding.UTF8.GetBytes(key);
|
||||
aes.Mode = CipherMode.ECB;
|
||||
aes.Padding = PaddingMode.PKCS7;
|
||||
|
||||
using (Aes aes = Aes.Create())
|
||||
var enc = aes.CreateEncryptor(aes.Key, aes.IV);
|
||||
|
||||
using var memoryStream = new MemoryStream();
|
||||
using var csEncrypt = new CryptoStream(memoryStream, enc, CryptoStreamMode.Write);
|
||||
using (var writer = new StreamWriter(csEncrypt))
|
||||
{
|
||||
aes.Key = Encoding.UTF8.GetBytes(key);
|
||||
aes.Mode = CipherMode.ECB;
|
||||
aes.Padding = PaddingMode.PKCS7;
|
||||
|
||||
var enc = aes.CreateEncryptor(aes.Key, aes.IV);
|
||||
|
||||
using (MemoryStream memoryStream = new MemoryStream())
|
||||
{
|
||||
using (CryptoStream csEncrypt = new CryptoStream(memoryStream, enc, CryptoStreamMode.Write))
|
||||
{
|
||||
using (StreamWriter writer = new StreamWriter(csEncrypt))
|
||||
{
|
||||
writer.Write(content);
|
||||
}
|
||||
|
||||
encrypted = memoryStream.ToArray();
|
||||
}
|
||||
}
|
||||
writer.Write(content);
|
||||
}
|
||||
|
||||
var encrypted = memoryStream.ToArray();
|
||||
|
||||
return Convert.ToBase64String(encrypted);
|
||||
}
|
||||
|
||||
public static string Decode(string key, string content)
|
||||
{
|
||||
string decrypted;
|
||||
using var aes = Aes.Create();
|
||||
aes.Key = Encoding.UTF8.GetBytes(key);
|
||||
aes.Mode = CipherMode.ECB;
|
||||
aes.Padding = PaddingMode.PKCS7;
|
||||
|
||||
using (Aes aes = Aes.Create())
|
||||
{
|
||||
aes.Key = Encoding.UTF8.GetBytes(key);
|
||||
aes.Mode = CipherMode.ECB;
|
||||
aes.Padding = PaddingMode.PKCS7;
|
||||
var dec = aes.CreateDecryptor(aes.Key, aes.IV);
|
||||
|
||||
var dec = aes.CreateDecryptor(aes.Key, aes.IV);
|
||||
using var memoryStream = new MemoryStream(Convert.FromBase64String(content));
|
||||
using var cs = new CryptoStream(memoryStream, dec, CryptoStreamMode.Read);
|
||||
using var reader = new StreamReader(cs);
|
||||
|
||||
using (MemoryStream memoryStream = new MemoryStream(Convert.FromBase64String(content)))
|
||||
{
|
||||
using (CryptoStream cs = new CryptoStream(memoryStream, dec, CryptoStreamMode.Read))
|
||||
{
|
||||
using (StreamReader reader = new StreamReader(cs))
|
||||
{
|
||||
decrypted = reader.ReadToEnd();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return decrypted;
|
||||
return reader.ReadToEnd();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netstandard2.0</TargetFramework>
|
||||
<LangVersion>8</LangVersion>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
|
|
Loading…
Reference in a new issue