Skip to content

Commit

Permalink
Added encryption
Browse files Browse the repository at this point in the history
  • Loading branch information
Whitebrim committed Apr 23, 2022
1 parent 2f23c93 commit ef08060
Show file tree
Hide file tree
Showing 10 changed files with 423 additions and 44 deletions.
7 changes: 7 additions & 0 deletions README.md.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,23 @@
namespace CurrencySystem
{
[Serializable]
public class CurrencyAccount : ICurrency
public sealed class CurrencyAccount : ICurrency
{
private readonly string _currencyCode;
private float _multiplier = 1;
private double _amount;
private IVault _vault;

[field: NonSerialized]
public event ICurrency.CurrencyEvent OnCurrencyChange;

private double Amount
{
get { return _amount; } // TODO decrypt
set { _amount = value; } // TODO encrypt
get { return _vault.Amount; }
set
{
_vault.Amount = value;
NotifyEvent();
}
}

public string CurrencyCode => _currencyCode;
Expand All @@ -27,8 +31,8 @@ private double Amount
public CurrencyAccount(string currencyCode, double amount, float multiplier = 1)
{
_currencyCode = currencyCode ?? throw new ArgumentNullException(nameof(currencyCode));
_amount = amount;
_multiplier = multiplier;
_vault = new Vault(amount, currencyCode);
}

private void NotifyEvent()
Expand All @@ -40,13 +44,11 @@ private void NotifyEvent()
public void Add(double amount)
{
Amount += amount * _multiplier;
NotifyEvent();
}

public void Clear()
{
Amount = 0;
NotifyEvent();
}

public double Get()
Expand All @@ -57,57 +59,55 @@ public double Get()
public void Subtract(double amount)
{
Amount -= amount;
NotifyEvent();
}

public bool TrySubtract(double amount)
{
if (Amount - amount >= 0)
{
Amount -= amount;
NotifyEvent();
return true;
}
return false;
}

public static CurrencyAccount operator +(CurrencyAccount vault, double income)
public static CurrencyAccount operator +(CurrencyAccount account, double income)
{
vault.Add(income);
return vault;
account.Add(income);
return account;
}

public static CurrencyAccount operator +(CurrencyAccount vault, CurrencyAccount vault2)
public static CurrencyAccount operator +(CurrencyAccount account, CurrencyAccount account2)
{
vault.Add(vault2.Get());
return vault;
account.Add(account2.Get());
return account;
}

public static CurrencyAccount operator ++(CurrencyAccount vault)
public static CurrencyAccount operator ++(CurrencyAccount account)
{
vault.Add(1);
return vault;
account.Add(1);
return account;
}

public static CurrencyAccount operator -(CurrencyAccount vault, double toll)
public static CurrencyAccount operator -(CurrencyAccount account, double toll)
{
vault.Subtract(toll);
return vault;
account.Subtract(toll);
return account;
}

public static CurrencyAccount operator -(CurrencyAccount vault, CurrencyAccount vault2)
public static CurrencyAccount operator -(CurrencyAccount account, CurrencyAccount account2)
{
vault.Subtract(vault2.Get());
return vault;
account.Subtract(account2.Get());
return account;
}

public static CurrencyAccount operator --(CurrencyAccount vault)
public static CurrencyAccount operator --(CurrencyAccount account)
{
vault.Subtract(1);
return vault;
account.Subtract(1);
return account;
}

public static implicit operator double(CurrencyAccount vault) => vault.Amount;
public static implicit operator double(CurrencyAccount account) => account.Amount;

public override bool Equals(object obj)
{
Expand Down
File renamed without changes.
7 changes: 7 additions & 0 deletions Runtime/Wallet/IVault.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace CurrencySystem
{
public interface IVault
{
double Amount { get; set; }
}
}
11 changes: 11 additions & 0 deletions Runtime/Wallet/IVault.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

113 changes: 113 additions & 0 deletions Runtime/Wallet/Vault.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
using System;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.Security.Cryptography;
using System.Text;

namespace CurrencySystem
{
[Serializable]
public class Vault : IVault
{
private byte[] _amount;
private readonly string _currencyCode;
[NonSerialized]
private byte[] _key;

public Vault(double amount, string currencyCode)
{
_currencyCode = currencyCode;
InitKeys();
Amount = amount;
}

public double Amount
{
get
{
return Decrypt(_amount, _key);
}
set
{
_amount = Encrypt(value, _key);
}
}

internal void InitKeys()
{
_key = GenerateValidKeys(_currencyCode);
}

private byte[] GenerateValidKeys(string seed)
{
byte[] result = new byte[0];
do
{
result = result.Concat(Encoding.ASCII.GetBytes(seed)).ToArray();
} while (result.Length < 16);
Array.Resize(ref result, 16);
return result;
}

private double Decrypt(byte[] encryptedData, byte[] key)
{
byte[] bytesIv = new byte[16];
Array.Copy(encryptedData, 0, bytesIv, 0, 16);
byte[] data = new byte[encryptedData.Length - 16];
Array.Copy(encryptedData, 16, data, 0, data.Length);

double result;

using (Aes aes = Aes.Create())
{
aes.Key = key;
aes.IV = bytesIv;

ICryptoTransform crypt = aes.CreateDecryptor(aes.Key, aes.IV);
using (MemoryStream ms = new MemoryStream(data))
{
using (CryptoStream cs = new CryptoStream(ms, crypt, CryptoStreamMode.Read))
{
using (BinaryReader sr = new BinaryReader(cs))
{
result = sr.ReadDouble();
}
}
}
}
return result;
}

private byte[] Encrypt(double data, byte[] key)
{
byte[] encrypted;
using (Aes aes = Aes.Create())
{
aes.Key = key;

ICryptoTransform crypt = aes.CreateEncryptor(aes.Key, aes.IV);
using (MemoryStream ms = new MemoryStream())
{
using (CryptoStream cs = new CryptoStream(ms, crypt, CryptoStreamMode.Write))
{
using (BinaryWriter sw = new BinaryWriter(cs))
{
sw.Write(data);
//sw.Write(BitConverter.GetBytes(data));
}
}
encrypted = ms.ToArray();
}
encrypted = aes.IV.Concat(encrypted).ToArray();
}
return encrypted;
}

[OnDeserialized]
private void OnDeserialized(StreamingContext context)
{
InitKeys();
}
}
}
11 changes: 11 additions & 0 deletions Runtime/Wallet/Vault.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit ef08060

Please sign in to comment.