二进制数据类型实际存储方式及SHA256密码字段C#对比疑问
Let’s break down your questions clearly—this is a super common point of confusion when working with raw byte data across databases and code.
1. What’s the actual storage format of binary(32) for SHA256 hashes?
SHA256 generates a 256-bit hash, which equals exactly 32 bytes—perfectly sized for a binary(32) field. The 0x81B637D8FCD2C6DA6359E6963113A1170DE795E4B725B84D1E0B4CFD9EC58CE9 string you see in SELECT results is just a human-readable hexadecimal representation of the raw bytes stored under the hood.
The database isn’t storing those ASCII characters (like '8', '1', 'B')—it’s saving the raw byte values directly. Each pair of hex characters maps to one byte: for example, 0x81 translates to a byte with the decimal value 129, and that’s exactly what’s stored in the database—no string formatting, just pure binary byte data.
2. Why does a C# byte array match without needing 0x prefix or uppercase conversion?
Your C# password hash function returns a byte[], which is a direct in-memory representation of the raw SHA256 bytes—this is exactly the same format the database uses for the binary(32) field.
When you pull the binary(32) value from the database into a C# byte[] (either via SqlDataReader.GetBytes() or an ORM that maps the field correctly), you’re getting the exact same sequence of byte values your hash function generated. There’s no string conversion happening here—you’re comparing raw binary data directly.
The 0x prefix and uppercase letters are only part of the database’s string formatting for display. Since neither your C# byte array nor the database’s stored data includes those formatting elements, a direct comparison (like using Enumerable.SequenceEqual() to check the byte arrays) will match perfectly without any extra processing.
For a quick example:
- If your hash function outputs a byte with value
0x81(decimal 129), that’s what lives in your C# array. - The database stores that same byte value
0x81(129) in thebinary(32)field. - When you compare the two, you’re checking if each byte in the array has the same numerical value—no string parsing or formatting required.
内容的提问来源于stack exchange,提问作者Black Panther

