Different results with Java's digest versus external utilities

前端 未结 1 1672
小蘑菇
小蘑菇 2021-01-30 01:25

I have written a simple Java class to generate the hash values of the Windows Calculator file. I am using Windows 7 Professional with SP1. I have tried Java

相关标签:
1条回答
  • 2021-01-30 01:58

    Got it. The Windows file system is behaving differently depending on the architecture of your process. This article explains it all - in particular:

    But what about 32-bit applications that have the system path hard coded and is running in a 64-bit Windows? How can they find the new SysWOW64 folder without changes in the program code, you might think. The answer is that the emulator redirects calls to System32 folder to the SysWOW64 folder transparently so even if the folder is hard coded to the System32 folder (like C:\Windows\System32), the emulator will make sure that the SysWOW64 folder is used instead. So same source code, that uses the System32 folder, can be compiled to both 32-bit and 64-bit program code without any changes.

    Try copying calc.exe to somewhere else... then run the same tools again. You'll get the same results as Java. Something about the Windows file system is giving different data to the tools than it's giving to Java... I'm sure it's something to do with it being in the Windows directory, and thus probably handled "differently".

    Furthermore, I've reproduced it in C#... and found out that it depends on the architecture of the process you're running. So here's a sample program:

    using System;
    using System.IO;
    using System.Security.Cryptography;
    
    class Test
    {
        static void Main()
        {
            using (var md5 = MD5.Create())
            {
                string path = "c:/Windows/System32/Calc.exe";
                var bytes = md5.ComputeHash(File.ReadAllBytes(path));
                Console.WriteLine(BitConverter.ToString(bytes));
            }
        }
    }
    

    And here's a console session (minus chatter from the compiler):

    c:\users\jon\Test>csc /platform:x86 Test.cs    
    
    c:\users\jon\Test>test
    60-B7-C0-FE-AD-45-F2-06-6E-5B-80-5A-91-F4-F0-FC
    
    c:\users\jon\Test>csc /platform:x64 Test.cs
    
    c:\users\jon\Test>test
    10-E4-A1-D2-13-2C-CB-5C-67-59-F0-38-CD-B6-F3-C9
    
    0 讨论(0)
提交回复
热议问题