2017-04-20 1 views
0

My MySQL 테이블에 비 암호화 해싱이 필요하므로 데이터를 더 빨리 쿼리 할 수 ​​있습니다. MySql 데이터베이스 해시 함수와 C# 응용 프로그램은 주어진 값에 대해 동일한 해시를 생성해야합니다. 단, 해당 값을 문자열로 유지해야합니다. 난 문자열 비교의 오버 헤드를 피할 수 있도록 그들을 BIGINT로 변환하고 싶습니다. 나는 Sha256이 암호화 해시 함수라는 것을 알고 있지만 적어도 MySql과 C#은 주어진 입력에 대해 동일한 해시 문자열을 생성하므로 비 암호화 용도로는 사용하지 않아도됩니다. MurmurHash3 X86과 같은 다른 온라인 해시 알고리즘을 시도했지만 해시 충돌이 있습니다. 어떤 도움을 주시면 감사하겠습니다. 감사!BIGINT로 변환하는 동안 MySql과 C# 해시가 일치하지 않습니다.

MySQL의 쿼리 :

SELECT SHA2('MyString', 256) AS Sha256, CONV(RIGHT(SHA2('MyString',256), 16), 16, 10) AS BIGINT_Sha256, MD5('MyString') AS MD_5, CONV(RIGHT(MD5('MyString'), 16), 16, 10) AS BIGINT_MD5; 

C# 코드 :

static void Main(string[] args) 
    { 
     using (var sha256 = SHA256.Create()) 
     { 
      var hashBytes = sha256.ComputeHash(Encoding.ASCII.GetBytes("MyString")); 
      var hash = BitConverter.ToString(hashBytes).Replace("-", "").ToLower(); 

      Console.WriteLine(hash); 
     } 

     using (var sha256 = SHA256.Create()) 
     { 
      var hashBytes = sha256.ComputeHash(Encoding.ASCII.GetBytes("MyString")); 
      var hash = BitConverter.ToInt64(hashBytes, 0); 

      Console.WriteLine(hash); 
     } 

     using (var md5 = MD5.Create()) 
     { 
      var hashBytes = md5.ComputeHash(Encoding.ASCII.GetBytes("MyString")); 
      var hash = BitConverter.ToString(hashBytes).Replace("-", "").ToLower(); 

      Console.WriteLine(hash); 
     } 

     using (var md5 = MD5.Create()) 
     { 
      var hashBytes = md5.ComputeHash(Encoding.ASCII.GetBytes("MyString")); 
      var hash = BitConverter.ToInt64(hashBytes, 0); 

      Console.WriteLine(hash); 
     } 

     Console.ReadLine(); 
    } 

MySQL의 결과 :

MySql Result

C 번호 결과 :

enter image description here

+0

어디에서 C#으로'BigInteger'로 변환합니까? 코드의 해당 부분을 게시 할 수 있습니까? –

+0

@LukePark이 줄 [var hash = BitConverter.ToInt64 (hashBytes, 0);] 내 C# 코드에서 BigInteger로 변환합니다. –

+0

아니요, 그렇지 않습니다. 이것은 64 비트 정수로 변환됩니다. SHA256 해시는 이름에서 알 수 있듯이 길이가 256 비트입니다. –

답변

0

answer은이 문제를 해결하는 데 도움이되었습니다.

오른쪽 16자를 사용하는 대신, MySQL 측에서 LEFT로 변경했습니다.

MySQL의 쿼리 :

SET @Value = 'MyString'; 
SELECT 
    SHA2(@Value, 256) AS Sha256, 
    CAST(CONV(LEFT(SHA2(@Value,256), 16), 16, 10) AS INT) AS BIGINT_Sha256, 
    MD5(@Value) AS MD_5, 
    CAST(CONV(LEFT(MD5(@Value), 16), 16, 10) AS INT) AS BIGINT_MD5; 

C# 코드 :

static void Main(string[] args) 
    { 
     var value = "MyString"; 

     using (var sha256 = SHA256.Create()) 
     { 
      var hashBytes = sha256.ComputeHash(Encoding.UTF8.GetBytes(value)); 
      var hash = BitConverter.ToString(hashBytes).Replace("-", "").ToLower(); 

      Console.WriteLine(hash); 
     } 

     using (var sha256 = SHA256.Create()) 
     { 
      var hashBytes = sha256.ComputeHash(Encoding.UTF8.GetBytes(value)); 
      var hash = BitConverter.ToInt64(hashBytes.Take(8).ToArray(), 0); 
      hash = IPAddress.HostToNetworkOrder(hash); 

      Console.WriteLine(hash); 
     } 

     using (var md5 = MD5.Create()) 
     { 
      var hashBytes = md5.ComputeHash(Encoding.UTF8.GetBytes(value)); 
      var hash = BitConverter.ToString(hashBytes).Replace("-", "").ToLower(); 

      Console.WriteLine(hash); 
     } 

     using (var md5 = MD5.Create()) 
     { 
      var hashBytes = md5.ComputeHash(Encoding.UTF8.GetBytes(value)); 
      var hash = BitConverter.ToInt64(hashBytes.Take(8).ToArray(), 0); 
      hash = IPAddress.HostToNetworkOrder(hash); 

      Console.WriteLine(hash); 
     } 

     Console.ReadLine(); 
    } 

MySQL의 결과 :

MySql Result

C 번호 결과 :

C# Result

3 천만 개가 넘는 레코드를 테스트하고 대답을 업데이트합니다.

관련 문제