Bug #54661 sha2() returns BINARY result
Submitted: 21 Jun 2010 10:25 Modified: 27 Jul 2010 0:45
Reporter: Alexander Barkov Email Updates:
Status: Closed Impact on me:
None 
Category:MySQL Server: Data Types Severity:S3 (Non-critical)
Version:5.5.3-m3, 5.6.99-m4 OS:Any
Assigned to: CPU Architecture:Any

[21 Jun 2010 10:25] Alexander Barkov
Description:
SHA2() reports BINARY collation.

How to repeat:
mysql> select sha(1), sha2('1',224);
Field   1:  `sha(1)`
Catalog:    `def`
Database:   ``
Table:      ``
Org_table:  ``
Type:       VAR_STRING
Collation:  utf8_general_ci (33)
Length:     120
Max_length: 40
Decimals:   31
Flags:      NOT_NULL 

Field   2:  `sha2('1',224)`
Catalog:    `def`
Database:   ``
Table:      ``
Org_table:  ``
Type:       VAR_STRING
Collation:  binary (63)
Length:     56
Max_length: 56
Decimals:   31
Flags:      BINARY 

+------------------------------------------+----------------------------------------------------------+
| sha(1)                                   | sha2('1',224)                                            |
+------------------------------------------+----------------------------------------------------------+
| 356a192b7913b04c54574d18c28d46e6395428ab | e25388fde8290dc286a6164fa2d97e551b53498dcbf7bc378eb1f178 |
+------------------------------------------+----------------------------------------------------------+
1 row in set (0.00 sec)

mysql> select sha(1), sha2('1',224)\G
*************************** 1. row ***************************
       sha(1): 356a192b7913b04c54574d18c28d46e6395428ab
sha2('1',224): e25388fde8290dc286a6164fa2d97e551b53498dcbf7bc378eb1f178
1 row in set (0.00 sec)

Suggested fix:
Make sha2() honour the current @@connection_collation value
like sha() does.
[21 Jun 2010 10:46] Valeriy Kravchuk
Verified just as described:

valeriy-kravchuks-macbook-pro:opt-bakporting openxs$ bin/mysql -uroot --column-type test
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.6.99-m4-debug Source distribution

Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
This software comes with ABSOLUTELY NO WARRANTY. This is free software,
and you are welcome to modify and redistribute it under the GPL v2 license

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> select sha(1), sha2('1',224);
Field   1:  `sha(1)`
Catalog:    `def`
Database:   ``
Table:      ``
Org_table:  ``
Type:       VAR_STRING
Collation:  utf8_general_ci (33)
Length:     120
Max_length: 40
Decimals:   31
Flags:      NOT_NULL 

Field   2:  `sha2('1',224)`
Catalog:    `def`
Database:   ``
Table:      ``
Org_table:  ``
Type:       VAR_STRING
Collation:  binary (63)
Length:     56
Max_length: 56
Decimals:   31
Flags:      BINARY 
...
[7 Jul 2010 6:51] Bugs System
A patch for this bug has been committed. After review, it may
be pushed to the relevant source trees for release in the next
version. You can access the patch from:

  http://lists.mysql.com/commits/112995

3095 Alexander Barkov	2010-07-07
      Bug#54661 sha2() returns BINARY result
      
      Problem: sha2() reported its result as BINARY
      
      Fix:
      - Inheriting Item_func_sha2 from Item_str_ascii_func
      - Setting max_length via fix_length_and_charset() 
        instead of direct assignment.
      - Adding tests
[7 Jul 2010 13:03] Alexander Barkov
Pushed into mysql-trunk-bugfixing (5.5.6-m3)
Pushed into mysql-next-mr-bugfixing (5.6.99-m4)
[23 Jul 2010 12:27] Bugs System
Pushed into mysql-trunk 5.5.6-m3 (revid:alik@sun.com-20100723121820-jryu2fuw3pc53q9w) (version source revid:vasil.dimov@oracle.com-20100531152341-x2d4hma644icamh1) (merge vers: 5.5.5-m3) (pib:18)
[23 Jul 2010 12:34] Bugs System
Pushed into mysql-next-mr (revid:alik@sun.com-20100723121929-90e9zemk3jkr2ocy) (version source revid:vasil.dimov@oracle.com-20100531152341-x2d4hma644icamh1) (pib:18)
[27 Jul 2010 0:45] Paul DuBois
Noted in 5.5.6 changelog.

The SHA2() function now returns a character string with the
connection character set and collation. Previously, it returned a
binary string. This is the same change made for several other
encryption functions in MySQL 5.5.3.