使用MD5对数据表字段进行加密

--3.2.1 创建数据表
Drop Table Test_User;
CREATE TABLE Test_User (
  UserName VARCHAR2(30)  NOT NULL ,
  PassWord  VARCHAR2(2000)  NOT NULL
)
/
 
--3.2.2 创建程序包
Create Or Replace Package Test_MD5 AS
  Function FN_GetMD5(P_Str In VarChar2)
    Return VarChar2;
  Function FN_CheckUser
   (P_UserName In VarChar2
     ,P_Password In VarChar2)
    Return Number;
End ;
/
CREATE OR REPLACE PACKAGE BODY Test_MD5 AS  
 
  FUNCTION FN_GetMD5 (P_Str  IN  VARCHAR2) 
   RETURN VARCHAR2 AS  
  BEGIN  
   RETURN DBMS_OBFUSCATION_TOOLKIT.MD5( 
   input_string => Upper (P_Str)); 
  END ; 
  Function FN_CheckUser 
   (P_UserName  IN  VARCHAR2 
    ,P_Password  IN  VARCHAR2) 
   Return Number 
  Is  
   L_Password VarChar2(2000); 
  BEGIN  
   SELECT  
     Utl_Raw.Cast_To_Raw( Password ) 
   INTO  
     L_Password 
   FROM   
     Test_User 
   WHERE   
     Upper (UserName) = UPPER (P_UserName); 
   If Utl_Raw.Cast_To_Raw(FN_GetMD5(P_Password))=L_Password Then  
    Return 1; 
    Else  
     Return 0; 
    End If; 
 EXCEPTION 
   WHEN NO_DATA_FOUND THEN  
    Return 0; 
  END ; 
End ; 
/ 
--3.3 测试
Delete Test_User;
Insert Into Test_User Values ( 'A' ,Test_MD5.FN_GetMD5( 'aa' ));
Insert Into Test_User Values ( 'B' ,Test_MD5.FN_GetMD5( 'bb' ));
Commit ;
Select Test_MD5.FN_CheckUser( 'a' , 'aa' ) From Dual;
Select Test_MD5.FN_CheckUser( 'a' , 'bb' ) From Dual;

你可能感兴趣的:(数据库)