Files
atakanozbancom/Models/classes/SecretProtector.cs
T
Atakan Doğan Özban e97a71e5b2 Add password hashing, user settings with 2FA, and project image lightbox.
Hardens auth with PBKDF2, lockouts, local QR setup, and safer embeds while moving account security under the username menu.
2026-07-17 18:49:34 +02:00

55 lines
1.5 KiB
C#

using System;
using System.Text;
using System.Web.Security;
namespace atakanozbancom.Models.classes
{
public static class SecretProtector
{
private static readonly string[] Purposes = { "atakanozbancom.TotpSecret.v1" };
public static string Protect(string plaintext)
{
if (string.IsNullOrEmpty(plaintext))
return plaintext;
var bytes = Encoding.UTF8.GetBytes(plaintext);
var protectedBytes = MachineKey.Protect(bytes, Purposes);
return Convert.ToBase64String(protectedBytes);
}
public static string Unprotect(string protectedValue)
{
if (string.IsNullOrEmpty(protectedValue))
return protectedValue;
try
{
var protectedBytes = Convert.FromBase64String(protectedValue);
var bytes = MachineKey.Unprotect(protectedBytes, Purposes);
return bytes == null ? null : Encoding.UTF8.GetString(bytes);
}
catch
{
return null;
}
}
public static bool LooksProtected(string value)
{
if (string.IsNullOrWhiteSpace(value))
return false;
try
{
var bytes = Convert.FromBase64String(value);
return bytes.Length > 16;
}
catch
{
return false;
}
}
}
}