2012-01-11 2 views
3

저는 C#이 정말 새롭고 전임자가 생성 한 코드를보고 있습니다. 여기 코드는 다음과 같습니다추가 해시 알고리즘 설정

public static string ComputeHash(string plainText, 
            string hashAlgorithm, byte[] saltBytes) 
    { 
     if (saltBytes == null) 
      saltBytes = CreateSalt(8); 

     // Convert plain text into a byte array. 
     byte[] plainTextBytes = Encoding.UTF8.GetBytes(plainText); 

     // Allocate array, which will hold plain text and salt. 
     byte[] plainTextWithSaltBytes = 
       new byte[plainTextBytes.Length + saltBytes.Length]; 

     // Copy plain text bytes into resulting array. 
     for (int i = 0; i < plainTextBytes.Length; i++) 
      plainTextWithSaltBytes[i] = plainTextBytes[i]; 

     // Append salt bytes to the resulting array. 
     for (int i = 0; i < saltBytes.Length; i++) 
      plainTextWithSaltBytes[plainTextBytes.Length + i] = saltBytes[i]; 

     // Because we support multiple hashing algorithms, we must define 
     // hash object as a common (abstract) base class. We will specify the 
     // actual hashing algorithm class later during object creation. 
     HashAlgorithm hash; 

     // Make sure hashing algorithm name is specified. 
     if (hashAlgorithm == null) 
      hashAlgorithm = ""; 

     // Initialize appropriate hashing algorithm class. 
     switch (hashAlgorithm.ToUpper()) 
     { 
      case "SHA1": 
       hash = new SHA1Managed(); 
       break; 

      case "SHA256": 
       hash = new SHA256Managed(); 
       break; 

      case "SHA384": 
       hash = new SHA384Managed(); 
       break; 

      case "SHA512": 
       hash = new SHA512Managed(); 
       break; 

      default: 
       hash = new MD5CryptoServiceProvider(); 
       break; 
     } 

     // Compute hash value of our plain text with appended salt. 
     byte[] hashBytes = hash.ComputeHash(plainTextWithSaltBytes); 

     // Create array which will hold hash and original salt bytes. 
     byte[] hashWithSaltBytes = new byte[hashBytes.Length + 
              saltBytes.Length]; 

     // Copy hash bytes into resulting array. 
     for (int i = 0; i < hashBytes.Length; i++) 
      hashWithSaltBytes[i] = hashBytes[i]; 

     // Append salt bytes to the result. 
     for (int i = 0; i < saltBytes.Length; i++) 
      hashWithSaltBytes[hashBytes.Length + i] = saltBytes[i]; 

     // Convert result into a base64-encoded string. 
     string hashValue = Convert.ToBase64String(hashWithSaltBytes); 

     // Return the result. 
     return hashValue; 
    } 

    public static bool VerifyHash(string plainText, 
            string hashAlgorithm, 
            string hashValue) 
    { 
     // Convert base64-encoded hash value into a byte array. 
     byte[] hashWithSaltBytes = Convert.FromBase64String(hashValue); 

     // We must know size of hash (without salt). 
     int hashSizeInBits, hashSizeInBytes; 

     // Make sure that hashing algorithm name is specified. 
     if (hashAlgorithm == null) 
      hashAlgorithm = ""; 

     // Size of hash is based on the specified algorithm. 
     switch (hashAlgorithm.ToUpper()) 
     { 
      case "SHA1": 
       hashSizeInBits = 160; 
       break; 

      case "SHA256": 
       hashSizeInBits = 256; 
       break; 

      case "SHA384": 
       hashSizeInBits = 384; 
       break; 

      case "SHA512": 
       hashSizeInBits = 512; 
       break; 

      default: // Must be MD5 
       hashSizeInBits = 128; 
       break; 
     } 

     // Convert size of hash from bits to bytes. 
     hashSizeInBytes = hashSizeInBits/8; 

     // Make sure that the specified hash value is long enough. 
     if (hashWithSaltBytes.Length < hashSizeInBytes) 
      return false; 

     // Allocate array to hold original salt bytes retrieved from hash. 
     byte[] saltBytes = new byte[hashWithSaltBytes.Length - 
            hashSizeInBytes]; 

     // Copy salt from the end of the hash to the new array. 
     for (int i = 0; i < saltBytes.Length; i++) 
      saltBytes[i] = hashWithSaltBytes[hashSizeInBytes + i]; 

     // Compute a new hash string. 
     string expectedHashString = 
        ComputeHash(plainText, hashAlgorithm, saltBytes); 

     // If the computed hash matches the specified hash, 
     // the plain text value must be correct. 
     return (hashValue == expectedHashString); 
    } 

이 회사는 보안 표준을 업그레이드 및 SHA-1, 3DES (삼중 DES) 또는 AES MAC 등의 보안 해싱 알고리즘을 필요로하고있다. 나는 그들을 어디에 넣어야할지 모른다. 누군가 도와 주시겠습니까?

+3

3DES는 해시 알고리즘이 아니며 AES MAC은 해시가 아닌 MAC처럼 들립니다. –

+0

@KerrekSB는 3DES가 ​​아닌 Triple DES입니까? 나는 그것이 해싱 알고리즘이라고 생각한다.Poly1305-AES 기반의 MAC (메시지 인증 코드)가 아닙니까? 내가 틀렸다면 나에게 정정해라. 내가이 모든 것에 정말로 새로운 사람이기 때문이다. –

+4

당신은이 문제에 대해 매우 확고하게 이해하고 그 질문에 직접 답할 때까지는 암호화 토픽 주위에 매우 넓은 선상을 유지해야합니다! (3DES (= Triple DES)는 * cipher *이고 MAC은 해시가 아닌 MAC입니다. 해시 (예 : HMAC)를 사용하여 MAC을 디자인 할 수는 있지만 별도의 주제입니다. –

답변

1

"AES MAC"으로 가정하면 Poly1305-AES을 말하는 것입니다. Poly1305-AES는 간단한 해시가 아니며 AES 키가 필요하며 두 엔터티간에 통신 할 때 사용되는 128 비트 nonce이 필요합니다.

3DES (Triple DES)는 암호화 암호이므로 메시지의 인증 또는 무결성을 보장하지 않습니다. 3DES의 유일한 기능은 메시지의 기밀성을 보장하는 것입니다. 이되었습니다 나는 당신이 이러한 새로운 보안 표준이 무엇인지에 대한 공식적인 설명을 얻을 제안 broken

2005 년부터 관련하여

SHA-1, 당신은 더 이상 해시를 사용하지합니다. 당신이 나열한 것들 중 2 개는 해시 알고리즘조차도 아니며, 3 번째는 나쁜 선택입니다. 현재 구현 된 SHA-256 이상에 나열된 해시 알고리즘 중 괜찮은 것이어야합니다 (일명 SHA-2 카테고리). 이것들은 현재 내가 알고있는 발행 된 취약점이 없습니다.

사이드 노트 : 아마도 바이트를 반복하는 대신 arraycopy을 사용하고 싶을 것입니다.

0

.NET Framework 기본 클래스 라이브러리에는 아마도 유용 할만한 HMAC type이 있습니다.

my C# password utilities library의 전체 또는 일부를 사용할 수도 있습니다. 그러나 더 많은 해시 유형을 추가하려면이를 조정해야합니다. 나는이 라이브러리가 왜 그리고 어떻게 구성되었는지를 설명하는 series of blog entries을 썼다.

2

회사는 보안 표준을 업그레이드했으며 SHA-1, 3DES (트리플 DES) 또는 AES MAC과 같은 보안 해시 알고리즘이 필요합니다.

우선, 당신은 이미 SHA-1을 가지고 있으며, 그 해시 알고리즘은 SHA-256/512보다 약간 약하지만, 여전히 꽤 좋습니다. 수퍼 컴퓨터을 사용하여 10 년 동안 메시지를 위반 한 악당을 다루는 경우가 아니면 SHA-512 will keep you very safe을 고수하십시오. MACs는 예컨대 SHA-2와 해싱 알고리즘을 사용 을 생성하는 동안 다른 두 알고리즘으로서

3DES가 대칭 사이퍼와 해시 부적합하다 (차이는 함께 당신에게 "비밀 키"를 해시이다 메시지 ("고정 소금 '같은)은 해싱을 위해, 따라서 또한 적합하지 AES 또한 대칭 사이퍼입니다. 그것의 진위를 확인하고 있습니다.

this page을 확인하기 위해 기업에서 사람에게 이러한 해시 함수 중 하나에 만족 (다른 말로하면 아무 것도 변경하지 마십시오.) 암호화에 익숙하지 않은 경우 시스템에 관계없이 시스템을 안전하지 않게 만들 수 있습니다. 힌지 선택.