Hello people,
Good Morning!
In this quick post, I will show you how to create a small random password generator to be used in many different ways. I will make this script available using C # languages (for use on SQL Server, with CLR), PHP and Transact-SQL.
These scripts are pretty simple, but the idea is to demonstrate how to use this feature in the main 3 technologies I use.
How to create a password generator written in PHP
Source code:
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 |
<?php function fncGera_Senha( $Qt_Caracteres, $Fl_Letras, $Fl_Maiusculas, $Fl_Numeros, $Fl_Simbolos ) { if ( $Qt_Caracteres == 0 ) { return null; } if ( !$Fl_Letras && !$Fl_Numeros && !$Fl_Simbolos ) { return null; } $caracteresValidos = ""; if ( $Fl_Letras ) { $caracteresValidos .= "abcdefghijklmnopqrstuvwxyz"; } if ( $Fl_Letras && $Fl_Maiusculas ) { $caracteresValidos .= "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; } if ( $Fl_Numeros ) { $caracteresValidos .= "1234567890"; } if ( $Fl_Simbolos ) { } $valormaximo = strlen( $caracteresValidos ); $senha = ""; for ( $i = 0; $i < $Qt_Caracteres; $i++ ) { $senha .= $caracteresValidos[rand( 0, $valormaximo - 1 )]; } return $senha; } echo "Exemplo 1: " . fncGera_Senha(10, 1, 1, 1, 1) . "<br/>"; echo "Exemplo 2: " . fncGera_Senha(8, 1, 0, 1, 0) . "<br/>"; echo "Exemplo 3: " . fncGera_Senha(4, 1, 1, 0, 0) . "<br/>"; |
How to create a password generator written in C # (with CLR)
Source code:
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 |
using System; using System.Text; public partial class UserDefinedFunctions { [Microsoft.SqlServer.Server.SqlFunction] public static string fncGera_Senha(int Qt_Caracteres, bool Fl_Letras, bool Fl_Maiusculas, bool Fl_Numeros, bool Fl_Simbolos) { if (Qt_Caracteres == 0) return null; if (!Fl_Letras && !Fl_Numeros && !Fl_Simbolos) return null; var caracteresValidos = ""; if (Fl_Letras) caracteresValidos += "abcdefghijklmnopqrstuvwxyz"; if (Fl_Letras && Fl_Maiusculas) caracteresValidos += "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; if (Fl_Numeros) caracteresValidos += "1234567890"; if (Fl_Simbolos) var valormaximo = caracteresValidos.Length; var random = new Random(DateTime.Now.Millisecond); var senha = new StringBuilder(Qt_Caracteres); for (var i = 0; i < Qt_Caracteres; i++) senha.Append(caracteresValidos[random.Next(0, valormaximo)]); return senha.ToString(); } } |
How to create a password generator written in Transact-SQL (TSQL)
Source code
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 |
CREATE FUNCTION dbo.fncGera_Senha( @Qt_Caracteres INT, @Fl_Letras BIT, @Fl_Maiusculas BIT, @Fl_Numeros BIT, @Fl_Simbolos BIT ) RETURNS VARCHAR(MAX) AS BEGIN IF (@Qt_Caracteres = 0) RETURN NULL IF (@Fl_Letras = 0 AND @Fl_Numeros = 0 AND @Fl_Simbolos = 0) RETURN NULL DECLARE @caracteresValidos VARCHAR(MAX) = '' IF (@Fl_Letras = 1) SET @caracteresValidos += 'abcdefghijklmnopqrstuvwxyz' IF (@Fl_Letras = 1 AND @Fl_Maiusculas = 1) SET @caracteresValidos += 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' IF (@Fl_Numeros = 1) SET @caracteresValidos += '1234567890' IF (@Fl_Simbolos = 1) DECLARE @valormaximo INT = LEN(@caracteresValidos), @i INT = 1, @Senha VARCHAR(MAX) = '' WHILE(@i <= @Qt_Caracteres) BEGIN -- Não é permitido utilizar a função RAND() dentro de UDF. Ou se cria uma view com a função RAND() ou utiliza a solução abaixo SET @Senha += SUBSTRING(@caracteresValidos, CAST(((ABS(CHECKSUM(PWDENCRYPT(N''))) / 2147483647.0) * @valormaximo) + 1 AS INT), 1) SET @i += 1 END RETURN @senha END |
As you may have noticed in the comment of the function, it is not possible to use the RAND () function within UDF functions. If you try to do so, SQL Server will return this error message:
Msg 443, Level 16, State 1, Procedure fncGera_Senha, Line 50
Invalid use of a side-effecting operator 'rand' within a function.
To get around this, see more by accessing the post. SQL Server - Msg 443 Invalid use of a side-effecting operator 'rand' within a function.
That's it folks!
I hope you enjoyed the post and see you next time.
php c # csharp random strings passwords generator
php c # csharp random strings passwords generator