[C#] Encrypting User Passwords Using the HMACSHA256 Class

발행일: Jan, 2025
조회수: 0
단어수: 86

Table of Contents

Introduction

By using the HMACSHA256 class in C#, you can easily convert a user's password into an encrypted string with very simple code.

Code Example


    public static class CredentialHelper
    {
        public static string GetHMACSHA256Hash(string username, string password)
        {
            var key = Encoding.ASCII.GetBytes(username);
            var data = Encoding.ASCII.GetBytes(password);
            return Convert.ToBase64String(new HMACSHA256(key).ComputeHash(data));
        }
    }

Usage

As shown above, you can create a static class and use the function to generate a hash string from the username and password entered by the user. You can then store this hash string and compare it with the hash generated from the username/password combination entered by the user during login to verify.

태그#HMACSHA256#C##password encryption#hashing#credential helper

JUNA BLOG VISITORS

오늘0
Total0