Create a complex password
CREATE PROCEDURE dbo.uspCreatePassword SET NOCOUNT ON – Initialize some variables – Set the default items in each group of characters – Enforce some limits on the length of the password IF @UpperCaseItems < -20 IF @LowerCaseItems > 20 IF @LowerCaseItems < -20 IF @NumberItems > 20 IF @NumberItems < -20 IF @SpecialItems > 20 IF @SpecialItems < -20 – Get the Upper Case Items WHILE @i > 0 AND LEN(@UpperCase) > 0 – Get the Lower Case Items WHILE @i > 0 AND LEN(@LowerCase) > 0 – Get the Number Items WHILE @i > 0 AND LEN(@Numbers) > 0 – Get the Special Items WHILE @i > 0 AND LEN(@Special) > 0 – Scramble the order of the selected items SELECT @Password
(
@UpperCaseItems SMALLINT,
@LowerCaseItems SMALLINT,
@NumberItems SMALLINT,
@SpecialItems SMALLINT
)
AS
DECLARE @UpperCase VARCHAR(26),
@LowerCase VARCHAR(26),
@Numbers VARCHAR(10),
@Special VARCHAR(13),
@Temp VARCHAR(8000),
@Password VARCHAR(8000),
@i SMALLINT,
@c VARCHAR(1),
@v TINYINT
SELECT @UpperCase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
@LowerCase = 'abcdefghijklmnopqrstuvwxyz',
@Numbers = '0123456789',
@Special = '!@#$%&*()_+-=',
@Temp = '',
@Password = ''
IF @UpperCaseItems > 20
SET @UpperCaseItems = 20
SET @UpperCaseItems = -20
SET @LowerCaseItems = 20
SET @LowerCaseItems = -20
SET @NumberItems = 20
SET @NumberItems = -20
SET @SpecialItems = 20
SET @SpecialItems = -20
SET @i = ABS(@UpperCaseItems)
SELECT @v = ABS(CAST(CAST(NEWID() AS BINARY(16)) AS BIGINT)) % LEN(@UpperCase) + 1,
@c = SUBSTRING(@UpperCase, @v, 1),
@UpperCase = CASE
WHEN @UpperCaseItems < 0 THEN STUFF(@UpperCase, @v, 1, '')
ELSE @UpperCase
END,
@Temp = @Temp + @c,
@i = @i - 1
SET @i = ABS(@LowerCaseItems)
SELECT @v = ABS(CAST(CAST(NEWID() AS BINARY(16)) AS BIGINT)) % LEN(@LowerCase) + 1,
@c = SUBSTRING(@LowerCase, @v, 1),
@LowerCase = CASE
WHEN @LowerCaseItems < 0 THEN STUFF(@LowerCase, @v, 1, '')
ELSE @LowerCase
END,
@Temp = @Temp + @c,
@i = @i - 1
SET @i = ABS(@NumberItems)
SELECT @v = ABS(CAST(CAST(NEWID() AS BINARY(16)) AS BIGINT)) % LEN(@Numbers) + 1,
@c = SUBSTRING(@Numbers, @v, 1),
@Numbers = CASE
WHEN @NumberItems < 0 THEN STUFF(@Numbers, @v, 1, '')
ELSE @Numbers
END,
@Temp = @Temp + @c,
@i = @i - 1
SET @i = ABS(@SpecialItems)
SELECT @v = ABS(CAST(CAST(NEWID() AS BINARY(16)) AS BIGINT)) % LEN(@Special) + 1,
@c = SUBSTRING(@Special, @v, 1),
@Special = CASE
WHEN @SpecialItems < 0 THEN STUFF(@Special, @v, 1, '')
ELSE @Special
END,
@Temp = @Temp + @c,
@i = @i - 1
WHILE LEN(@Temp) > 0
SELECT @v = ABS(CAST(CAST(NEWID() AS BINARY(16)) AS BIGINT)) % LEN(@Temp) + 1,
@Password = @Password + SUBSTRING(@Temp, @v, 1),
@Temp = STUFF(@Temp, @v, 1, '')
Legacy Comments
Ron Bracale
2008-11-25 |
re: Create a complex password Works like a charm. Thanks. |
casinot
2011-05-03 |
re: Create a complex password Feeling gratitude and not expressing it is like wrapping a present and not giving it. So it would be marked with the lot of enthusiasm and appreciation |