Situatie
Sunt cazuri in care avem nevoie sa punem parole pt diferite servicii (sau chiar si pt useri cu drepturi mai mari) si vrem sa fie o parola care sa fie puternica si sa nu fie usor de spart.
In cazul in care nu avem inspiratie , am creat in TSQL un generator de parole care primeste urmatorii parametri:
@useNumbers de tip bit – daca are valoarea 1 se vor folosi numere la generarea parolei, daca are valoarea 0 nu se vor folosi numere la generarea parolei
@useLowerCase de tip bit – daca are valoarea 1 se vor folosi litere mici la generarea parolei, daca are valoarea 0 nu se vor folosi litere mici la generarea parolei
@useUpperCase de tip bit – daca are valoarea 1 se vor folosi litere mari la generarea parolei, daca are valoarea 0 nu se vor folosi litere mari la generarea parolei
@charactersToUse de tip varchar – daca se pune null se vor folosi toate literele alfabetului, daca nu se doreste asta se poate pune un string din care se vor alege caracterele pt generarea parolei
@specialCharacters de tip bit – daca are valoarea 1 se vor folosi si caractere speciale la generarea parolei, daca are valoarea 0 nu se vor folosi caractere speciale
@passwordLength de tip int – lungimea parolei
Procedura are inca un parametru de tip OUT si se apeleaza astfel:
declare @a varchar(10) — unde @a este lungimea parolei
EXEC BD_GeneratorParola 1,1,1,null, 1, 10,@a OUT
PRINT @a
Solutie
create procedure dbo.BD_GeneratorParola (
@useNumbers bit,
@useLowerCase bit,
@useUpperCase bit,
@charactersToUse as varchar(100),
@specialCharacters bit,
@passwordLength as int,
@password varchar(100) OUT
)
As
Begin
if @passwordLength <= 0
raiserror(‘Parametrul cu numarul 4 trebuie sa fie null sau un string!’,16,1)
set @passwordLength = @passwordLength -1
declare @characters varchar(100)
declare @count int
set @characters = ”
if @useNumbers = 1
begin
set @count = 48
while @count <=57
begin
set @characters = @characters + Cast(CHAR(@count) as char(1))
set @count = @count + 1
end
end
if @useLowerCase = 1
begin
set @count = 65
while @count <=90
begin
set @characters = @characters + Cast(CHAR(@count) as char(1))
set @count = @count + 1
end
end
if @useUpperCase = 1
begin
set @count = 97
while @count <=122
begin
set @characters = @characters + Cast(CHAR(@count) as char(1))
set @count = @count + 1
end
end
set @count = 0
set @password = ”
if Len(@charactersToUse) > 0
begin
while charindex(@charactersToUse,’ ‘) > 0
begin
set @charactersToUse = replace(@charactersToUse,’ ‘,”)
end
if Len(@charactersToUse) = 0
raiserror(‘Parametrul cu numarul 4 trebuie sa fie null sau un string!’,16,1)
while @count <= @passwordLength
begin
if @specialCharacters = 1
set @characters = @characters+’!@#$.’
set @password = @password +
SUBSTRING(@charactersToUse,CAST(ABS(CHECKSUM(NEWID()))*RAND(@count) as
int)%LEN(@charactersToUse)+1,1)
set @count = @count + 1
end
end
else
begin
while @count <= @passwordLength
begin
if @specialCharacters = 1
set @characters = @characters+’!@#$.’
set @password = @password +
SUBSTRING(@characters,CAST(ABS(CHECKSUM(NEWID()))*RAND(@count) as
int)%LEN(@characters)+1,1)
set @count = @count + 1
end
end
–SELECT @password
end
GO
Leave A Comment?