oracle如何脱敏

栏目:eas cloud知识作者:金蝶来源:金蝶云社区发布:2024-09-22浏览:1

oracle如何脱敏

创建脱敏表:
create table tuomintable(TABLE_NAME varchar(50), column_name  varchar(50), data_type  varchar(50),  tuomintype int,  needtuomin int);
--truncate table tuomintable;    
用业务账套把表的字段信息插入到脱敏表tuomintable:
DECLARE
 
  v_TABLE_NAME    varchar(50);
  v_column_name         varchar(50);
  v_data_type         varchar(50);
  v_tuomintype          number(30);
  v_needtuomin         number(30);
  v_text2 varchar(1000);
  CURSOR c1 IS
  select table_name,column_name,data_type,'1' as tuomintype ,'0' as needtuomin from user_tab_columns   ;
BEGIN
  OPEN c1;
   LOOP
    FETCH c1
      INTO v_TABLE_NAME, v_column_name,v_data_type, v_tuomintype, v_needtuomin ;
    EXIT WHEN c1%NOTFOUND;
 v_text2 := 'insert into tuomintable values  ('|| ''''||v_TABLE_NAME ||''''|| ',' ||    ''''||v_column_name||'''' || ',' || ''''||v_data_type||'''' || ',' ||    v_tuomintype ||','||''''|| v_needtuomin||''''||')';
execute immediate v_text2;
  DBMS_OUTPUT.PUT_LINE(v_text2);
   END LOOP;
  commit;
  close c1;
END;
/
把脱敏表tuomintable导入到excel,并对v_tuomintype,字段参照以下信息更改,
如果字段需要脱每, v_needtuomin改成1,否则删除该行,或把v_needtuomin改成0
  1 :名称
  11:地址
  2 :证件
  3 :银行账号
  4 :联系电话
  5 :接入号码
  12: number数值型,取100~1000之间的随机数  
 


CREATE OR REPLACE FUNCTION F_GET_SENSITIVE2020(IN_STR VARCHAR, IN_TYPE NUMBER)
  RETURN VARCHAR2 IS
  V_STR_LENGTH NUMBER;
  V_NAME       VARCHAR2(1000);
  V_N          NUMBER;
  V_HID        VARCHAR2(200);
  V_SQL        VARCHAR2(200);
  V_NUM_FLAG   NUMBER;
  /****
  N_TYPE 脱敏字段类型
  1 :名称
  11:地址
  2 :证件
  3 :银行账号
  4 :联系电话
  5 :接入号码
  12: number数值型,取100~1000之间的随机数  
  ***/
BEGIN
  V_STR_LENGTH := LENGTH(IN_STR);
  V_N          := 0;
 
 IF V_STR_LENGTH=0 THEN
      RETURN(NULL);
  END IF;

 /**********数值脱敏,随机数**********/  
   IF IN_TYPE = 12 THEN
    V_NAME := TRUNC(100+900*dbms_random.value);
    RETURN(V_NAME);
  END IF;
 
  /**********名称和地址脱敏规则**********/
  IF IN_TYPE = 1 OR IN_TYPE=11 THEN
    IF V_STR_LENGTH = 2 OR V_STR_LENGTH = 3 THEN
      V_NAME := REGEXP_REPLACE(IN_STR, '(.)', '*', 2, 1);
    ELSIF V_STR_LENGTH < 2 THEN
      V_NAME :=IN_STR;
    ELSE
      WHILE V_N < V_STR_LENGTH / 2 LOOP
        V_N   := V_N + 1;
        V_HID := V_HID || '*';
      END LOOP;
      V_NAME := SUBSTR(IN_STR, 0, V_STR_LENGTH / 2) || V_HID;
    END IF;
    RETURN(V_NAME);
  END IF;
  /**********证件脱敏规则**********/
  IF IN_TYPE = 2 THEN
    IF V_STR_LENGTH = 15 THEN
      V_NAME := SUBSTR(IN_STR, 0, 6) || '******' || SUBSTR(IN_STR, -3, 3);
    ELSIF V_STR_LENGTH = 18 THEN
      V_NAME := SUBSTR(IN_STR, 0, 6) || '********' || SUBSTR(IN_STR, -4, 4);
    ELSE
      WHILE V_N < V_STR_LENGTH / 3 LOOP
        V_N   := V_N + 1;
        V_HID := V_HID || '*';
      END LOOP;
      V_NAME := SUBSTR(IN_STR, 0, V_STR_LENGTH / 3) || V_HID ||
                SUBSTR(IN_STR, -V_STR_LENGTH / 3, V_STR_LENGTH / 3);
    END IF;
    RETURN(V_NAME);
  END IF;
 
  /**********银行账号脱敏规则**********/
  IF IN_TYPE = 3 THEN
    IF V_STR_LENGTH > 15 THEN
      V_NAME := SUBSTR(IN_STR, 0, 4) || '********' || SUBSTR(IN_STR, -4, 4);
    ELSE
      V_NAME :=IN_STR;
    END IF;
     RETURN(V_NAME);
  END IF;
  /**********联系电话脱敏规则**********/
  IF IN_TYPE = 4 THEN
    V_NAME := SUBSTR(IN_STR, 0, V_STR_LENGTH - 4) || '****';
    RETURN(V_NAME);
  END IF;
 
  /**********接入号码脱敏规则**********/
  IF IN_TYPE = 5 THEN
    V_SQL := 'SELECT COUNT(1) FROM DUAL  WHERE LENGTH(''' || IN_STR ||
             ''') = LENGTH(REGEXP_REPLACE(''' || IN_STR || ''', ''[^0-9]''))';
    EXECUTE IMMEDIATE V_SQL
      INTO V_NUM_FLAG;
    IF V_NUM_FLAG = 1 AND (V_STR_LENGTH = 7 OR V_STR_LENGTH = 8) THEN
      V_NAME := SUBSTR(IN_STR, 0, 2) || '****' || SUBSTR(IN_STR, -2, 2);
    ELSIF V_NUM_FLAG = 1 AND V_STR_LENGTH = 11 THEN
      V_NAME := SUBSTR(IN_STR, 0, 3) || '*****' || SUBSTR(IN_STR, -3, 3);
    ELSE
      V_NAME := IN_STR;
    END IF;
      RETURN(V_NAME);
  END IF;
  RETURN(IN_STR);
 
 
EXCEPTION
  WHEN OTHERS THEN
    -- DBMS_OUTPUT.PUT_LINE('1'||V_SQL);
    V_NAME := '-1';
    RETURN V_NAME;
END F_GET_SENSITIVE2020;
/


对脱敏表tuomintable中needtuomin=1的表指定字段进行脱敏
set serveroutput on

DECLARE
  v_TABLE_NAME    varchar(50);
  v_column_name         varchar(50);
  v_data_type         varchar(50);
  v_tuomintype          number(30);
  v_needtuomin         number(30);
  v_text2 varchar(1000);
  v_textprint varchar(1000);
  CURSOR c1 IS
  select table_name, column_name, data_type, tuomintype  from tuomintable  where needtuomin=1  order by 1 asc,3 asc ,2 asc;
BEGIN
  OPEN c1;
  LOOP
    FETCH c1
      INTO v_TABLE_NAME, v_column_name, v_data_type, v_tuomintype ;
    EXIT WHEN c1%NOTFOUND;
  v_text2 := 'update '|| v_TABLE_NAME||' set '||v_column_name||'='||   'F_GET_SENSITIVE2020('||v_column_name||','||v_tuomintype||') ';
--execute immediate v_text2;
v_textprint:='update '|| v_TABLE_NAME||' set '||v_column_name||'='||   'F_GET_SENSITIVE2020('||v_column_name||','||v_tuomintype||') '||';';
DBMS_OUTPUT.PUT_LINE(v_textprint);
   END LOOP;
  commit;
  close c1;
END;
/



update tuomintable set tuomintype=12, needtuomin=1 where data_type='NUMBER';
commit;
select * from tuomintable;
11    TEST2    SAL    NUMBER    12    1

得到要更新的表
update TUOMINTABLE set NEEDTUOMIN=F_GET_SENSITIVE(NEEDTUOMIN,12)
update TUOMINTABLE set TUOMINTYPE=F_GET_SENSITIVE(TUOMINTYPE,12)
update TEST3 set DEPTNO=F_GET_SENSITIVE(DEPTNO,12)
update TEST2 set DEPTNO=F_GET_SENSITIVE(DEPTNO,12)
update TEST2 set COMM=F_GET_SENSITIVE(COMM,12)
update TEST2 set SAL=F_GET_SENSITIVE(SAL,12)
update TEST2 set MGR=F_GET_SENSITIVE(MGR,12)
update TEST2 set EMPNO=F_GET_SENSITIVE(EMPNO,12)
update TEST1 set SAMPLE_SIZE=F_GET_SENSITIVE(SAMPLE_SIZE,12)
update TEST1 set NUM_FREELIST_BLOCKS=F_GET_SENSITIVE(NUM_FREELIST_BLOCKS,12)
update TEST1 set
AVG_SPACE_FREELIST_BLOCKS=F_GET_SENSITIVE(AVG_SPACE_FREELIST_BLOCKS,12)
update TEST1 set AVG_ROW_LEN=F_GET_SENSITIVE(AVG_ROW_LEN,12)
update TEST1 set CHAIN_CNT=F_GET_SENSITIVE(CHAIN_CNT,12)
update TEST1 set AVG_SPACE=F_GET_SENSITIVE(AVG_SPACE,12)
update TEST1 set EMPTY_BLOCKS=F_GET_SENSITIVE(EMPTY_BLOCKS,12)
update TEST1 set BLOCKS=F_GET_SENSITIVE(BLOCKS,12)
update TEST1 set NUM_ROWS=F_GET_SENSITIVE(NUM_ROWS,12)
update TEST1 set FREELIST_GROUPS=F_GET_SENSITIVE(FREELIST_GROUPS,12)
update TEST1 set FREELISTS=F_GET_SENSITIVE(FREELISTS,12)
update TEST1 set PCT_INCREASE=F_GET_SENSITIVE(PCT_INCREASE,12)
update TEST1 set MAX_EXTENTS=F_GET_SENSITIVE(MAX_EXTENTS,12)
update TEST1 set MIN_EXTENTS=F_GET_SENSITIVE(MIN_EXTENTS,12)
update TEST1 set NEXT_EXTENT=F_GET_SENSITIVE(NEXT_EXTENT,12)
update TEST1 set INITIAL_EXTENT=F_GET_SENSITIVE(INITIAL_EXTENT,12)
update TEST1 set MAX_TRANS=F_GET_SENSITIVE(MAX_TRANS,12)
update TEST1 set INI_TRANS=F_GET_SENSITIVE(INI_TRANS,12)
update TEST1 set PCT_USED=F_GET_SENSITIVE(PCT_USED,12)
update TEST1 set PCT_FREE=F_GET_SENSITIVE(PCT_FREE,12)




oracle如何脱敏

创建脱敏表:create table tuomintable(TABLE_NAME varchar(50), column_name varchar(50), data_type varchar(50), tuomintype...
点击下载文档
确认删除?
回到顶部
客服QQ
  • 客服QQ点击这里给我发消息