salix/db/routines/vn/procedures/crypt.sql

58 lines
1.9 KiB
SQL

DELIMITER $$
CREATE OR REPLACE DEFINER=`vn`@`localhost` PROCEDURE `vn`.`crypt`(vText VARCHAR(255), vKey VARCHAR(255), OUT vResult VARCHAR(255) )
BEGIN
DECLARE vEncryptedText VARCHAR(255) DEFAULT '';
DECLARE vKeyLength INT;
DECLARE vKeyPosition INT DEFAULT 1;
DECLARE vKeyChar VARCHAR(1);
DECLARE vTextLength INT;
DECLARE vTextPosition INT DEFAULT 1;
DECLARE vTextChar VARCHAR(1);
DECLARE vEncryptedChar VARCHAR(1);
DECLARE vEncryptedCharPosition INT;
DECLARE vChain VARCHAR(255) DEFAULT '#$.-_0123456789abcdefghijklmnopqrstuvwxyz';
DECLARE vChainTextPosition INT;
DECLARE vChainKeyPosition INT;
DECLARE vChainLength INT;
DECLARE vInvalidadChars VARCHAR(255) DEFAULT '';
SET vTextLength = LENGTH(vText);
SET vKeyLength = LENGTH(vKey);
SET vChainLength = LENGTH(vChain);
WHILE vTextPosition <= vTextLength DO
SET vTextChar = MID(vText,vTextPosition,1);
SET vKeyChar = MID(vKey,vKeyPosition,1);
SET vChainTextPosition = LOCATE(vTextChar, vChain);
SET vInvalidadChars = IF(vChainTextPosition, vInvalidadChars, CONCAT(vInvalidadChars,vTextChar));
SET vChainKeyPosition = LOCATE(vKeyChar, vChain);
SET vEncryptedCharPosition = vChainTextPosition + vChainKeyPosition;
IF vEncryptedCharPosition > vChainLength THEN
SET vEncryptedCharPosition = vEncryptedCharPosition - vChainLength;
END IF;
SET vEncryptedChar = MID(vChain, vEncryptedCharPosition,1);
SET vEncryptedText = CONCAT(vEncryptedText, vEncryptedChar);
SET vKeyPosition = IF(vKeyPosition = vKeyLength, 1,vKeyPosition + 1);
SET vTextPosition = vTextPosition + 1;
END WHILE;
SET vResult = IF(LENGTH(vInvalidadChars), CONCAT('Caracteres no válidos: ',vInvalidadChars),vEncryptedText);
END$$
DELIMITER ;