简体   繁体   English

字符串的 SHA256 散列**使用更新**(UTF8 字符串)

[英]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);我正在尝试 Hash 来自控制台输入的字符串并进行更新(类似于节点加密中的字符串);

I've Used this in Node JS.我在 Node JS 中使用过这个。 How can I replicate this Behavior In C#如何在 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.节点文档中我可以看出,更新 function 只是将新数据添加到 hash 函数的输入中。 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");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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