Thursday, April 19, 2012

Password change function which validates minimum Length, Upper Case, Lower Case and digit functionality

CREATE OR REPLACE FUNCTION change_password
(userid VARCHAR2,
old_password VARCHAR2,
new_password VARCHAR2)

RETURN VARCHAR2
IS

upperchar_found NUMBER;
lowerchar_found NUMBER;
digit_found NUMBER;
not_include_list_found NUMBER;
valid_pass VARCHAR2 (100);
pass_length NUMBER;
digits_list VARCHAR2 (20);
upperchar_list VARCHAR2 (26);
lowerchar_list VARCHAR2 (26);
not_include_list VARCHAR2 (100);

BEGIN

valid_pass := '0';
upperchar_found := 0;
lowerchar_found := 0;
digit_found := 0;
not_include_list_found := 0;
digits_list := '1234567890';
not_include_list := '`~!@#$%^&*()-_=+\|]}[{";:/?.>, <''';
upperchar_list := 'QWERTYUIOPASDFGHJKLZXCVBNM';
lowerchar_list := 'qwertyuiopasdfghjklzxcvbnm';
pass_length := LENGTH (new_password);

IF new_password = old_password
THEN
valid_pass := 'Old and New password not same! Please enter different';
ELSIF LENGTH (new_password) < 6
THEN
valid_pass := 'Minimum length of new password shall be 6 characters';
ELSE


--1. Check special characters
FOR i IN 1 .. LENGTH (not_include_list)
LOOP
FOR j IN 1 .. pass_length
LOOP
IF SUBSTR (new_password, j, 1) = SUBSTR (not_include_list, i, 1) AND not_include_list_found = 0
THEN
not_include_list_found := 1;
END IF;
END LOOP;
END LOOP;


--2. Check for at least one upper case letter

FOR i IN 1 .. LENGTH (upperchar_list)
LOOP
FOR j IN 1 .. pass_length
LOOP
IF SUBSTR (new_password, j, 1) = SUBSTR (upperchar_list, i, 1) AND upperchar_found = 0
THEN
upperchar_found := 1;
END IF;
END LOOP;
END LOOP;


--3. Check for at least one lower case letter

FOR i IN 1 .. LENGTH (lowerchar_list)
LOOP
FOR j IN 1 .. pass_length
LOOP
IF SUBSTR (new_password, j, 1) = SUBSTR (lowerchar_list, i, 1) AND lowerchar_found = 0
THEN
lowerchar_found := 1;
END IF;
END LOOP;
END LOOP;


--4. Check for at least one digit

FOR i IN 1 .. LENGTH (digits_list)
LOOP
FOR j IN 1 .. pass_length
LOOP
IF SUBSTR (new_password, j, 1) = SUBSTR (digits_list, i, 1) AND digit_found = 0
THEN
digit_found := 1;
END IF;
END LOOP;
END LOOP;


IF not_include_list_found = 1
THEN
valid_pass := 'Special Character not allowed in password';
ELSIF upperchar_found = 0
THEN
valid_pass := 'Password must contain at least 1 upper character';
ELSIF lowerchar_found = 0
THEN
valid_pass := 'Password must contain at least 1 lower character';
ELSIF digit_found = 0
THEN
valid_pass := 'Password must contain at least 1 digit character';
ELSE
valid_pass := '1';
END IF;
END IF;


RETURN (valid_pass);
END;
/