Labels

Wednesday, May 4, 2011

Password Generator

DECLARE @len int  --Length of the password to be generated
DECLARE @password_type varchar(7)
SET @len=8
SET @password_type= 'Complex'
DECLARE @password varchar(25), @type tinyint, @bitmap char(6)
SET @password=''
SET @bitmap = 'pardha'

WHILE @len > 0
BEGIN
      IF @password_type = 'simple' --Generating a simple password
      BEGIN
      IF (@len%2) = 0  --Appending a random vowel to @password
           
            SET @password = @password + SUBSTRING(@bitmap,CONVERT(int,ROUND(1 + (RAND() * (5)),0)),1)
      ELSE --Appending a random alphabet
            SET @password = @password + CHAR(ROUND(97 + (RAND() * (25)),0))
           
      END
      ELSE --Generating a complex password
      BEGIN
            SET @type = ROUND(1 + (RAND() * (3)),0)
            IF @type = 1 --Appending a random lower case alphabet to @password
                  SET @password = @password + CHAR(ROUND(97 + (RAND() * (25)),0))
            ELSE IF @type = 2 --Appending a random upper case alphabet to @password
                  SET @password = @password + CHAR(ROUND(65 + (RAND() * (25)),0))
            ELSE IF @type = 3 --Appending a random number between 0 and 9 to @password
                  SET @password = @password + CHAR(ROUND(48 + (RAND() * (9)),0))
            ELSE IF @type = 4 --Appending a random special character to @password
                  SET @password = @password + CHAR(ROUND(33 + (RAND() * (13)),0))
      END
      SET @len = @len - 1
END
SELECT @password --Here's the result

No comments:

Post a Comment