Oracle PL/SQL/System Packages/dbms crypto — различия между версиями

Материал из SQL эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Версия 13:45, 26 мая 2010

"3DES"

  
SQL>
SQL> CREATE OR REPLACE FUNCTION get_enc_val (
  2     p_in_val      IN   VARCHAR2,
  3     p_key         IN   VARCHAR2,
  4     p_iv          IN   VARCHAR2 := NULL
  5  )
  6     RETURN VARCHAR2
  7  IS
  8     l_enc_val    RAW (4000);
  9     l_enc_algo   PLS_INTEGER;
 10     l_in         RAW (4000);
 11     l_iv         RAW (4000);
 12     l_key        RAW (4000);
 13     l_ret        VARCHAR2 (4000);
 14  BEGIN
 15     l_enc_algo := dbms_crypto.encrypt_3des;
 16     l_in := utl_i18n.string_to_raw (p_in_val, "AL32UTF8");
 17     l_iv := utl_i18n.string_to_raw (p_iv, "AL32UTF8");
 18     l_key := utl_i18n.string_to_raw (p_key, "AL32UTF8");
 19     l_enc_val :=
 20        dbms_crypto.encrypt (src      => l_in,
 21                             KEY      => l_key,
 22                             iv       => l_iv,
 23                             typ      =>   l_enc_algo
 24                                         + dbms_crypto.chain_cbc
 25                                         + dbms_crypto.pad_pkcs5
 26                            );
 27     l_ret := RAWTOHEX (l_enc_val);
 28     RETURN l_ret;
 29  END;
 30  /
Function created.
SQL>



"3DES_2KEY"

  
SQL>
SQL> CREATE OR REPLACE FUNCTION get_enc_val (
  2     p_in_val      IN   VARCHAR2,
  3     p_key         IN   VARCHAR2,
  4     p_iv          IN   VARCHAR2 := NULL
  5  )
  6     RETURN VARCHAR2
  7  IS
  8     l_enc_val    RAW (4000);
  9     l_enc_algo   PLS_INTEGER;
 10     l_in         RAW (4000);
 11     l_iv         RAW (4000);
 12     l_key        RAW (4000);
 13     l_ret        VARCHAR2 (4000);
 14  BEGIN
 15     l_enc_algo := dbms_crypto.encrypt_3des_2key;
 16     l_in := utl_i18n.string_to_raw (p_in_val, "AL32UTF8");
 17     l_iv := utl_i18n.string_to_raw (p_iv, "AL32UTF8");
 18     l_key := utl_i18n.string_to_raw (p_key, "AL32UTF8");
 19     l_enc_val :=
 20        dbms_crypto.encrypt (src      => l_in,
 21                             KEY      => l_key,
 22                             iv       => l_iv,
 23                             typ      =>   l_enc_algo
 24                                         + dbms_crypto.chain_cbc
 25                                         + dbms_crypto.pad_pkcs5
 26                            );
 27     l_ret := RAWTOHEX (l_enc_val);
 28     RETURN l_ret;
 29  END;
 30  /
Function created.
SQL>



"AES128"

  
SQL>
SQL>
SQL> CREATE OR REPLACE FUNCTION get_enc_val (
  2     p_in_val      IN   VARCHAR2,
  3     p_key         IN   VARCHAR2,
  4     p_iv          IN   VARCHAR2 := NULL
  5  )
  6     RETURN VARCHAR2
  7  IS
  8     l_enc_val    RAW (4000);
  9     l_enc_algo   PLS_INTEGER;
 10     l_in         RAW (4000);
 11     l_iv         RAW (4000);
 12     l_key        RAW (4000);
 13     l_ret        VARCHAR2 (4000);
 14  BEGIN
 15     l_enc_algo := dbms_crypto.encrypt_aes128;
 16     l_in := utl_i18n.string_to_raw (p_in_val, "AL32UTF8");
 17     l_iv := utl_i18n.string_to_raw (p_iv, "AL32UTF8");
 18     l_key := utl_i18n.string_to_raw (p_key, "AL32UTF8");
 19     l_enc_val :=
 20        dbms_crypto.encrypt (src      => l_in,
 21                             KEY      => l_key,
 22                             iv       => l_iv,
 23                             typ      =>   l_enc_algo
 24                                         + dbms_crypto.chain_cbc
 25                                         + dbms_crypto.pad_pkcs5
 26                            );
 27     l_ret := RAWTOHEX (l_enc_val);
 28     RETURN l_ret;
 29  END;
 30  /
Function created.
SQL>
SQL>



"AES192"

  
SQL>
SQL>
SQL> CREATE OR REPLACE FUNCTION get_enc_val (
  2     p_in_val      IN   VARCHAR2,
  3     p_key         IN   VARCHAR2,
  4     p_iv          IN   VARCHAR2 := NULL
  5  )
  6     RETURN VARCHAR2
  7  IS
  8     l_enc_val    RAW (4000);
  9     l_enc_algo   PLS_INTEGER;
 10     l_in         RAW (4000);
 11     l_iv         RAW (4000);
 12     l_key        RAW (4000);
 13     l_ret        VARCHAR2 (4000);
 14  BEGIN
 15     l_enc_algo := dbms_crypto.encrypt_aes192;
 16     l_in := utl_i18n.string_to_raw (p_in_val, "AL32UTF8");
 17     l_iv := utl_i18n.string_to_raw (p_iv, "AL32UTF8");
 18     l_key := utl_i18n.string_to_raw (p_key, "AL32UTF8");
 19     l_enc_val :=
 20        dbms_crypto.encrypt (src      => l_in,
 21                             KEY      => l_key,
 22                             iv       => l_iv,
 23                             typ      =>   l_enc_algo
 24                                         + dbms_crypto.chain_cbc
 25                                         + dbms_crypto.pad_pkcs5
 26                            );
 27     l_ret := RAWTOHEX (l_enc_val);
 28     RETURN l_ret;
 29  END;
 30  /
Function created.



"AES256"

  
SQL>
SQL>
SQL>
SQL> CREATE OR REPLACE FUNCTION get_enc_val (
  2     p_in_val      IN   VARCHAR2,
  3     p_key         IN   VARCHAR2,
  4     p_iv          IN   VARCHAR2 := NULL
  5  )
  6     RETURN VARCHAR2
  7  IS
  8     l_enc_val    RAW (4000);
  9     l_enc_algo   PLS_INTEGER;
 10     l_in         RAW (4000);
 11     l_iv         RAW (4000);
 12     l_key        RAW (4000);
 13     l_ret        VARCHAR2 (4000);
 14  BEGIN
 15     l_enc_algo := dbms_crypto.encrypt_aes256;
 16     l_in := utl_i18n.string_to_raw (p_in_val, "AL32UTF8");
 17     l_iv := utl_i18n.string_to_raw (p_iv, "AL32UTF8");
 18     l_key := utl_i18n.string_to_raw (p_key, "AL32UTF8");
 19     l_enc_val :=
 20        dbms_crypto.encrypt (src      => l_in,
 21                             KEY      => l_key,
 22                             iv       => l_iv,
 23                             typ      =>   l_enc_algo
 24                                         + dbms_crypto.chain_cbc
 25                                         + dbms_crypto.pad_pkcs5
 26                            );
 27     l_ret := RAWTOHEX (l_enc_val);
 28     RETURN l_ret;
 29  END;
 30  /
Function created.



dbms_crypto.hash

 
SQL>
SQL> CREATE TABLE EMP(
  2      EMPNO NUMBER(4) NOT NULL,
  3      ENAME VARCHAR2(10),
  4      JOB VARCHAR2(9),
  5      MGR NUMBER(4),
  6      startDate DATE,
  7      SAL NUMBER(7, 2),
  8      COMM NUMBER(7, 2),
  9      DEPTNO NUMBER(2)
 10  );
Table created.
SQL>
SQL> INSERT INTO EMP VALUES(7369, "SMITH", "CLERK", 7902,TO_DATE("17-DEC-1980", "DD-MON-YYYY"), 800, NULL, 20);
1 row created.
SQL> INSERT INTO EMP VALUES(7499, "ALLEN", "SALESMAN", 7698,TO_DATE("20-FEB-1981", "DD-MON-YYYY"), 1600, 300, 30);
1 row created.
SQL> INSERT INTO EMP VALUES(7521, "WARD", "SALESMAN", 7698,TO_DATE("22-FEB-1981", "DD-MON-YYYY"), 1250, 500, 30);
1 row created.
SQL> INSERT INTO EMP VALUES(7566, "JONES", "MANAGER", 7839,TO_DATE("2-APR-1981", "DD-MON-YYYY"), 2975, NULL, 20);
1 row created.
SQL> INSERT INTO EMP VALUES(7654, "MARTIN", "SALESMAN", 7698,TO_DATE("28-SEP-1981", "DD-MON-YYYY"), 1250, 1400, 30);
1 row created.
SQL> INSERT INTO EMP VALUES(7698, "BLAKE", "MANAGER", 7839,TO_DATE("1-MAY-1981", "DD-MON-YYYY"), 2850, NULL, 30);
1 row created.
SQL> INSERT INTO EMP VALUES(7782, "CLARK", "MANAGER", 7839,TO_DATE("9-JUN-1981", "DD-MON-YYYY"), 2450, NULL, 10);
1 row created.
SQL> INSERT INTO EMP VALUES(7788, "SCOTT", "ANALYST", 7566,TO_DATE("09-DEC-1982", "DD-MON-YYYY"), 3000, NULL, 20);
1 row created.
SQL> INSERT INTO EMP VALUES(7839, "KING", "PRESIDENT", NULL,TO_DATE("17-NOV-1981", "DD-MON-YYYY"), 5000, NULL, 10);
1 row created.
SQL> INSERT INTO EMP VALUES(7844, "TURNER", "SALESMAN", 7698,TO_DATE("8-SEP-1981", "DD-MON-YYYY"), 1500, 0, 30);
1 row created.
SQL> INSERT INTO EMP VALUES(7876, "ADAMS", "CLERK", 7788,TO_DATE("12-JAN-1983", "DD-MON-YYYY"), 1100, NULL, 20);
1 row created.
SQL> INSERT INTO EMP VALUES(7900, "E", "CLERK", 7698,TO_DATE("3-DEC-1981", "DD-MON-YYYY"), 950, NULL, 30);
1 row created.
SQL> INSERT INTO EMP VALUES(7902, "FORD", "ANALYST", 7566,TO_DATE("3-DEC-1981", "DD-MON-YYYY"), 3000, NULL, 20);
1 row created.
SQL> INSERT INTO EMP VALUES(7934, "MILLER", "CLERK", 7782,TO_DATE("23-JAN-1982", "DD-MON-YYYY"), 1300, NULL, 10);
1 row created.
SQL>
SQL> CREATE TABLE DEPT(
  2      DEPTNO NUMBER(2),
  3      DNAME VARCHAR2(14),
  4      LOC VARCHAR2(13)
  5  );
Table created.
SQL>
SQL> INSERT INTO DEPT VALUES (10, "ACCOUNTING", "NEW YORK");
1 row created.
SQL> INSERT INTO DEPT VALUES (20, "RESEARCH", "DALLAS");
1 row created.
SQL> INSERT INTO DEPT VALUES (30, "SALES", "CHICAGO");
1 row created.
SQL> INSERT INTO DEPT VALUES (40, "OPERATIONS", "BOSTON");
1 row created.
SQL>
SQL>
SQL>
SQL> variable hash varchar2(100);
SQL>
SQL> begin
  2      for x in ( select deptno, dname, loc
  3                   from dept
  4                  where deptno = 10 )
  5      loop
  6          dbms_output.put_line( "Dname:  " || x.dname );
  7          dbms_output.put_line( "Loc:    " || x.loc );
  8          dbms_output.put_line( "Hash:   " || dbms_crypto.hash( utl_raw.cast_to_raw(x.deptno||"/"||x.dname||"/"||x.loc),dbms_crypto.hash_sh1 ) );
  9            :hash := dbms_crypto.hash( utl_raw.cast_to_raw(x.deptno||"/"||x.dname||"/"||x.loc),dbms_crypto.hash_sh1 );
 10      end loop;
 11  end;
 12  /

SQL>
SQL> drop table emp;
Table dropped.
SQL> drop table dept;
Table dropped.



dbms_crypto.hash_sh1

 
SQL>
SQL>
SQL> CREATE TABLE EMP(
  2      EMPNO NUMBER(4) NOT NULL,
  3      ENAME VARCHAR2(10),
  4      JOB VARCHAR2(9),
  5      MGR NUMBER(4),
  6      HIREDATE DATE,
  7      SAL NUMBER(7, 2),
  8      COMM NUMBER(7, 2),
  9      DEPTNO NUMBER(2)
 10  );
Table created.
SQL>
SQL> INSERT INTO EMP VALUES(7369, "SMITH", "CLERK", 7902,TO_DATE("17-DEC-1980", "DD-MON-YYYY"), 800, NULL, 20);
1 row created.
SQL> INSERT INTO EMP VALUES(7499, "ALLEN", "SALESMAN", 7698,TO_DATE("20-FEB-1981", "DD-MON-YYYY"), 1600, 300, 30);
1 row created.
SQL> INSERT INTO EMP VALUES(7521, "WARD", "SALESMAN", 7698,TO_DATE("22-FEB-1981", "DD-MON-YYYY"), 1250, 500, 30);
1 row created.
SQL> INSERT INTO EMP VALUES(7566, "JONES", "MANAGER", 7839,TO_DATE("2-APR-1981", "DD-MON-YYYY"), 2975, NULL, 20);
1 row created.
SQL> INSERT INTO EMP VALUES(7654, "MARTIN", "SALESMAN", 7698,TO_DATE("28-SEP-1981", "DD-MON-YYYY"), 1250, 1400, 30);
1 row created.
SQL> INSERT INTO EMP VALUES(7698, "BLAKE", "MANAGER", 7839,TO_DATE("1-MAY-1981", "DD-MON-YYYY"), 2850, NULL, 30);
1 row created.
SQL> INSERT INTO EMP VALUES(7782, "CLARK", "MANAGER", 7839,TO_DATE("9-JUN-1981", "DD-MON-YYYY"), 2450, NULL, 10);
1 row created.
SQL> INSERT INTO EMP VALUES(7788, "SCOTT", "ANALYST", 7566,TO_DATE("09-DEC-1982", "DD-MON-YYYY"), 3000, NULL, 20);
1 row created.
SQL> INSERT INTO EMP VALUES(7839, "KING", "PRESIDENT", NULL,TO_DATE("17-NOV-1981", "DD-MON-YYYY"), 5000, NULL, 10);
1 row created.
SQL> INSERT INTO EMP VALUES(7844, "TURNER", "SALESMAN", 7698,TO_DATE("8-SEP-1981", "DD-MON-YYYY"), 1500, 0, 30);
1 row created.
SQL> INSERT INTO EMP VALUES(7876, "ADAMS", "CLERK", 7788,TO_DATE("12-JAN-1983", "DD-MON-YYYY"), 1100, NULL, 20);
1 row created.
SQL> INSERT INTO EMP VALUES(7900, "E", "CLERK", 7698,TO_DATE("3-DEC-1981", "DD-MON-YYYY"), 950, NULL, 30);
1 row created.
SQL> INSERT INTO EMP VALUES(7902, "FORD", "ANALYST", 7566,TO_DATE("3-DEC-1981", "DD-MON-YYYY"), 3000, NULL, 20);
1 row created.
SQL> INSERT INTO EMP VALUES(7934, "MILLER", "CLERK", 7782,TO_DATE("23-JAN-1982", "DD-MON-YYYY"), 1300, NULL, 10);
1 row created.
SQL>
SQL> CREATE TABLE DEPT(
  2      DEPTNO NUMBER(2),
  3      DNAME VARCHAR2(14),
  4      LOC VARCHAR2(13)
  5  );
Table created.
SQL>
SQL> INSERT INTO DEPT VALUES (10, "ACCOUNTING", "NEW YORK");
1 row created.
SQL> INSERT INTO DEPT VALUES (20, "RESEARCH", "DALLAS");
1 row created.
SQL> INSERT INTO DEPT VALUES (30, "SALES", "CHICAGO");
1 row created.
SQL> INSERT INTO DEPT VALUES (40, "OPERATIONS", "BOSTON");
1 row created.
SQL>
SQL>
SQL> begin
  2      for x in ( select deptno, dname, loc from dept where deptno = 10 )
  3      loop
  4          dbms_output.put_line( "Dname:  " || x.dname );
  5          dbms_output.put_line( "Loc:    " || x.loc );
  6          dbms_output.put_line( "Hash:   " ||dbms_crypto.hash( utl_raw.cast_to_raw(x.deptno||"/"||x.dname||"/"||x.loc),dbms_crypto.hash_sh1 ) );
  7      end loop;
  8  end;
  9  /

SQL>
SQL>
SQL>
SQL> drop table dept;
Table dropped.
SQL> drop table emp;
Table dropped.



"RC4"

  
SQL>
SQL>
SQL> CREATE OR REPLACE FUNCTION get_enc_val (
  2     p_in_val      IN   VARCHAR2,
  3     p_key         IN   VARCHAR2,
  4     p_iv          IN   VARCHAR2 := NULL
  5  )
  6     RETURN VARCHAR2
  7  IS
  8     l_enc_val    RAW (4000);
  9     l_enc_algo   PLS_INTEGER;
 10     l_in         RAW (4000);
 11     l_iv         RAW (4000);
 12     l_key        RAW (4000);
 13     l_ret        VARCHAR2 (4000);
 14  BEGIN
 15     l_enc_algo := dbms_crypto.encrypt_rc4;
 16     l_in := utl_i18n.string_to_raw (p_in_val, "AL32UTF8");
 17     l_iv := utl_i18n.string_to_raw (p_iv, "AL32UTF8");
 18     l_key := utl_i18n.string_to_raw (p_key, "AL32UTF8");
 19     l_enc_val :=
 20        dbms_crypto.encrypt (src      => l_in,
 21                             KEY      => l_key,
 22                             iv       => l_iv,
 23                             typ      =>   l_enc_algo
 24                                         + dbms_crypto.chain_cbc
 25                                         + dbms_crypto.pad_pkcs5
 26                            );
 27     l_ret := RAWTOHEX (l_enc_val);
 28     RETURN l_ret;
 29  END;
 30  /
Function created.
SQL>
SQL>