Tuesday, January 6, 2009

Generate Random Password Oracle

When user tries to reset password, new password should be generated randomly which is always independent with respect to user details.

Below oracle function returns such randomly generated password of specific length.

CREATE OR REPLACE FUNCTION generatePassword(LENGTH IN NUMBER)
RETURN VARCHAR
IS
tmpChr VARCHAR2(1);
tmpPswd VARCHAR2(32767);
BEGIN
--Number of loops equal to the length specified
FOR i IN 1 .. LENGTH
LOOP
--1. Generate a random integer between 48 and 122 because to generate string that
--has alphanumeric charaters and few special characters availbale on key board
--2. Get the caharacter whose ascii is equal to the generated ascii code
SELECT CHR(ROUND(dbms_random.value(48,122),0)) INTO tmpChr FROM dual;
--Appending each randomly generated character
tmpPswd:=tmpPswd||tmpChr;
END LOOP;
--returning the final created random password
RETURN tmpPswd;
END;
After creating the above function call the function with 10 as input parameter which generates a text or password of length 10.
Run query:-

select generatePassword(10) as password from dual

Output:-

rZdjSZo7eg





No comments: