使用MD5编码实现数据库用户密码字段的加密

清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>

  3 实例演练  
  3.1 测试环境 
  使用Scott/Tiger@YourDBName 
  
  --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; 
  
  注:对MD5编码的比较,可以不需要经过Utl_Raw.Cast_To_Raw转换。此处使用Utl_Raw.Cast_To_Raw的目的,是为了调试方便,同时多提供一个知识点。