DEV Community

Michael
Michael

Posted on

SHA-2 Hashing in GBase 8a: sha2() and Its Four Modes

Recent versions of the China-domestically developed GBase 8a MPP database from GBASE added the SHA2 family of hash functions. This post covers sha2 and its four modes — SHA-224, SHA-256, SHA-384 and SHA-512 — with the actual output from version 9.5.3.28.4_patch.1bcc63ff.

Tested version

gbase> select version();
+---------------------------+
| version()                 |
+---------------------------+
| 9.5.3.28.4_patch.1bcc63ff |
+---------------------------+
1 row in set (Elapsed: 00:00:00.00)
Enter fullscreen mode Exit fullscreen mode

Syntax

sha2(str String,mode int) return String
Enter fullscreen mode Exit fullscreen mode
  • str: the input string. It can contain Chinese characters, letters, digits and any other character a varchar supports.
  • mode: the hash mode.
  • mode=224 maps to SHA-224 and returns varchar(56).
  • mode=256 maps to SHA-256 and returns varchar(64).
  • mode=384 maps to SHA-384 and returns varchar(96).
  • mode=512 maps to SHA-512 and returns varchar(128).

Notes

The function hashes the input string into a fixed-length string. If mode is not one of those four numbers, it returns null. Use UTF8 character encoding.

Examples

SHA-224

gbase> select sha2('GBase 8a',224);
+----------------------------------------------------------+
| sha2('GBase 8a',224)                                     |
+----------------------------------------------------------+
| 7e3272023ef3e8edcbeb1daf2244529d806229d481753d45bd5bb524 |
+----------------------------------------------------------+
1 row in set (Elapsed: 00:00:00.00)
Enter fullscreen mode Exit fullscreen mode

SHA-256

gbase> select sha2('GBase 8a',256);
+------------------------------------------------------------------+
| sha2('GBase 8a',256)                                             |
+------------------------------------------------------------------+
| 37e28220f6937e7dd7afcf6511e35a72d835dc50bd8c363fec8054485933313b |
+------------------------------------------------------------------+
1 row in set (Elapsed: 00:00:00.00)
Enter fullscreen mode Exit fullscreen mode

SHA-384

gbase> select sha2('GBase 8a',384);
+--------------------------------------------------------------------------------------------------+
| sha2('GBase 8a',384)                                                                             |
+--------------------------------------------------------------------------------------------------+
| a1ea79415dc17396399a97d1e07c0553c775519be2f031333c9ef05d724341e4f2ed09cda8b890c595c1add5e8df2348 |
+--------------------------------------------------------------------------------------------------+
1 row in set (Elapsed: 00:00:00.00)
Enter fullscreen mode Exit fullscreen mode

SHA-512

gbase> select sha2('GBase 8a',512);
+----------------------------------------------------------------------------------------------------------------------------------+
| sha2('GBase 8a',512)                                                                                                             |
+----------------------------------------------------------------------------------------------------------------------------------+
| 66668c66c50b095b772fc69cc903881a6107187cf33d4d9b43016c3b7260115e99175b677c2d603675ac7edcc0cf5ec7436c905d1b126237ab5e7949e48b62ed |
+----------------------------------------------------------------------------------------------------------------------------------+
1 row in set (Elapsed: 00:00:00.00)
Enter fullscreen mode Exit fullscreen mode

Any other mode

gbase> select sha2('GBase 8a',123);
+----------------------+
| sha2('GBase 8a',123) |
+----------------------+
| NULL                 |
+----------------------+
1 row in set, 1 warning (Elapsed: 00:00:00.00)

gbase> show warnings;
+---------+------+-------------------+
| Level   | Code | Message           |
+---------+------+-------------------+
| Warning | 1682 | numeric overflow. |
+---------+------+-------------------+
1 row in set (Elapsed: 00:00:00.00)
Enter fullscreen mode Exit fullscreen mode

mode=123 returns NULL, and show warnings reports code 1682 with numeric overflow.

Going forward, validate the mode before passing user input into sha2 on a gbase cluster: an out-of-range value does not raise an error, it quietly produces NULL plus a warning, so a hash column in a gbase database can end up empty without any failed query.

Top comments (0)