2013-02-13 2 views
4

내 C++ 코드에서 Sha256 클래스의 업데이트 기능을 사용하여 하나의 해시 값에 문자열을 포함하지만 Sha256 .net 클래스에서이 함수를 찾을 수 없습니다. 이 함수는 Sha의 자바 구현이지만 Cnet 구현에는 제공되지 않지만 .net에서는 제공되지 않습니다. C++에Sha256에서 .net의 업데이트 기능은 어디에 있습니까?

샘플 코드 :

l_ceSHA2.Init(); 
for (l_dwordCnt = 0; l_dwordCnt < l_dwordHashRounds; l_dwordCnt++) 
{ 
    l_ceSHA2.Update(mp_strPassword, strlen(mp_strPassword))); 
    l_ceSHA2.Update(mp_byteSalt, 32); 
} 
l_ceSHA2.Final(mp_byteCryptoKey); 

그래서, 그것은 PBKDF처럼,하지만 쉽게.

Sha256 code in C for reference

+0

'.Update()'는 문자열을 연결하고 해시를 계산하는 데 다른 결과를 제공합니까? – TheEvilPenguin

+0

문자열이 너무 큽니다. HashRounds가 많이 있습니다. – crea7or

답변

5

당신은 TransformBlock를 사용할 수 있습니다.

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

// Example code for using TransformBlock to hash data in chunks 

namespace HashTest 
{ 
    class HashTest 
    { 
     static void Main(string[] args) 
     { 
      SHA256 hash = SHA256.Create(); 
      ASCIIEncoding encoding = new ASCIIEncoding(); 
      string password = "password"; 

      // Hash a string using ComputeHash 
      string sourcetext = password; 
      Console.WriteLine(sourcetext); 
      byte[] sourcebytes = encoding.GetBytes(sourcetext); 
      byte[] hashBytes = hash.ComputeHash(sourcebytes); 
      string hashStr = BitConverter.ToString(hashBytes).Replace("-", ""); 
      Console.WriteLine(hashStr); 

      // Hash exactly two copies of a string 
      // (used to cross verify other methods below). 
      Console.WriteLine(); 
      sourcetext = password + password; 
      Console.WriteLine(sourcetext); 
      sourcebytes = encoding.GetBytes(sourcetext); 
      hashBytes = hash.ComputeHash(sourcebytes); 
      hashStr = BitConverter.ToString(hashBytes).Replace("-", ""); 
      Console.WriteLine(hashStr); 

      // Hash a string using TransformFinalBlock 
      Console.WriteLine(); 
      sourcetext = password; 
      sourcebytes = encoding.GetBytes(sourcetext); 
      Console.WriteLine(sourcetext); 
      hash.TransformFinalBlock(sourcebytes, 0, sourcebytes.Length); 
      hashBytes = hash.Hash; 
      hashStr = BitConverter.ToString(hashBytes).Replace("-", ""); 
      Console.WriteLine(hashStr); 

      // At this point we've finalized the hash. To 
      // reuse it we must first call Initialize(). 

      // Hash string twice using TransformBlock/TransformFinalBlock 
      Console.WriteLine(); 
      hash.Initialize(); 
      sourcetext = password; 
      sourcebytes = encoding.GetBytes(sourcetext); 
      Console.Write(sourcetext); 
      hash.TransformBlock(sourcebytes, 0, sourcebytes.Length, null, 0); 
      Console.WriteLine(sourcetext); 
      hash.TransformFinalBlock(sourcebytes, 0, sourcebytes.Length); 
      hashBytes = hash.Hash; 
      hashStr = BitConverter.ToString(hashBytes).Replace("-", ""); 
      Console.WriteLine(hashStr); 

      // Hash string twice using TransformBlock in a loop 

      Console.WriteLine(); 
      hash.Initialize(); 
      sourcetext = password; 
      sourcebytes = encoding.GetBytes(sourcetext); 
      for (int i = 0; i < 2; ++i) 
      { 
       Console.Write(sourcetext); 
       hash.TransformBlock(sourcebytes, 0, sourcebytes.Length, null, 0); 
      } 
      Console.WriteLine(); 
      hash.TransformFinalBlock(sourcebytes, 0, 0); 
      hashBytes = hash.Hash; 
      hashStr = BitConverter.ToString(hashBytes).Replace("-", ""); 
      Console.WriteLine(hashStr); 
     } 
    } 
} 
+0

정확히 내가 원하는 것. 그들이 왜 이름을 바꾸 었는지 짐작할 수 있습니다 ... – crea7or

+0

다행이 도움이되었습니다. 감사. Microsoft의 TransformBlock이보다 일반화되어 있기 때문에 이름이 바뀐 것 같아요. 해시뿐 아니라 암호화 및 해독에도 사용됩니다. – jimhark