使用openssl处理文件与在线哈希工具生成的MD5哈希值不一致问题
Great question! Let's break down why the openssl dgst -md5 output and the online tool's result don't match—even though you're targeting what seems like the same string.
The core issue here is that you're hashing two different sets of bytes, even if you think they're identical. Here are the most likely culprits:
Hidden line break in your file
This is the most common gotcha. If you createdfile.txtusing a command likeecho "computer" > file.txt(on Linux/macOS or Windows), most shells automatically append a newline character (\non Unix-like systems,\r\non Windows) to the end of the file. Even if you can't see it, that extra byte changes the entire hash. Your online tool is hashing just the 8-byte string"computer", whileopensslis hashing those 8 bytes plus the newline.To test this, try recreating the file without the newline:
echo -n "computer" > file.txtThen run
openssl dgst -md5 file.txtagain—it should match the online tool's hash.UTF-8 BOM (Byte Order Mark)
If you savedfile.txtusing a text editor that adds a UTF-8 BOM (like some versions of Notepad on Windows), the file starts with three invisible bytes:EF BB BF. These get included in the hash calculation, making it different from the plain "computer" string the online tool uses.Wrong file encoding
If your file is saved in a non-ASCII encoding (like UTF-16 instead of UTF-8/ASCII), each character takes up 2 bytes instead of 1. For "computer", that would be 16 total bytes instead of 8—completely changing the hash output.Invisible whitespace characters
It's possible your file contains a non-printable whitespace character (like a full-width space or tab) that you can't see butopensslstill reads and hashes. You can verify the exact bytes in the file using a tool likehexdump(on Unix-like systems) orfc /b(on Windows).
To confirm exactly what's in your file, run this command to view its raw bytes:
hexdump -C file.txt
For the plain string "computer", you should see:
00000000 63 6f 6d 70 75 74 65 72 |computer| 00000008
If you see any extra bytes here (like 0a at the end for a newline, or ef bb bf at the start), that's why your hashes don't match.
内容的提问来源于stack exchange,提问作者yak

