2015-09-27 14 views
7

Trong khi mã hóa trước đó tôi nhận thấy một cái gì đó kỳ lạ về SHA256, trong đó có vẻ như để tạo ra số nguyên nhiều hơn so với chữ cái cho băm. Lúc đầu, tôi nghĩ rằng tôi chỉ đang tưởng tượng nó, vì vậy tôi đặt cùng một bài kiểm tra nhanh để đảm bảo. Thật đáng ngạc nhiên, thử nghiệm của tôi dường như chứng minh rằng SHA256 ưu tiên các giá trị số nguyên trong băm mà nó tạo ra. Tôi muốn biết lý do tại sao. Không nên sự khác biệt giữa một chỉ số băm là một chữ cái và một số là chính xác giống nhau? Dưới đây là ví dụ thử nghiệm của tôi:SHA256 có ưu tiên số nguyên không?

namespace TestingApp 
{ 
    static class Program 
    { 
     private static string letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"; 
     private static char[] characters = letters.ToCharArray(); 
     private static Random _rng = new Random(); 

     static void Main(string[] args) 
     { 
      int totalIntegers = 0; 
      int totalLetters = 0; 
      for (int testingIntervals = 0; testingIntervals < 3000; testingIntervals++) 
      { 
       string randomString = NextString(10); 
       string checksum = DreamforceChecksum.GenerateSHA256(randomString); 
       int integerCount = checksum.Count(Char.IsDigit); 
       int letterCount = checksum.Count(Char.IsLetter); 
       Console.WriteLine("String: " + randomString); 
       Console.WriteLine("Checksum: " + checksum); 
       Console.WriteLine("Integers: " + integerCount); 
       Console.WriteLine("Letters: " + letterCount); 
       totalIntegers += integerCount; 
       totalLetters += letterCount; 
      } 
      Console.WriteLine("Total Integers: " + totalIntegers); 
      Console.WriteLine("Total Letters: " + totalLetters); 
      Console.Read(); 
     } 

     private static string NextString(int length) 
     { 
      StringBuilder builder = new StringBuilder(); 
      for (int i = 0; i < length; i++) 
      { 
       builder.Append(characters[_rng.Next(characters.Length)]); 
      } 
      return builder.ToString(); 
     } 
    } 
} 

và lớp checksum/băm của tôi:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Security.Cryptography; 
using System.Text; 
using System.Threading.Tasks; 

namespace DreamforceFramework.Framework.Cryptography 
{ 
    public static class DreamforceChecksum 
    { 
     private static readonly SHA256Managed _shaManagedInstance = new SHA256Managed(); 
     private static readonly StringBuilder _checksumBuilder = new StringBuilder(); 
     public static string GenerateSHA256(string text) 
     { 
      byte[] bytes = Encoding.UTF8.GetBytes(text); 
      byte[] hash = _shaManagedInstance.ComputeHash(bytes); 
      _checksumBuilder.Clear(); 
      for (int index = 0; index < hash.Length; index++) 
      { 
       _checksumBuilder.Append(hash[index].ToString("x2")); 
      } 
      return _checksumBuilder.ToString(); 
     } 

     public static byte[] GenerateSHA256Bytes(string text) 
     { 
      byte[] bytes = Encoding.UTF8.GetBytes(text); 
      byte[] hash = _shaManagedInstance.ComputeHash(bytes); 
      _checksumBuilder.Clear(); 
      for (int index = 0; index < hash.Length; index++) 
      { 
       _checksumBuilder.Append(hash[index].ToString("x2")); 
      } 
      return Encoding.UTF8.GetBytes(_checksumBuilder.ToString()); 
     } 

     public static bool ValidateDataIntegrity(string data, string targetHashcode) 
     { 
      return GenerateSHA256(data).Equals(targetHashcode); 
     } 
    } 
} 

Tôi đã chạy thử nghiệm của tôi nhiều lần, và mỗi lần có vẻ như nhiều số nguyên được tạo ra trong băm hơn chữ . Dưới đây là 3 thử nghiệm chạy:

enter image description here

enter image description here

enter image description here

Có ai biết tại sao SHA256 dường như ủng hộ số thay vì một phân phối bình đẳng của cả chữ và số?

+1

SHA-256 (như gần như tất cả băm) xuất ra byte thô. Chúng chỉ biến thành ký tự khi bạn áp dụng mã hóa, hex trong trường hợp của bạn. Sự phân bố các ký tự là một thuộc tính của mã hóa đó. – CodesInChaos

Trả lời

25

Cho rằng có 10 chữ số và 6 chữ cái có thể, tỷ lệ phải xấp xỉ 10: 6. Điều đó phù hợp với kết quả của bạn.

+0

Mỗi 0-9a-f được phân bố đều. – mksteve

+3

Người đàn ông, tôi đã bỏ lỡ điều đó như thế nào? – Krythic

2

Đầu ra là hệ thập lục phân. 0-9 và a-f