如何从SHA-1字节数组生成Guid



我有这个代码来生成SHA-1哈希:

SHA1 sha1 = SHA1CryptoServiceProvider.Create();
Byte[] myStringBytes = ASCIIEncoding.Default.GetBytes(myString);
Byte[] hash = sha1.ComputeHash(myStringBytes);

有没有办法将hash转换为Guid(我想,类型5与SHA-1一致)?

您可以使用基于rfc4122的C#代码。

为了防止链接腐烂,这里有一些代码:

public static Guid Create(Guid namespaceId, string name)
{
    if (name == null)
        throw new ArgumentNullException("name");
    // convert the name to a sequence of octets (as defined by the standard or conventions of its namespace) (step 3)
    // ASSUME: UTF-8 encoding is always appropriate
    byte[] nameBytes = Encoding.UTF8.GetBytes(name);
    // convert the namespace UUID to network order (step 3)
    byte[] namespaceBytes = namespaceId.ToByteArray();
    SwapByteOrder(namespaceBytes);
    // comput the hash of the name space ID concatenated with the name (step 4)
    byte[] hash;
    using (HashAlgorithm algorithm =  SHA1.Create())
    {
        algorithm.TransformBlock(namespaceBytes, 0, namespaceBytes.Length, null, 0);
        algorithm.TransformFinalBlock(nameBytes, 0, nameBytes.Length);
        hash = algorithm.Hash;
    }
    // most bytes from the hash are copied straight to the bytes of the new GUID (steps 5-7, 9, 11-12)
    byte[] newGuid = new byte[16];
    Array.Copy(hash, 0, newGuid, 0, 16);
    // set the four most significant bits (bits 12 through 15) of the time_hi_and_version field to the appropriate 4-bit version number from Section 4.1.3 (step 8)
    newGuid[6] = (byte)((newGuid[6] & 0x0F) | (5 << 4));
    // set the two most significant bits (bits 6 and 7) of the clock_seq_hi_and_reserved to zero and one, respectively (step 10)
    newGuid[8] = (byte)((newGuid[8] & 0x3F) | 0x80);
    // convert the resulting UUID to local byte order (step 13)
    SwapByteOrder(newGuid);
    return new Guid(newGuid);
}
/// <summary>
/// The namespace for fully-qualified domain names (from RFC 4122, Appendix C).
/// </summary>
public static readonly Guid DnsNamespace = new Guid("6ba7b810-9dad-11d1-80b4-00c04fd430c8");
/// <summary>
/// The namespace for URLs (from RFC 4122, Appendix C).
/// </summary>
public static readonly Guid UrlNamespace = new Guid("6ba7b811-9dad-11d1-80b4-00c04fd430c8");
/// <summary>
/// The namespace for ISO OIDs (from RFC 4122, Appendix C).
/// </summary>
public static readonly Guid IsoOidNamespace = new Guid("6ba7b812-9dad-11d1-80b4-00c04fd430c8");
// Converts a GUID (expressed as a byte array) to/from network order (MSB-first).
internal static void SwapByteOrder(byte[] guid)
{
    SwapBytes(guid, 0, 3);
    SwapBytes(guid, 1, 2);
    SwapBytes(guid, 4, 5);
    SwapBytes(guid, 6, 7);
}
private static void SwapBytes(byte[] guid, int left, int right)
{
    byte temp = guid[left];
    guid[left] = guid[right];
    guid[right] = temp;
}

正如Justin所指出的,Guid每次都应该是唯一的,而对于相同的值,哈希每次都会给出一致的结果。

现在我想补充一点,并说Guid和哈希(如果不是所有算法的话,大多数算法)都会发生冲突,尽管我的直觉是哈希比Guid更容易发生冲突。。。尽管这可能受制于散列的大小(即128位、256位、512位等)。

您将遇到的另一个问题是,来自SHA1哈希的byte[]是20字节长,而Guid是16字节长,因此,从SHA1哈希创建Guid是不准确的。

示例:

string myString = "Hello World";
SHA1 sha1 = SHA1CryptoServiceProvider.Create();
Byte[] myStringBytes = ASCIIEncoding.Default.GetBytes(myString);
Byte[] hash = sha1.ComputeHash(myStringBytes);
Console.WriteLine(new Guid(hash.Take(16).ToArray()));

上面的例子将从你的散列创建一个Guid,尽管它使用LINQ从散列数组中获得16个字节(因此不准确…最后4个字节被简单地省略了)

MD5是一个16字节的散列,因此它似乎比SHA1更适合转换为Guid。

示例:

string myString = "Hello World";
MD5 md5 = MD5.Create();
Byte[] myStringBytes = ASCIIEncoding.Default.GetBytes(myString);
Byte[] hash = md5.ComputeHash(myStringBytes);
Console.WriteLine(new Guid(hash));

这会从MD5哈希中生成一个准确的Guid,尽管我会说,所有这些都是MD5哈希的Guid表示。。。在CCD_ 3数据中应该没有实际的改变。

相关内容

  • 没有找到相关文章

最新更新