14,954
edits
(→密碼) |
|||
| Line 78: | Line 78: | ||
** (1) HEX: CHAR(64) Using [http://php.net/manual/en/function.hash-file.php PHP: hash_file()], MySQL [https://dev.mysql.com/doc/refman/5.6/en/encryption-functions.html#function_sha2 SHA2(str, hash_length)] | ** (1) HEX: CHAR(64) Using [http://php.net/manual/en/function.hash-file.php PHP: hash_file()], MySQL [https://dev.mysql.com/doc/refman/5.6/en/encryption-functions.html#function_sha2 SHA2(str, hash_length)] | ||
** (2) Binary: BINARY(32) Using [http://php.net/manual/en/function.hex2bin.php hex2bin()] in PHP e.g. {{kbd | key=<nowiki>echo hex2bin(hash_file('sha256', $file_path));</nowiki>}}, In MySQL e.g. {{kbd | key=<nowiki>SELECT UNHEX(SHA2('The quick brown fox jumped over the lazy dog.', 256))</nowiki>}} | ** (2) Binary: BINARY(32) Using [http://php.net/manual/en/function.hex2bin.php hex2bin()] in PHP e.g. {{kbd | key=<nowiki>echo hex2bin(hash_file('sha256', $file_path));</nowiki>}}, In MySQL e.g. {{kbd | key=<nowiki>SELECT UNHEX(SHA2('The quick brown fox jumped over the lazy dog.', 256))</nowiki>}} | ||
==== Retrieve the hash value from string or file content ==== | |||
PHP: | PHP: | ||
| Line 98: | Line 100: | ||
SELECT SHA2('The quick brown fox jumped over the lazy dog.', 256) | SELECT SHA2('The quick brown fox jumped over the lazy dog.', 256) | ||
</pre> | </pre> | ||
==== Retrieve the hash value from binary value ==== | |||
* PHP: [http://php.net/manual/en/function.bin2hex.php bin2hex()] e.g. {{kbd | key=<nowiki>echo bin2hex(hex2bin(hash('sha256', 'The quick brown fox jumped over the lazy dog.')));</nowiki>}} | |||
* MySQL: [https://dev.mysql.com/doc/refman/8.0/en/hexadecimal-literals.html Hexadecimal Literals] e.g. {{kbd | key=<nowiki>SELECT HEX(UNHEX(SHA2('The quick brown fox jumped over the lazy dog.', 256)));</nowiki>}}<ref>[https://stackoverflow.com/questions/14608413/storing-a-binary-sha1-hash-into-a-mysql-binary20-column insert - Storing a binary SHA1 hash into a mySQL BINARY(20) column - Stack Overflow]</ref> | |||
=== membership === | === membership === | ||