forked from DavidWiseman/sqlserver-kit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dbo.usp_GenerateRandomPassword.sql
104 lines (100 loc) · 3.44 KB
/
dbo.usp_GenerateRandomPassword.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
IF OBJECT_ID('dbo.usp_GenerateRandomPassword') IS NULL
EXEC('CREATE PROCEDURE dbo.usp_GenerateRandomPassword AS SELECT 1;');
GO
ALTER PROCEDURE dbo.usp_GenerateRandomPassword(
@length int = 20,
@allowAtoZ BIT = 1,
@allow0to9 BIT = 1,
@allowSpecials1 BIT = 1,
@allowSpecials2 BIT = 1,
@avoidAmbiguousCharacters BIT = 1)
AS
/*
summary: >
This procedure generate random passwords with some customization
result.
Source link: https://github.com/ktaranov/sqlserver-kit/blob/master/Stored_Procedure/dbo.usp_GenerateRandomPassword.sql
Revisions:
- Author: Pinal Dave
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
Version: 1.0
Modification: First version
date: 2018-08-15
- version: 1.1
modification: add CREATE and ALTER logic, add documentation, format code
date: 2019-01-29 by Konstantin Taranov
Example:
- code: EXEC dbo.usp_GenerateRandomPassword @avoidAmbiguousCharacters = 0;
EXEC dbo.usp_GenerateRandomPassword @length = 30, @allow0to9 = 0, @allowSpecials1 = 0, @allowSpecials2 = 0, @avoidAmbiguousCharacters = 0;
EXEC dbo.usp_GenerateRandomPassword @length = 40, @allow0to9 = 0, @allowSpecials1 = 0, @allowSpecials2 = 1, @avoidAmbiguousCharacters = 1;
EXEC dbo.usp_GenerateRandomPassword @length = 90, @allowAtoZ = 0, @allow0to9 = 0, @allowSpecials1 = 1, @allowSpecials2 = 1;
IF OBJECT_ID(N't', 'U') IS NULL
BEGIN
CREATE TABLE t(
[Password] NVARCHAR(4000),
PasswordLength INT,
AllowAtoZ BIT,
Allow0to9 BIT,
AllowSpecials1 BIT,
AllowSpecials2 BIT,
AvoidAmbiguousCharacters BIT);
END;
DECLARE @i INT = 1;
WHILE @i <= 50
BEGIN
INSERT INTO t EXEC dbo.usp_GenerateRandomPassword;
SET @i = @i + 1;
END;
SELECT * FROM t;
DROP TABLE t;
returns: >
single row with new random password and all input parameters.
*/
BEGIN
DECLARE @range Varchar(90) = ''
-- Start by assuming @avoidAmbiguousCharacters is true
IF @allowAtoZ = 1
BEGIN
SET @range = @range +'ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz';
END
IF @allow0to9 = 1
BEGIN
SET @range = @range +'23456789';
END
IF @allowSpecials1 = 1
BEGIN
SET @range = @range +'!"#$%&''()*+,-./';
END
IF @allowSpecials2 = 1
BEGIN
SET @range = @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 = @range +'IOl';
END
IF @allow0to9 = 1
BEGIN
SET @range = @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)))
) AS fn
SELECT @pwd AS [Password],
@length AS PasswordLength,
@allowAtoZ,
@allow0to9 AS Allow0to9,
@allowSpecials1 AS AllowSpecials1,
@allowSpecials2 AS AllowSpecials2,
@avoidAmbiguousCharacters AS AvoidAmbiguousCharacters;
END;
GO