|
| 1 | +IF OBJECT_ID('dbo.usp_GenerateRandomPassword') IS NULL |
| 2 | + EXEC('CREATE PROCEDURE dbo.usp_GenerateRandomPassword AS SELECT 1;'); |
| 3 | +GO |
| 4 | + |
| 5 | + |
| 6 | +ALTER PROCEDURE dbo.usp_GenerateRandomPassword( |
| 7 | + @length int = 20, |
| 8 | + @allowAtoZ BIT = 1, |
| 9 | + @allow0to9 BIT = 1, |
| 10 | + @allowSpecials1 BIT = 1, |
| 11 | + @allowSpecials2 BIT = 1, |
| 12 | + @avoidAmbiguousCharacters BIT = 1) |
| 13 | +AS |
| 14 | +/* |
| 15 | +summary: > |
| 16 | + This procedure generate random passwords with some customization |
| 17 | + result. |
| 18 | + Source link: https://github.com/ktaranov/sqlserver-kit/blob/master/Stored_Procedure/dbo.usp_GenerateRandomPassword.sql |
| 19 | +Revisions: |
| 20 | + - Author: Pinal Dave |
| 21 | + Original link: https://blog.sqlauthority.com/2018/08/15/sql-server-how-to-generate-random-password-enhanced-version-part-2/?utm_source=DBW&utm_medium=pubemail |
| 22 | + Version: 1.0 |
| 23 | + Modification: First version |
| 24 | + date: 2018-08-15 |
| 25 | + - version: 1.1 |
| 26 | + modification: add CREATE and ALTER logic, add documentation, format code |
| 27 | + date: 2019-01-29 by Konstantin Taranov |
| 28 | +Example: |
| 29 | + - code: EXEC dbo.usp_GenerateRandomPassword @avoidAmbiguousCharacters = 0; |
| 30 | + EXEC dbo.usp_GenerateRandomPassword @length = 30, @allow0to9 = 0, @allowSpecials1 = 0, @allowSpecials2 = 0, @avoidAmbiguousCharacters = 0; |
| 31 | + EXEC dbo.usp_GenerateRandomPassword @length = 40, @allow0to9 = 0, @allowSpecials1 = 0, @allowSpecials2 = 1, @avoidAmbiguousCharacters = 1; |
| 32 | + EXEC dbo.usp_GenerateRandomPassword @length = 90, @allowAtoZ = 0, @allow0to9 = 0, @allowSpecials1 = 1, @allowSpecials2 = 1; |
| 33 | +
|
| 34 | + IF OBJECT_ID(N't', 'U') IS NULL |
| 35 | + BEGIN |
| 36 | + CREATE TABLE t( |
| 37 | + [Password] NVARCHAR(4000), |
| 38 | + PasswordLength INT, |
| 39 | + AllowAtoZ BIT, |
| 40 | + Allow0to9 BIT, |
| 41 | + AllowSpecials1 BIT, |
| 42 | + AllowSpecials2 BIT, |
| 43 | + AvoidAmbiguousCharacters BIT); |
| 44 | + END; |
| 45 | + DECLARE @i INT = 1; |
| 46 | + WHILE @i <= 50 |
| 47 | + BEGIN |
| 48 | + INSERT INTO t EXEC dbo.usp_GenerateRandomPassword; |
| 49 | + SET @i = @i + 1; |
| 50 | + END; |
| 51 | + SELECT * FROM t; |
| 52 | + DROP TABLE t; |
| 53 | +returns: > |
| 54 | + single row with new random password and all input parameters. |
| 55 | +*/ |
| 56 | +BEGIN |
| 57 | + DECLARE @range Varchar(90) = '' |
| 58 | + -- Start by assuming @avoidAmbiguousCharacters is true |
| 59 | + IF @allowAtoZ = 1 |
| 60 | +BEGIN |
| 61 | + SET @range = @range +'ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'; |
| 62 | + END |
| 63 | + IF @allow0to9 = 1 |
| 64 | +BEGIN |
| 65 | + SET @range = @range +'23456789'; |
| 66 | + END |
| 67 | + IF @allowSpecials1 = 1 |
| 68 | +BEGIN |
| 69 | + SET @range = @range +'!"#$%&''()*+,-./'; |
| 70 | + END |
| 71 | + IF @allowSpecials2 = 1 |
| 72 | +BEGIN |
| 73 | + SET @range = @range +':;<=>?@'; |
| 74 | + END |
| 75 | + IF @avoidAmbiguousCharacters = 0 |
| 76 | +BEGIN |
| 77 | + -- put back capital i, lowercase l, capital o, the number(s) 0, 1 |
| 78 | + IF @allowAtoZ = 1 |
| 79 | +BEGIN |
| 80 | + SET @range = @range +'IOl'; |
| 81 | + END |
| 82 | + IF @allow0to9 = 1 |
| 83 | +BEGIN |
| 84 | + SET @range = @range +'01'; |
| 85 | + END |
| 86 | + END |
| 87 | + DECLARE @pwd VARCHAR(512) = ''; |
| 88 | + --use master.dbo.spt_values as a pseudo tally(numbers) table |
| 89 | + |
| 90 | + SELECT TOP(@length) |
| 91 | + @pwd += SUBSTRING(@range,fn.rnd,1) |
| 92 | + FROM master.dbo.spt_values sv |
| 93 | + CROSS APPLY ( |
| 94 | + SELECT [rnd] = (ABS(CHECKSUM(NEWID())) % (LEN(@range))) |
| 95 | + ) AS fn |
| 96 | + SELECT @pwd AS [Password], |
| 97 | + @length AS PasswordLength, |
| 98 | + @allowAtoZ, |
| 99 | + @allow0to9 AS Allow0to9, |
| 100 | + @allowSpecials1 AS AllowSpecials1, |
| 101 | + @allowSpecials2 AS AllowSpecials2, |
| 102 | + @avoidAmbiguousCharacters AS AvoidAmbiguousCharacters; |
| 103 | +END; |
| 104 | +GO |
0 commit comments