salix/db/routines/util/functions/stringXor.sql

25 lines
613 B
SQL

DELIMITER $$
CREATE OR REPLACE DEFINER=`root`@`localhost` FUNCTION `util`.`stringXor`(vString MEDIUMBLOB, vConst TINYINT UNSIGNED)
RETURNS mediumblob
DETERMINISTIC
NO SQL
BEGIN
/**
* Returns XOR of binary string and an 8-bit constant.
*/
DECLARE vLen, vPos INT UNSIGNED;
DECLARE vResult MEDIUMBLOB;
SET vLen = LENGTH(vString);
SET vPos = 1;
SET vResult = '';
WHILE vPos <= vLen DO
SET vResult = CONCAT(vResult, LPAD(HEX( ORD(SUBSTR(vString, vPos, 1)) ^ vConst), 2, '0'));
SET vPos = vPos + 1;
END WHILE;
RETURN UNHEX(vResult);
END$$
DELIMITER ;