©SQL :: Manager
HomeProgram InformationTechnical detailsFree downloadDonate for a cup of coffee
myway SQL Manager for MySQL and MariaDB, Download on Uptodown
SQL functionMySQLMariaDB

The SHA2 SQL Function in MySQL and MariaDB - SHA-2 checksum

SHA2Syntax:SHA2(String, Length)
Return value:CHAR
Function type:Encryption & Compression
Function Description

The SHA2() function calculates a SHA-2 checksum for the "String".

The SHA-2 is considered cryptographically more secure than a SHA-1 160-bit checksum.

The SHA-2 family includes SHA-224, SHA-256, SHA-384 and SHA-512. The "Length" must be one of these values: 224, 256 or 0, 384 or 512, otherwise the function returns NULL.

The value is returned as a string of hexadecimal digits, the length of which changes depending on the precision selected, specified in "Length".

Numeric values ​​as an argument are interpreted as a character string.

If the value of the "String" argument is NULL, the SHA2() function returns NULL.

If the value of the "Length" argument is NULL, the SHA2() function in MySQL 8.0 returns NULL, MariaDB returns the value for SHA-256.



SHA2 only works if MariaDB is configured with TLS support or MySQL with SSL support.

SQL Examples for the SHA2 function

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16

select sha2('Test', 224), char_length(sha2('Test', 224)) as `chars`bit_length(sha2('Test', 224)) as `bits`;
select sha2('Test', 256), char_length(sha2('Test', 256)) as `chars`bit_length(sha2('Test', 256)) as `bits`;
select sha2('Test''256');
select sha2('Test', 0);
select sha2('Test', 384), char_length(sha2('Test', 384)) as `chars`bit_length(sha2('Test', 384)) as `bits`;
select char_length(sha2('Test', 512)), bit_length(sha2('Test', 512)) as `bits`;

select sha2(123456, 256);

select sha2('', 256);
select sha2('Test', 160);
select sha2('Test', 220);
select sha2(null, 256);
select sha2('Test'null);

sha2('Test', 224)
chars
bits
varchar(56) BINARYint(10)bigint(11)
3606346815fd4d491a92649905a40da025d8cf15f095136b19f3792356448
sha2('Test', 256)
chars
bits
varchar(64) BINARYint(10)bigint(11)
532eaabd9574880dbf76b9b8cc00832c20a6ec113d682299550d7a6e0f345e2564512
sha2('Test', '256')
varchar(64) BINARY
532eaabd9574880dbf76b9b8cc00832c20a6ec113d682299550d7a6e0f345e25
sha2('Test', 0)
varchar(64) BINARY
532eaabd9574880dbf76b9b8cc00832c20a6ec113d682299550d7a6e0f345e25
sha2('Test', 384)
chars
bits
varchar(96) BINARYint(10)bigint(11)
7b8f4654076b80eb963911f19cfad1aaf4285ed48e826f6cde1b01a79aa73fadb5446e667fc4f90417782c91270540f396768
char_length(sha2('Test', 512))
bits
int(10)bigint(11)
1281024
sha2(123456, 256)
varchar(64) BINARY
8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92
sha2('', 256)
varchar(64) BINARY
e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855
sha2('Test', 160)
varchar(0) BINARY
NULL
sha2('Test', 220)
varchar(0) BINARY
NULL
sha2(null, 256)
varchar(64) BINARY
NULL
sha2('Test', null)
varchar(64) BINARY
532eaabd9574880dbf76b9b8cc00832c20a6ec113d682299550d7a6e0f345e25

Example MySQL 8.0 with "Length" NULL

 1
 2
 3

select sha2('Test'null);

sha2('Test', null)
varchar(64) BINARY
NULL
The examples were created with the MyWAY SQL manager: Download

How to use the SHA2() function in MySQL and MariaDB databases

Both MySQL and MariaDB provide the SHA2() function, which is used to calculate hash values using the SHA-2 family of algorithms. The desired bit length for the hash can be selected, which offers more flexibility and stronger hashing options compared to the outdated SHA-1. The security level and the length of the resulting hash can be selected by specifying the bit length 256 or 512. Common use cases for the feature include secure password storage, data integrity verification, digital signatures and cryptographic operations.
SETVALSHASHA1SHA2SIGNSINSLEEP

Further MySQL und MariaDB SQL Encryption & Compression

AES_DECRYPTAES_DECRYPT(String, Key)
More about AES_DECRYPT Function

AES_ENCRYPTAES_ENCRYPT(String, Key)
More about AES_ENCRYPT Function

COMPRESSCOMPRESS(String)
More about COMPRESS Function

DECODEDECODE(Binary string, Password)
More about DECODE Function

DES_DECRYPTDES_DECRYPT(Binary string [, Key])
More about DES_DECRYPT Function

DES_ENCRYPTDES_ENCRYPT(String [, Key])
More about DES_ENCRYPT Function

ENCODEENCODE(String, Password)
More about ENCODE Function

ENCRYPTENCRYPT(String [, Salt])
More about ENCRYPT Function

FROM_BASE64FROM_BASE64(String)
More about FROM_BASE64 Function

MD5MD5(String)
More about MD5 Function

OLD_PASSWORDOLD_PASSWORD(String)
More about OLD_PASSWORD Function

PASSWORDPASSWORD(String)
More about PASSWORD Function

RANDOM_BYTESRANDOM_BYTES(Length)
More about RANDOM_BYTES Function

SHASHA(String)
More about SHA Function

SHA1SHA1(String)
More about SHA1 Function

TO_BASE64TO_BASE64(String)
More about TO_BASE64 Function

UNCOMPRESSUNCOMPRESS(Binary string)
More about UNCOMPRESS Function

Numeric functionsString functionsRegular ExpressionsDate and Time functions
Comparison functionsEncryption & CompressionConversion functionsNULL functions
Aggregate functionsWindow functionsJSON functionsGeometric functions
Sequence functionsInformation functionsDynamic ColumnsMiscellaneous functions
More information about the SHA2 SQL function: mysql.com and mariadb.com
Updated: 2023-09-24ImprintContactTerms & conditionsPrivacy & CookiesUpdatesSitemapFacebookLinkedinTwitterStatistics©2020-2024 Heino Cunze-Fischer