DECLARE

v_chiave VARCHAR2(32767) := 'mFo1GE$+GBL;EsdghET$%JK2356';

v_chiave_binario RAW(32767);

v_applmsg VARCHAR2(32767) := '2024-05-10T16:05:57';

v_applmsg_binario RAW(32767);

v_hmac_binario RAW(32767);

v_b64_hmac VARCHAR2(32767);

BEGIN
dbms_output.put_line(message)
/* trasformazione in binario della stringa della chiave di cifratura */

v_chiave_binario := utl_raw.cast_to_raw(v_chiave);

dbms_output.put_line('v_chiave_binario=' || v_chiave_binario);

-- valore in esadecimale: 313233343536373839306162636465666768696C6D6E6F707172737475767A24

/* trasformazione in binario della stringa del messaggio in chiaro */

v_applmsg_binario := utl_raw.cast_to_raw(v_applmsg);

dbms_output.put_line('v_applmsg_binario=' || v_applmsg_binario);

-- valore in esadecimale: 323031382D30352D30355430393A33393A3130

/* calcolo hmac con SHA1 */

v_hmac_binario := dbms_crypto.mac(src => v_applmsg_binario,

typ => dbms_crypto.hmac_sh1,

key => v_chiave_binario);

dbms_output.put_line('v_hmac_binario=' || v_hmac_binario);

-- valore in esadecimale: F057BE3161ED226210E9D558951AB7292561B746

v_b64_hmac := utl_raw.cast_to_varchar2(utl_encode.base64_encode(v_hmac_binario));

dbms_output.put_line('v_b64_hmac=' || v_b64_hmac);

-- hmac in base64: 8Fe+MWHtImIQ6dVYlRq3KSVht0Y=
END;
/ 

PL/SQL Online Compiler

Write, Run & Share PL/SQL code online using OneCompiler's Oracle PL/SQL online editor and compiler for free. It's one of the robust, feature-rich online editor and compiler for Oracle PL/SQL running on latest version 23c (23.3.0.0). Getting started with the OneCompiler's Oracle PL/SQL editor is really simple and pretty fast. The editor shows sample boilerplate code when you choose language as 'PL/SQL' and start writing code to learn and test online without worrying about tedious process of installation.

About PL/SQL

PL/SQL is procedural extension for SQL created by Oracle. It is by default embedded into the Oracle Database. PL/SQL program units are compiled and stored inside the Oracle Database which results in optimal execution times as the PL/SQL and SQL run within the same server process.

Syntax help

Following is the syntax structure for the PL/SQL code blocks

DECLARE 
   <declarations section> 
BEGIN 
   <executable command(s)>
EXCEPTION 
   <exception handling> 
END;

Example

DECLARE 
   message  varchar2(100):= 'Hello, World!'; 
BEGIN 
   dbms_output.put_line(message); 
END; 
/

Named procedures

CREATE OR REPLACE FUNCTION 
hello_user
   (user_name IN VARCHAR2) 
    RETURN VARCHAR2
IS
BEGIN
   RETURN 'Hello ' || user_name;
END hello_user;
/

BEGIN
   dbms_output.put_line(hello_user('Peter'));
END;
/

Exception handling

BEGIN
  DBMS_OUTPUT.put_line (1/0);
EXCEPTION
  WHEN OTHERS
  THEN
    DBMS_OUTPUT.put_line ('error is: ' || SQLERRM);
END;