diff options
| author | Lucas Faria Mendes <lucas.fariamo08@gmail.com> | 2026-03-30 17:17:46 +0000 |
|---|---|---|
| committer | Lucas Faria Mendes <lucas.fariamo08@gmail.com> | 2026-03-30 17:17:46 +0000 |
| commit | 0440c722a221b8068bbf388c1c0c51f0faff0451 (patch) | |
| tree | 169cbf90c50ff7961db82ecb606c50c2a45a1688 /Gestor.Common/Gestor.Common.Security/Token.cs | |
| parent | 225aa1499e37faf9d38257caabbadc68d78b427e (diff) | |
| download | gestor-master.tar.gz gestor-master.zip | |
Diffstat (limited to 'Gestor.Common/Gestor.Common.Security/Token.cs')
| -rw-r--r-- | Gestor.Common/Gestor.Common.Security/Token.cs | 138 |
1 files changed, 138 insertions, 0 deletions
diff --git a/Gestor.Common/Gestor.Common.Security/Token.cs b/Gestor.Common/Gestor.Common.Security/Token.cs new file mode 100644 index 0000000..a988d6a --- /dev/null +++ b/Gestor.Common/Gestor.Common.Security/Token.cs @@ -0,0 +1,138 @@ +using System; +using System.IO; +using System.Security.Cryptography; +using System.Text; +using Gestor.Common.Helpers; + +namespace Gestor.Common.Security; + +public class Token +{ + private readonly byte[] _key = new byte[32] + { + 45, 103, 73, 146, 210, 184, 220, 224, 94, 3, + 114, 60, 211, 119, 21, 100, 18, 201, 230, 195, + 119, 252, 73, 208, 209, 39, 222, 48, 47, 142, + 94, 24 + }; + + private readonly byte[] _initializationVector = new byte[16] + { + 95, 17, 151, 243, 209, 243, 119, 80, 63, 252, + 13, 180, 162, 13, 23, 218 + }; + + private static readonly byte[] Salt = new byte[16] + { + 38, 220, 255, 0, 173, 237, 122, 238, 197, 254, + 7, 175, 77, 8, 34, 60 + }; + + public bool VerifyToken(string authHeader) + { + try + { + if (!authHeader.Contains("Token")) + { + return false; + } + string ecryptedText = authHeader.Replace("Token ", ""); + return DefaultDecryption(ecryptedText); + } + catch (Exception) + { + return false; + } + } + + public string DefaultEncryption(long clientId, long providerId) + { + string plainText = $"{providerId}:{Functions.GetNetworkTime().ToUniversalTime().Ticks}"; + return Encrypt(plainText); + } + + public bool DefaultDecryption(string ecryptedText) + { + string[] textDecrypted = Decrypt(ecryptedText).Split(new char[1] { ':' }); + return DefaultVerification(textDecrypted); + } + + public bool DefaultVerification(string[] textDecrypted) + { + if (textDecrypted.Length < 2) + { + return false; + } + return new DateTime(long.Parse(textDecrypted[1]), DateTimeKind.Utc).Date.AddHours(5.0) >= Functions.GetNetworkTime().ToUniversalTime().Date; + } + + public string Encrypt(string plainText) + { + byte[] inArray; + using (AesCryptoServiceProvider aesCryptoServiceProvider = new AesCryptoServiceProvider()) + { + ICryptoTransform transform = aesCryptoServiceProvider.CreateEncryptor(_key, _initializationVector); + using MemoryStream memoryStream = new MemoryStream(); + using CryptoStream stream = new CryptoStream(memoryStream, transform, CryptoStreamMode.Write); + using (StreamWriter streamWriter = new StreamWriter(stream)) + { + streamWriter.Write(plainText); + } + inArray = memoryStream.ToArray(); + } + return Convert.ToBase64String(inArray); + } + + public string Decrypt(string plainText) + { + try + { + byte[] buffer = Convert.FromBase64String(plainText); + string result; + using (AesCryptoServiceProvider aesCryptoServiceProvider = new AesCryptoServiceProvider()) + { + ICryptoTransform transform = aesCryptoServiceProvider.CreateDecryptor(_key, _initializationVector); + using MemoryStream stream = new MemoryStream(buffer); + using CryptoStream stream2 = new CryptoStream(stream, transform, CryptoStreamMode.Read); + using StreamReader streamReader = new StreamReader(stream2); + result = streamReader.ReadToEnd(); + } + return result; + } + catch (Exception) + { + return null; + } + } + + public string AggerEncrypt(string plain) + { + string password = "aGG3r" + Convert.ToString(1012) + "#w3BDz$"; + byte[] bytes = Encoding.UTF8.GetBytes(plain); + Rijndael rijndael = Rijndael.Create(); + Rfc2898DeriveBytes rfc2898DeriveBytes = new Rfc2898DeriveBytes(password, Salt); + rijndael.Key = rfc2898DeriveBytes.GetBytes(32); + rijndael.IV = rfc2898DeriveBytes.GetBytes(16); + MemoryStream memoryStream = new MemoryStream(); + CryptoStream cryptoStream = new CryptoStream(memoryStream, rijndael.CreateEncryptor(), CryptoStreamMode.Write); + cryptoStream.Write(bytes, 0, bytes.Length); + cryptoStream.Close(); + return Convert.ToBase64String(memoryStream.ToArray()); + } + + public string AggerDecrypt(string cipher) + { + string password = "aGG3r" + Convert.ToString(1012) + "#w3BDz$"; + byte[] array = Convert.FromBase64String(cipher); + Rijndael rijndael = Rijndael.Create(); + Rfc2898DeriveBytes rfc2898DeriveBytes = new Rfc2898DeriveBytes(password, Salt); + rijndael.Key = rfc2898DeriveBytes.GetBytes(32); + rijndael.IV = rfc2898DeriveBytes.GetBytes(16); + MemoryStream memoryStream = new MemoryStream(); + CryptoStream cryptoStream = new CryptoStream(memoryStream, rijndael.CreateDecryptor(), CryptoStreamMode.Write); + cryptoStream.Write(array, 0, array.Length); + cryptoStream.Close(); + byte[] bytes = memoryStream.ToArray(); + return Encoding.UTF8.GetString(bytes); + } +} |