using System.Security.Cryptography;
using System.Text;

namespace UserAuthApp.BLL
{
    public static class PasswordHelper
    {
        /// <summary>
        /// Takes a plain-text password and returns a SHA-256 hash string.
        /// Example: "MyPassword123" → "ef92b778bafe771e89245b89ecbc08a44a4e166c..."
        /// </summary>
        public static string HashPassword(string password)
        {
            // Create a SHA-256 hasher
            using (SHA256 sha256 = SHA256.Create())
            {
                // Convert the password string to bytes, then hash those bytes
                byte[] bytes = sha256.ComputeHash(Encoding.UTF8.GetBytes(password));

                // Convert the hashed bytes back to a readable hex string
                StringBuilder builder = new StringBuilder();
                foreach (byte b in bytes)
                {
                    builder.Append(b.ToString("x2")); // "x2" = lowercase hexadecimal
                }

                return builder.ToString();
            }
        }
    }
}
