Created
October 12, 2017 11:46
-
-
Save juanparati/f2f1afbd8f5b7fe16248e48020babd34 to your computer and use it in GitHub Desktop.
Laravel decrypt implementation as MySQL function
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| CREATE DEFINER=`%`@`%` FUNCTION `LARAVEL_DECRYPT`( | |
| `encstr` TEXT, | |
| `raw_key` VARCHAR(255) | |
| ) | |
| RETURNS varchar(255) CHARSET utf8 COLLATE utf8_unicode_ci | |
| LANGUAGE SQL | |
| DETERMINISTIC | |
| READS SQL DATA | |
| SQL SECURITY DEFINER | |
| COMMENT '' | |
| BEGIN | |
| DECLARE decrypted VARCHAR(255); | |
| SET decrypted = AES_DECRYPT(FROM_BASE64(JSON_UNQUOTE(JSON_EXTRACT(CONVERT(FROM_BASE64(encstr) USING utf8), '$.value'))), FROM_BASE64(raw_key), FROM_BASE64(JSON_UNQUOTE(JSON_EXTRACT(CONVERT(FROM_BASE64(encstr) USING utf8), '$.iv')))); | |
| SET decrypted = SUBSTRING_INDEX(decrypted, ':', -1); | |
| SET decrypted = SUBSTRING(decrypted, 2, CHAR_LENGTH(decrypted) - 3); | |
| return (decrypted); | |
| END |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is more portable version that works in MySQL and MariaDB >= 11.2