简体   繁体   中英

SHA256 Hashing of String **With Update** (UTF8 String)

I'm Trying to Hash a String from Console Input with Update (Similar to That in Node Crypto);

I've Used this in Node JS. How can I replicate this Behavior In C#

import { createHmac } from 'crypto';
Hash(password: string, update: string): string {
    return createHmac('sha256','StringSecret').update('StringKey').digest('hex');
}

I've Tried this Solution It Goes a Like This.

using System.Text;  
using System.Security.Cryptography;  
  
namespace HashConsoleApp  
{  
    class Program  
    {  
        static void Main(string[] args)  
        {  
            string plainData = "Password";  
            Console.WriteLine("Raw data: {0}", plainData);  
            string hashedData = ComputeSha256Hash(plainData);  
            Console.WriteLine("Hash {0}", hashedData);  
            Console.WriteLine(ComputeSha256Hash("Password"));  
            Console.ReadLine();  
        }  
  
        static string ComputeSha256Hash(string rawData)  
        {  
            // Create a SHA256   
            using (SHA256 sha256Hash = SHA256.Create())  
            {  
                // ComputeHash - returns byte array  
                byte[] bytes = sha256Hash.ComputeHash(Encoding.UTF8.GetBytes(rawData));  
  
                // Convert byte array to a string   
                StringBuilder builder = new StringBuilder();  
                for (int i = 0; i < bytes.Length; i++)  
                {  
                    builder.Append(bytes[i].ToString("x2"));  
                }  
                return builder.ToString();  
            }  
        }  
                 
    }  
  
}  

But it Does not allow any Update;

From what I can tell from the Node docs , the update function is just adding the new data to the hash function's input. If that is indeed the case, you'd accomplish this simply by appending the data before passing it to your hashing method.

string hashedData = ComputeSha256Hash("StringSecret" + "StringKey");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM