33 lines
947 B
MySQL
33 lines
947 B
MySQL
|
DELIMITER $$
|
||
|
CREATE OR REPLACE DEFINER=`root`@`localhost` FUNCTION `util`.`hmacSha2`(`vAlg` SMALLINT, `vMsg` MEDIUMBLOB, `vKey` MEDIUMBLOB)
|
||
|
RETURNS varchar(128) CHARSET utf8mb3 COLLATE utf8mb3_general_ci
|
||
|
DETERMINISTIC
|
||
|
NO SQL
|
||
|
BEGIN
|
||
|
/**
|
||
|
* HMAC function based on SHA2 algorythms.
|
||
|
*/
|
||
|
DECLARE vHashlen INT UNSIGNED;
|
||
|
DECLARE vOpad, vIpad TINYBLOB;
|
||
|
|
||
|
CASE vAlg
|
||
|
WHEN 224 THEN SET vHashlen = 64;
|
||
|
WHEN 256 THEN SET vHashlen = 64;
|
||
|
WHEN 384 THEN SET vHashlen = 128;
|
||
|
WHEN 512 THEN SET vHashlen = 128;
|
||
|
ELSE CALL throw ('WRONG_ALGORYTHM_IDENTIFICATOR_USED');
|
||
|
END CASE;
|
||
|
|
||
|
IF LENGTH(vKey) > vHashlen THEN
|
||
|
SET vKey = UNHEX(SHA2(vKey, vAlg));
|
||
|
END IF;
|
||
|
|
||
|
SET vKey = RPAD(vKey, vHashlen, 0x00);
|
||
|
|
||
|
SET vIpad = stringXor(vKey, 0x36);
|
||
|
SET vOpad = stringXor(vKey, 0x5C);
|
||
|
|
||
|
RETURN SHA2(CONCAT(vOpad, UNHEX(SHA2(CONCAT(vIpad, vMsg), vAlg))), vAlg);
|
||
|
END$$
|
||
|
DELIMITER ;
|