Function to create random password Genpass(p integer, p uppercase p lowercase p special characters) return password varchar2; select Genpassword(2,2,1,2) It contain 2 numbers,2 uppercase characters ,special character like # and 2 numbers;
-
What is your question?MarEll– MarEll2021-11-17 14:25:03 +00:00Commented Nov 17, 2021 at 14:25
-
My question is about random password generate function which creates passwords contain numbers ,letters and special characters in plsqlAhmed– Ahmed2021-11-17 14:26:55 +00:00Commented Nov 17, 2021 at 14:26
-
Your question is not clear. Do you need a function? Do you have a function that is not working? What is your effort so far and where do you stuck?MarEll– MarEll2021-11-18 07:01:40 +00:00Commented Nov 18, 2021 at 7:01
-
create or replace FUNCTION GENPWD( PNOIT IN NUMBER, PSC IN NUMBER, PL IN NUMBER, PU IN NUMBER) return varchar2 is pwd varchar2(11); begin select into pwd:= dbms_random.string('U',pu), pwd:=dbms_random.string('l',pl), pwd:= dbms_random.string('x',psc), pwd:= dbms_random.string('a',pnoit) from dual; return pwd; end;Ahmed– Ahmed2021-11-18 08:21:52 +00:00Commented Nov 18, 2021 at 8:21
1 Answer
The Oracle DBMS_RANDOM.STRING functionality does not allow you to state you want special characters expressly. Also, when you use DBMS_RANDOM.STRING for each type of character, you end up with a series that always has the same structure (which might even be a security issue again).
But if you do not want to have the same structure all the time, you would have to write something that would (in yet another random way) distribute the characters of your structured password.
Another option is to use an example I adopted from an AskTom entry; you leverage the existing functionality and try until you have a result:
create or replace FUNCTION GENPWD(
p_numbers IN NUMBER,
p_specialchar IN NUMBER,
p_lowercase IN NUMBER,
p_uppercase IN NUMBER) return varchar2
IS
v_length number := p_numbers + p_specialchar + p_lowercase + p_uppercase;
v_password varchar2(200);
v_iterations number := 0;
v_max_iterations number := 500;
BEGIN
loop
v_password := dbms_random.string('p',v_length);
v_iterations := v_iterations + 1;
exit when (regexp_count(v_password,'[a-z]') = p_lowercase
and regexp_count(v_password,'[A-Z]') = p_uppercase
and regexp_count(v_password,'[0-9]') = p_numbers)
or v_iterations=v_max_iterations;
end loop;
if v_iterations = v_max_iterations THEN
v_password := '';
end if;
return(v_password);
END;
3 Comments
DBMS_RANDOM is suitable for password generation? Note that Oracle writes: "DBMS_RANDOM is not intended for cryptography." There appears to be a DBMS_CRYPTO interface with RANDOM*-Functions.