none
Saving a text file as a hash to compare faster RRS feed

  • Question

  • Hi all,

    I'm looking for some information on hashes in c#. I have some stored procedures (SP's) I would like to compare.

    I know that I can save the SP's to a List<> or an array and compare them either using Except or SequenceEqual and such but, is there a way to save or read the SP's into a hash and compare the hashes at all?

    Any suggestions or advice would be greatly appreciated.

    Thank you.


    CuriousCoder

    Thursday, October 19, 2017 3:21 PM

Answers

  • Hello CuriousCoder15,

    If you want to compare the text files ,it's a good choice by hash value .Base on your description I have created  a simple demo that check whether the both files are the same.

    string p_1 = @"D:\T1.txt";
    
                string p_2 = @"D:\T2.txt";     
    
                var hash = System.Security.Cryptography.HashAlgorithm.Create();
    
                //Calculate the hash value of the First file
    
                var stream_1 = new System.IO.FileStream(p_1, System.IO.FileMode.Open);
    
                byte[] hashByte_1 = hash.ComputeHash(stream_1);
    
                stream_1.Close();
    
                //Calculate the hash value of the second file
    
                var stream_2 = new System.IO.FileStream(p_2, System.IO.FileMode.Open);
    
                byte[] hashByte_2 = hash.ComputeHash(stream_2);
    
                stream_2.Close();
    
                //Compare the hash value
    
                if (BitConverter.ToString(hashByte_1) == BitConverter.ToString(hashByte_2))
    
                    Console.WriteLine("The both files are the same");
    
                else
    
                    Console.WriteLine("Not equal");
    
            }

    Hope this would be helpful.If you have any issues with my reply please feel free to contact me.

    Sincerely,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by CuriousCoder15 Tuesday, October 24, 2017 10:23 AM
    Friday, October 20, 2017 9:02 AM
    Moderator

All replies

  • Do you mean compare the code , the TSQL in the stored procedures? Or compare the results of the stored procedures?


    My Technet Articles

    If you like this or another reply, vote it up!
    If you think this or another reply answers the original question, mark it or propose it as an answer.


    Mauricio Feijo
    www.mauriciofeijo.com

    Thursday, October 19, 2017 4:35 PM
  • Yes I mean comparing the TSQL stored procedure code.

    Thanks


    CuriousCoder

    Thursday, October 19, 2017 4:57 PM
  • Also, do you need to simply determine if they are equal or do you need to determine the specific changes.

    I am sorry that I don't know much about SPs. I assume they need to be exported to exist as text files. If that can be done however then there are plenty of utilities that can find duplicate files.

    Also if you just need to get a hash code from a string then I think I found answers by just searching MSDN for hash text string. Do the following help?



    Sam Hobbs
    SimpleSamples.Info


    Thursday, October 19, 2017 5:24 PM
  • Hi, yes the SP's can be saved as standard text files, i was thinking to readalllines into an array then remove all white space with regx expression and finally convert them to a hash md5 for quick comparison.

    I will have a look at the links you provided too, thank you.


    CuriousCoder

    Thursday, October 19, 2017 5:42 PM
  • Hello CuriousCoder15,

    If you want to compare the text files ,it's a good choice by hash value .Base on your description I have created  a simple demo that check whether the both files are the same.

    string p_1 = @"D:\T1.txt";
    
                string p_2 = @"D:\T2.txt";     
    
                var hash = System.Security.Cryptography.HashAlgorithm.Create();
    
                //Calculate the hash value of the First file
    
                var stream_1 = new System.IO.FileStream(p_1, System.IO.FileMode.Open);
    
                byte[] hashByte_1 = hash.ComputeHash(stream_1);
    
                stream_1.Close();
    
                //Calculate the hash value of the second file
    
                var stream_2 = new System.IO.FileStream(p_2, System.IO.FileMode.Open);
    
                byte[] hashByte_2 = hash.ComputeHash(stream_2);
    
                stream_2.Close();
    
                //Compare the hash value
    
                if (BitConverter.ToString(hashByte_1) == BitConverter.ToString(hashByte_2))
    
                    Console.WriteLine("The both files are the same");
    
                else
    
                    Console.WriteLine("Not equal");
    
            }

    Hope this would be helpful.If you have any issues with my reply please feel free to contact me.

    Sincerely,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by CuriousCoder15 Tuesday, October 24, 2017 10:23 AM
    Friday, October 20, 2017 9:02 AM
    Moderator
  • Thank you, Neil, I will have a play with your idea and let you know how it goes.

    It looks like what I was looking for.

    Thanks again.


    CuriousCoder

    Friday, October 20, 2017 12:03 PM
  • Hi Neil,

    is there a way to make this a class with methods for comparing hashes and calculating the hash value in a generic way so I may compare multiple hashes in a folder?

    Thanks for your help.


    CuriousCoder

    Tuesday, October 24, 2017 10:47 AM
  • Hello CuriousCoder15,

    Just edit some code as below

     public class T1{
    
            public static Boolean CompareHash(string p_1, string p_2)
            {
                var hash = System.Security.Cryptography.HashAlgorithm.Create();
                var stream_1 = new System.IO.FileStream(p_1, System.IO.FileMode.Open);
                byte[] hashByte_1 = hash.ComputeHash(stream_1);
                stream_1.Close();
                //Calculate the hash value of the second file
                var stream_2 = new System.IO.FileStream(p_2, System.IO.FileMode.Open);
                byte[] hashByte_2 = hash.ComputeHash(stream_2);
                stream_2.Close();
                //Compare the hash value
                if (BitConverter.ToString(hashByte_1) == BitConverter.ToString(hashByte_2))
                    return true;
                else
                    return false;
            }
        }

    Sincerely,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Sunday, October 29, 2017 11:11 AM
    Moderator