DigestComparer.cs source code in C# .NET

Source code for the .NET framework in C#

                        

Code:

/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / cdf / src / WF / Common / Shared / DigestComparer.cs / 1305376 / DigestComparer.cs

                            using System; 
using System.Diagnostics;
using System.Collections.Generic;
using System.Globalization;
 
internal class DigestComparer : IComparer, IEqualityComparer
{ 
    int IComparer.Compare(byte[] digest1, byte[] digest2) 
    {
        Debug.Assert(digest1.Length == 16 && digest2.Length == 16, "Invalid digest!"); 

        for (int index = 0; index < 16; index++)
            if (digest1[index] != digest2[index])
                return (digest1[index] < digest2[index]) ? -1 : 1; 

        return 0; 
    } 

    bool IEqualityComparer.Equals(byte[] digest1, byte[] digest2) 
    {
        Debug.Assert(digest1.Length == 16 && digest2.Length == 16, "Invalid digest!");

        for (int index = 0; index < 16; index++) 
            if (digest1[index] != digest2[index])
                return false; 
 
        return true;
    } 

    int IEqualityComparer.GetHashCode(byte[] checksumBytes)
    {
        return GetMD5DigestString(checksumBytes).GetHashCode(); 
    }
 
    internal static string GetMD5DigestString(byte[] md5Digest) 
    {
        return string.Format(CultureInfo.InvariantCulture, "{0}{1}{2}{3}{4}{5}{6}{7}{8}{9}{10}{11}{12}{13}{14}{15}", md5Digest[0].ToString("X2", CultureInfo.InvariantCulture), md5Digest[1].ToString("X2", CultureInfo.InvariantCulture), md5Digest[2].ToString("X2", CultureInfo.InvariantCulture), md5Digest[3].ToString("X2", CultureInfo.InvariantCulture), md5Digest[4].ToString("X2", CultureInfo.InvariantCulture), md5Digest[5].ToString("X2", CultureInfo.InvariantCulture), md5Digest[6].ToString("X2", CultureInfo.InvariantCulture), md5Digest[7].ToString("X2", CultureInfo.InvariantCulture), md5Digest[8].ToString("X2", CultureInfo.InvariantCulture), md5Digest[9].ToString("X2", CultureInfo.InvariantCulture), md5Digest[10].ToString("X2", CultureInfo.InvariantCulture), md5Digest[11].ToString("X2", CultureInfo.InvariantCulture), md5Digest[12].ToString("X2", CultureInfo.InvariantCulture), md5Digest[13].ToString("X2", CultureInfo.InvariantCulture), md5Digest[14].ToString("X2", CultureInfo.InvariantCulture), md5Digest[15].ToString("X2", CultureInfo.InvariantCulture)); 
    }
}

// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.
using System; 
using System.Diagnostics;
using System.Collections.Generic;
using System.Globalization;
 
internal class DigestComparer : IComparer, IEqualityComparer
{ 
    int IComparer.Compare(byte[] digest1, byte[] digest2) 
    {
        Debug.Assert(digest1.Length == 16 && digest2.Length == 16, "Invalid digest!"); 

        for (int index = 0; index < 16; index++)
            if (digest1[index] != digest2[index])
                return (digest1[index] < digest2[index]) ? -1 : 1; 

        return 0; 
    } 

    bool IEqualityComparer.Equals(byte[] digest1, byte[] digest2) 
    {
        Debug.Assert(digest1.Length == 16 && digest2.Length == 16, "Invalid digest!");

        for (int index = 0; index < 16; index++) 
            if (digest1[index] != digest2[index])
                return false; 
 
        return true;
    } 

    int IEqualityComparer.GetHashCode(byte[] checksumBytes)
    {
        return GetMD5DigestString(checksumBytes).GetHashCode(); 
    }
 
    internal static string GetMD5DigestString(byte[] md5Digest) 
    {
        return string.Format(CultureInfo.InvariantCulture, "{0}{1}{2}{3}{4}{5}{6}{7}{8}{9}{10}{11}{12}{13}{14}{15}", md5Digest[0].ToString("X2", CultureInfo.InvariantCulture), md5Digest[1].ToString("X2", CultureInfo.InvariantCulture), md5Digest[2].ToString("X2", CultureInfo.InvariantCulture), md5Digest[3].ToString("X2", CultureInfo.InvariantCulture), md5Digest[4].ToString("X2", CultureInfo.InvariantCulture), md5Digest[5].ToString("X2", CultureInfo.InvariantCulture), md5Digest[6].ToString("X2", CultureInfo.InvariantCulture), md5Digest[7].ToString("X2", CultureInfo.InvariantCulture), md5Digest[8].ToString("X2", CultureInfo.InvariantCulture), md5Digest[9].ToString("X2", CultureInfo.InvariantCulture), md5Digest[10].ToString("X2", CultureInfo.InvariantCulture), md5Digest[11].ToString("X2", CultureInfo.InvariantCulture), md5Digest[12].ToString("X2", CultureInfo.InvariantCulture), md5Digest[13].ToString("X2", CultureInfo.InvariantCulture), md5Digest[14].ToString("X2", CultureInfo.InvariantCulture), md5Digest[15].ToString("X2", CultureInfo.InvariantCulture)); 
    }
}

// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.
                        

Link Menu

Network programming in C#, Network Programming in VB.NET, Network Programming in .NET
This book is available now!
Buy at Amazon US or
Buy at Amazon UK