SET PATH *LIBL ; 

CREATE OR REPLACE FUNCTION CISTOOLS.GET_MXAPLUS_PASSWORD ( ) 
	RETURNS CHAR(10)   
	LANGUAGE SQL 
	SPECIFIC CISTOOLS.GET_MXAPLUS_PASSWORD 
	NOT DETERMINISTIC 
	MODIFIES SQL DATA 
	CALLED ON NULL INPUT 
	SET OPTION  ALWBLK = *ALLREAD , 
	ALWCPYDTA = *OPTIMIZE , 
	COMMIT = *NONE , 
	DECRESULT = (31, 31, 00) , 
	DYNDFTCOL = *NO , 
	DYNUSRPRF = *USER , 
	SRTSEQ = *HEX   
	BEGIN 
DECLARE ENCRYPTED_PASSWORD CHAR ( 20 ) DEFAULT '' ; 
DECLARE DECRYPTED_PASSWORD CHAR ( 20 ) DEFAULT '' ; 
DECLARE INDEX_ARRAY INTEGER ; 
DECLARE INDEX_POSITION INTEGER ; 
DECLARE VALIDATION_TOKEN CHAR ( 16 ) DEFAULT 'RK2GSBV3FAV9N55S' ;  --Infor's val--idation token, out in plain sight.., from their source 
-- Register SQL version of traditional RPG program in QTEMP 
CREATE OR REPLACE PROCEDURE QTEMP . AXZCSU0R ( IN ASET CHAR ( 10 ) , IN VLDD CHAR ( 16 ) , IN SCRD CHAR ( 1 ) , INOUT PRPW CHAR ( 20 ) , 
INOUT SETD CHAR ( 40 ) DEFAULT '' , INOUT RTRN CHAR ( 7 ) DEFAULT '' ) 
LANGUAGE RPGLE 
EXTERNAL NAME AMCESLIB / AXZCSU0R 
PARAMETER STYLE GENERAL ; 
CALL QTEMP . AXZCSU0R ( 'MXAPLUS' , VALIDATION_TOKEN , '1' , ENCRYPTED_PASSWORD) ; 
-- Decrypt the password 
SET INDEX_ARRAY = 1 ; 
WHILE INDEX_ARRAY <= 20 DO 
SET INDEX_POSITION = 
CASE INDEX_ARRAY 
WHEN 1 THEN 4 
WHEN 2 THEN 17 
WHEN 3 THEN 2 
WHEN 4 THEN 9 
WHEN 5 THEN 1 
WHEN 6 THEN 20 
WHEN 7 THEN 18 
WHEN 8 THEN 13 
WHEN 9 THEN 11 
WHEN 10 THEN 14 
WHEN 11 THEN 3 
WHEN 12 THEN 10 
WHEN 13 THEN 5 
WHEN 14 THEN 19 
WHEN 15 THEN 8 
WHEN 16 THEN 6 
WHEN 17 THEN 12 
WHEN 18 THEN 16 
WHEN 19 THEN 7 
WHEN 20 THEN 15 
END ; 
SET DECRYPTED_PASSWORD = LEFT ( DECRYPTED_PASSWORD , INDEX_POSITION - 1 ) || SUBSTRING ( ENCRYPTED_PASSWORD , INDEX_ARRAY , 1 ) 
|| SUBSTRING ( DECRYPTED_PASSWORD , INDEX_POSITION + 1 ) ; 
SET INDEX_ARRAY = INDEX_ARRAY + 1 ; 
END WHILE ; 
-- Replace * with space (same as %xlate('*':' ')) 
SET DECRYPTED_PASSWORD = REPLACE ( DECRYPTED_PASSWORD , '*' , ' ' ) ; 
-- Drop the temp procedure 
DROP PROCEDURE QTEMP . AXZCSU0R ; 
SET CISTOOLS . SQL_CLOB = DECRYPTED_PASSWORD ; 
-- Password starts from position 11 
RETURN SUBSTRING ( DECRYPTED_PASSWORD , 11 , 10 ) ; 
END  ; 

GRANT ALTER , EXECUTE   
ON SPECIFIC FUNCTION CISTOOLS.GET_MXAPLUS_PASSWORD 
TO AMAPICS WITH GRANT OPTION ; 

GRANT EXECUTE   
ON SPECIFIC FUNCTION CISTOOLS.GET_MXAPLUS_PASSWORD 
TO PUBLIC ; 

