Hey guys,
Good morning!

In this quick post, I will demonstrate to you how to create a small random password generator, to be used in the most diverse ways. I will make this script available using the languages ​​C# (to be used in SQL Server, with the CLR), PHP and Transact-SQL.

These scripts are very simple, but the idea is to demonstrate how to use this feature in the 3 main technologies that I use.

How to create a password generator written in PHP

php-random-password-generator-with-php
php-random-password-generator-with-php

Source code:

<?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 )
    {
        $caracteresValidos .= "!@#$%&*()_+-={}/\<>?§£¢¬|.,;:";
    }


    $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)

sql-server-random-password-generator-with-clr-csharp-c
sql-server-random-password-generator-with-clr-csharp-c

Source code:

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)
            caracteresValidos += @"!@#$%&*()_+-={}/\<>?§£¢¬|.,;:?";


        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)

sql-server-random-password-generator-with-transact-sql-tsql
sql-server-random-password-generator-with-transact-sql-tsql

Source code

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)
        SET @caracteresValidos += '"!@#$%&*()_+-={}/\<>?§£¢¬|.,;:?'

    
    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 function comment, 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_Password, 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