ORACLE 密碼驗證函式

G8bao7發表於2016-12-22

點選(此處)摺疊或開啟

  1. CREATE OR REPLACE FUNCTION verify_function
  2. (username varchar2,
  3. password varchar2,
  4. old_password varchar2)
  5. RETURN boolean IS
  6. n boolean;
  7. m integer;
  8. differ integer;
  9. isdigit boolean;
  10. ischar boolean;
  11. ispunct boolean;
  12. digitarray varchar2(20);
  13. punctarray varchar2(25);
  14. chararray varchar2(52);
  15. BEGIN
  16. digitarray:= '0123456789';
  17. chararray:= 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  18. punctarray:='!"#$%&()``*+,-/:;<=>?_';
  19. -- Check if the password is same as the username
  20. IF NLS_LOWER(password) = NLS_LOWER(username) THEN
  21. raise_application_error(-20001, 'Password same as or similar to user');
  22. END IF;
  23. -- Check for the minimum length of the password
  24. IF length(password) < 4 THEN
  25. raise_application_error(-20002, 'Password length less than 4');
  26. END IF;
  27. -- Check if the password is too simple. A dictionary of words may be
  28. -- maintained and a check may be made so as not to allow the words
  29. -- that are too simple for the password.
  30. IF NLS_LOWER(password) IN ('welcome', 'database', 'account', 'user', 'password', 'oracle', 'computer', 'abcd') THEN
  31. raise_application_error(-20002, 'Password too simple');
  32. END IF;
  33. -- Check if the password contains at least one letter, one digit and one
  34. -- punctuation mark.
  35. -- 1. Check for the digit
  36. isdigit:=FALSE;
  37. m := length(password);
  38. FOR i IN 1..10 LOOP
  39. FOR j IN 1..m LOOP
  40. IF substr(password,j,1) = substr(digitarray,i,1) THEN
  41. isdigit:=TRUE;
  42. GOTO findchar;
  43. END IF;
  44. END LOOP;
  45. END LOOP;
  46. IF isdigit = FALSE THEN
  47. raise_application_error(-20003, 'Password should contain at least one digit, one character and one punctuation');
  48. END IF;
  49. -- 2. Check for the character
  50. <<findchar>>
  51. ischar:=FALSE;
  52. FOR i IN 1..length(chararray) LOOP
  53. FOR j IN 1..m LOOP
  54. IF substr(password,j,1) = substr(chararray,i,1) THEN
  55. ischar:=TRUE;
  56. GOTO findpunct;
  57. END IF;
  58. END LOOP;
  59. END LOOP;
  60. IF ischar = FALSE THEN
  61. raise_application_error(-20003, 'Password should contain at least one
  62. digit, one character and one punctuation');
  63. END IF;
  64. -- 3. Check for the punctuation
  65. <<findpunct>>
  66. ispunct:=FALSE;
  67. FOR i IN 1..length(punctarray) LOOP
  68. FOR j IN 1..m LOOP
  69. IF substr(password,j,1) = substr(punctarray,i,1) THEN
  70. ispunct:=TRUE;
  71. GOTO endsearch;
  72. END IF;
  73. END LOOP;
  74. END LOOP;
  75. IF ispunct = FALSE THEN
  76. raise_application_error(-20003, 'Password should contain at least one
  77. digit, one character and one punctuation');
  78. END IF;
  79. <<endsearch>>
  80. -- Check if the password differs from the previous password by at least
  81. -- 3 letters
  82. IF old_password IS NOT NULL THEN
  83. differ := length(old_password) - length(password);
  84. IF abs(differ) < 3 THEN
  85. IF length(password) < length(old_password) THEN
  86. m := length(password);
  87. ELSE
  88. m := length(old_password);
  89. END IF;
  90. differ := abs(differ);
  91. FOR i IN 1..m LOOP
  92. IF substr(password,i,1) != substr(old_password,i,1) THEN
  93. differ := differ + 1;
  94. END IF;
  95. END LOOP;
  96. IF differ < 3 THEN
  97. raise_application_error(-20004, 'Password should differ by at
  98. least 3 characters');
  99. END IF;
  100. END IF;
  101. END IF;
  102. -- Everything is fine; return TRUE ;
  103. RETURN(TRUE);
  104. END;

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/26250550/viewspace-2131237/,如需轉載,請註明出處,否則將追究法律責任。

相關文章