PL/SQL Random Password Generator [message #39518] |
Mon, 22 July 2002 04:55 |
Marko
Messages: 2 Registered: July 2002
|
Junior Member |
|
|
Hi all,
would some kind soul happen to have a PL/SQL script for random password generation? If it's possible to get something pronounceable that would be great.
Kindest regards,
Marko
|
|
|
|
Here it is [message #39544 is a reply to message #39518] |
Tue, 23 July 2002 00:18 |
Marko
Messages: 2 Registered: July 2002
|
Junior Member |
|
|
--Thanks Todd, I've found a version in ASP, re-coded it in pl/sql and combined it with random number generator found here on orafaq. It's somewhat lengthy code but works fine.
CREATE OR REPLACE PACKAGE Random AS
/*
* Random number generator. Uses the same algorithm as the
* rand() function in C.
* USAGE: select random.rand() from dual;
* USAGE: select random.pwd(8) from dual
*/
PROCEDURE ChangeSeed(p_NewSeed IN NUMBER);
-- Used to change the seed. From a given seed, the same
-- sequence of random numbers will be generated.
FUNCTION Rand RETURN NUMBER;
-- Returns a random integer between 1 and 32767.
FUNCTION Rnd RETURN NUMBER;
-- Returns a random integer between 0 and 1
Function Pwd(nLength IN NUMBER) RETURN VARCHAR2;
-- Generates a pronounceable random password
END Random;
/
CREATE OR REPLACE PACKAGE BODY Random AS
/* Used for calculating the next number. */
v_Multiplier CONSTANT NUMBER := 22695477;
v_Increment CONSTANT NUMBER := 1;
/* Seed used to generate random sequence. */
v_Seed NUMBER := 1;
PROCEDURE ChangeSeed(p_NewSeed IN NUMBER) IS
BEGIN
v_Seed := p_NewSeed;
END ChangeSeed;
FUNCTION Rand RETURN NUMBER IS
BEGIN
v_Seed := MOD(v_Multiplier * v_Seed + v_Increment, (2 ** 32));
RETURN BITAND(v_Seed/(2 ** 16), 32767);
END Rand;
FUNCTION Rnd RETURN NUMBER IS
pom NUMBER;
BEGIN
pom:=Rand();
RETURN pom/(POWER(10,LENGTH(pom-1)));
END Rnd;
Function Pwd(nLength IN NUMBER) RETURN VARCHAR2 IS
I NUMBER :=1;
strDoubleConsonants Constant VARCHAR2(12) := 'bdfglmnpst';
strConsonants Constant VARCHAR2(20):= 'bcdfghklmnpqrstv';
strVocal Constant VARCHAR2(5) := 'aeiou';
GeneratedPassword VARCHAR2(50) := '';
bMadeConsonant BOOLEAN := False;
nRnd NUMBER;
nSubRnd NUMBER;
c VARCHAR2(1);
Begin
FOR I IN 1..nLength LOOP
--Get a random number number between 0 and 1
SELECT random.rnd() INTO nRnd FROM dual;
SELECT random.rnd() INTO nSubRnd FROM dual;
/* Simple or double consonant, or a new vocal?
* Does not start with a double consonant
* '15% or less chance for the next letter being a double consonant
*/
IF GeneratedPassword <> '' AND (bMadeConsonant <> True) AND (nRnd < 0.15) THEN
--double consonant
c := SUBSTR(strDoubleConsonants, Length(strDoubleConsonants) * nSubRnd + 1, 1);
c := c || c;
bMadeConsonant := True;
ELSIF ((bMadeConsonant <> True) And (nRnd < 0.95)) THEN
--80% or less chance for the next letter being a consonant,
--depending on wether the last letter was a consonant or not.
--Simple consonant
c := SUBSTR(strConsonants, Length(strConsonants) * nSubRnd + 1, 1);
bMadeConsonant := True;
ELSE
--5% or more chance for the next letter being a vocal. 100% if last
--letter was a consonant - theoreticaly speaking...
--If last one was a consonant, make vocal
c := SUBSTR(strVocal, Length(strVocal) * nSubRnd + 1, 1);
bMadeConsonant := False;
END IF;
GeneratedPassword := GeneratedPassword || c;
END LOOP;
--Is the password long enough, or perhaps too long?
If (Length(GeneratedPassword) > nLength) Then
GeneratedPassword := SUBSTR(GeneratedPassword, 1, nLength);
End If;
RETURN GeneratedPassword;
End Pwd;
BEGIN
/* Package initialization. Initialize the seed to the current
time in seconds. */
ChangeSeed(TO_NUMBER(TO_CHAR(SYSDATE, 'SSSSS')));
END Random;
/
|
|
|
Re: Here it is [message #39557 is a reply to message #39518] |
Tue, 23 July 2002 08:13 |
Todd Barry
Messages: 4819 Registered: August 2001
|
Senior Member |
|
|
Ok, glad you found something. Remember though that there is already a random number generator built-in to Oracle (DBMS_RANDOM). It can also return numbers within specified ranges.
|
|
|