Last active
August 25, 2022 14:07
-
-
Save tcartwright/c27a45a4a211f7cc79466140fb80a56d to your computer and use it in GitHub Desktop.
SQL SERVER: Generate a random password (enhanced)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DROP PROCEDURE IF EXISTS dbo.GenerateRandomPwd | |
GO | |
CREATE PROCEDURE dbo.GenerateRandomPwd ( | |
@password VARCHAR(512) OUTPUT, | |
@length INT = 20, | |
@allowAtoZ BIT = 1, | |
@allow0to9 BIT = 1, | |
@allowSpecials1 BIT = 1, | |
@allowSpecials2 BIT = 1, | |
@avoidAmbiguousCharacters BIT = 1 | |
) | |
AS | |
BEGIN | |
SET NOCOUNT ON | |
DECLARE @range VARCHAR(90) = ''; | |
-- Start by assuming @avoidAmbiguousCharacters is true | |
IF @allowAtoZ = 1 BEGIN SET @range += 'ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'; END; | |
IF @allow0to9 = 1 BEGIN SET @range += '23456789'; END; | |
IF @allowSpecials1 = 1 BEGIN SET @range += '!"#$%&''()*+,-./'; END; | |
IF @allowSpecials2 = 1 BEGIN SET @range += ':;<=>?@'; END; | |
IF @avoidAmbiguousCharacters = 0 BEGIN | |
-- put back capital i, lowercase l, capital o, the number(s) 0, 1 | |
IF @allowAtoZ = 1 BEGIN SET @range += 'IOl'; END; | |
IF @allow0to9 = 1 BEGIN SET @range += '01'; END; | |
END; | |
DECLARE @pwd VARCHAR(512) = ''; | |
--use master.dbo.spt_values as a pseudo tally(numbers) table | |
SELECT TOP (@length) | |
@pwd += SUBSTRING(@range, fn.rnd, 1) | |
FROM master.dbo.spt_values sv | |
CROSS APPLY ( | |
SELECT rnd = (ABS(CHECKSUM(NEWID())) % (LEN(@range))) + 1 | |
) fn; | |
SET @password = @pwd | |
--PRINT CONCAT('Password = ', @pwd, | |
-- CHAR(10), CHAR(9), '[@allowAtoZ] = ', @allowAtoZ, | |
-- CHAR(10), CHAR(9), '[@allow0to9] = ', @allow0to9, | |
-- CHAR(10), CHAR(9), '[@allowSpecials1] = ', @allowSpecials1, | |
-- CHAR(10), CHAR(9), '[@allowSpecials2] = ', @allowSpecials2, | |
-- CHAR(10), CHAR(9), '[@avoidAmbiguousCharacters] = ', @avoidAmbiguousCharacters) | |
END; | |
GO | |
/* TESTS */ | |
SET NOCOUNT ON | |
DECLARE @pwd VARCHAR(512) = ''; | |
EXEC dbo.GenerateRandomPwd @password = @pwd OUTPUT, @length = 50, @avoidAmbiguousCharacters = 0; SELECT @pwd AS [@pwd], LEN(@pwd) AS [LEN(@pwd)]; | |
EXEC dbo.GenerateRandomPwd @password = @pwd OUTPUT, @length = 50, @allow0to9 = 0, @allowSpecials1 = 0, @allowSpecials2 = 0, @avoidAmbiguousCharacters = 0; SELECT @pwd AS [@pwd], LEN(@pwd) AS [LEN(@pwd)];; | |
EXEC dbo.GenerateRandomPwd @password = @pwd OUTPUT, @length = 50, @allow0to9 = 0, @allowSpecials1 = 0, @allowSpecials2 = 0, @avoidAmbiguousCharacters = 1; SELECT @pwd AS [@pwd], LEN(@pwd) AS [LEN(@pwd)];; | |
EXEC dbo.GenerateRandomPwd @password = @pwd OUTPUT, @length = 50, @allowAtoZ = 0, @allow0to9 = 0, @allowSpecials1 = 1, @allowSpecials2 = 1; SELECT @pwd AS [@pwd], LEN(@pwd) AS [LEN(@pwd)];; | |
EXEC dbo.GenerateRandomPwd @password = @pwd OUTPUT, @length = 512, @allowAtoZ = 1, @allow0to9 = 1, @allowSpecials1 = 0, @allowSpecials2 = 0; SELECT @pwd AS [@pwd], LEN(@pwd) AS [LEN(@pwd)]; | |
/* TESTS */ | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment