https://blog.sqlauthority.com/2018/08/02/sql-server-how-to-generate-random-password-enhanced-version/
CREATE PROCEDURE GenerateRandomPwd1 (@length int = 20, @allowAtoZ BIT = 1, @allow0to9 BIT = 1, @allowSpecials1 BIT = 1, @allowSpecials2 BIT = 1, @avoidAmbiguousCharacters BIT = 1) AS BEGIN DECLARE @pwd VARCHAR (512) = '' --use master.dbo.spt_values as a pseudo tally(numbers) table SELECT TOP (@length) @pwd += CHAR (fn.rnd) FROM master.dbo.spt_values sv CROSS APPLY ( -- (max - min) + min SELECT [rnd] = ( ABS (CHECKSUM(NEWID())) % (123 - 33)) + 33 ) fn WHERE (@avoidAmbiguousCharacters = 0 OR fn.rnd NOT IN (73, 108, 124, 79, 48, 49)) --capital i, lowercase l, vertical bar | capital o, the number(s) 0, 1 AND ( (@allowAtoZ = 1 AND ((fn.rnd >= 65 AND fn.rnd <= 90) OR (fn.rnd >= 97 AND fn.rnd <= 122))) OR (@allow0to9 = 1 AND (fn.rnd >= 48 AND fn.rnd <= 57)) OR (@allowSpecials1 = 1 AND (fn.rnd >= 33 AND fn.rnd <= 47)) OR (@allowSpecials2 = 1 AND (fn.rnd >= 58 AND fn.rnd <= 64)) ) SELECT [ Password ] = @pwd, [@allowAtoZ] = @allowAtoZ, [@allow0to9] = @allow0to9, [@allowSpecials1] = @allowSpecials1, [@allowSpecials2] = @allowSpecials2, [@avoidAmbiguousCharacters] = @avoidAmbiguousCharacters END GO |
Now let us run the same stored procedure with few parameters.
1
2
3
4
| EXEC GenerateRandomPwd1 @length = 20, @avoidAmbiguousCharacters = 0 EXEC GenerateRandomPwd1 @length = 20, @allow0to9 = 0, @allowSpecials1 = 0, @allowSpecials2 = 0, @avoidAmbiguousCharacters = 0 EXEC GenerateRandomPwd1 @length = 20, @allow0to9 = 0, @allowSpecials1 = 0, @allowSpecials2 = 0, @avoidAmbiguousCharacters = 1 EXEC GenerateRandomPwd1 @length = 20, @allowAtoZ = 0, @allow0to9 = 0, @allowSpecials1 = 1, @allowSpecials2 = 1 |
No comments:
Post a Comment