Files
Nebula-Auth/NebulaAuth/Model/PHandler.cs
T
achiez 8ff960189e 1.5.6 Bug fixes and code clean-ups
- Update: Added "Copy Password" to Mafile context menu (if available)
- Update: "Save Password" now pre-fills the current password in the "Login Again" dialog when encryption is enabled
- Fix: Prevented overlapping confirmation cycles in AutoConfirmer
- Fix: Added a Snackbar notification when a confirmation cycle is skipped due to a too frequent timer interval
- Fix: Fixed a rare bug where the previous proxy was used instead of the current one during session refresh
- Fix: Corrected "Session Permanently Expired" message showing on unrelated errors (e.g. network issues)
- UI-Fix: Fixed visual glitch where proxy input appeared non-empty when switching to a mafile-specific proxy
- Cleanup: Refactored and cleaned up codebase, improved styling and formatting via ReSharper
2025-05-02 23:29:24 +03:00

94 lines
2.6 KiB
C#

using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
namespace NebulaAuth.Model;
public static class PHandler
{
private static byte[] _k = [];
public static bool IsPasswordSet => _k.Length > 0;
/// <summary>
/// </summary>
/// <param name="password"></param>
/// <returns><see langword="true" /> if password was set and not empty. Otherwise - <see langword="false" /></returns>
public static bool SetPassword(string? password)
{
if (string.IsNullOrWhiteSpace(password))
{
_k = [];
return false;
}
var keyBytes = Encoding.UTF8.GetBytes(password);
_k = SHA256.HashData(keyBytes);
return _k.Length > 0;
}
public static string Encrypt(string plainText)
{
if (_k.Length == 0) throw new Exception("Password not set");
byte[] encryptedBytes;
using (var aes = Aes.Create())
{
aes.Key = _k;
aes.IV = new byte[16];
var encryptor = aes.CreateEncryptor(aes.Key, aes.IV);
var plainTextBytes = Encoding.UTF8.GetBytes(plainText);
using (var memoryStream = new MemoryStream())
{
using (var cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write))
{
cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length);
cryptoStream.FlushFinalBlock();
}
encryptedBytes = memoryStream.ToArray();
}
}
return Convert.ToBase64String(encryptedBytes);
}
public static string Decrypt(string encryptedText)
{
if (_k.Length == 0) throw new Exception("Password not set");
var encryptedBytes = Convert.FromBase64String(encryptedText);
using var aes = Aes.Create();
var keyBytes = _k;
var iv = new byte[aes.IV.Length];
aes.Key = keyBytes;
aes.Mode = CipherMode.CBC;
aes.Padding = PaddingMode.PKCS7;
using var decryptor = aes.CreateDecryptor(aes.Key, iv);
using var ms = new MemoryStream(encryptedBytes);
using var cs = new CryptoStream(ms, decryptor, CryptoStreamMode.Read);
using var reader = new StreamReader(cs);
var decryptedText = reader.ReadToEnd();
return decryptedText;
}
public static string? DecryptPassword(string? encryptedPassword)
{
if (string.IsNullOrWhiteSpace(encryptedPassword)) return null;
try
{
return Decrypt(encryptedPassword);
}
catch (Exception)
{
return null;
}
}
}