Generate Random Password 10++ languages

This article has been written before more than 24months, information might old.

M-am gandit sa scriu o functie de generare password in cate limbaje de programare / scripting pot Pana acum l-am scris in : c/c++ , VB , C# , Boo , Autoit , Ahl , Pawn , javascript , thin basic , visual basic script , WinBatch , Php , Python , Ruby , si o sa il mai scriu si in altele .
Acum codul :

C++ :

#include <iostream>
#include <string>
#include <time.h>
using namespace std;

string GenPass(int Size, bool Numbers , bool UpperLetters )  {
if(Size > 50) Size = 50;
srand ( (unsigned) time(NULL) );
string s= "";
if(Numbers){
if(UpperLetters){
for(int i=0;i<Size;i++){
	switch(rand() % 3 + 1){
	case 1: s.append(1,(char)(rand()%(57-48+1))+48);break; // Numbers
	case 2: s.append(1,(char)(rand()%(122-97+1))+97);break; // Lower Letters
	case 3: s.append(1,(char)(rand()%(90-65+1))+65);break; // Uper Letters
	default: s.append(1,(char)0);break;
}}}else{
for(int i=0;i<Size;i++){
	switch(rand() % 2 + 1){
	case 1: s.append(1,(char)(rand()%(57-48+1))+48);break; // Numbers
	case 2: s.append(1,(char)(rand()%(122-97+1))+97);break; // Lower Letters
	default: s.append(1,(char)0);break;
}}
}}else{
if(UpperLetters){
for(int i=0;i<Size;i++){
	switch(rand() % 2 + 1){
	case 1: s.append(1,(char)(rand()%(122-97+1))+97);break; // Lower Letters
	case 2: s.append(1,(char)(rand()%(90-65+1))+65);break; // Uper Letters
	default: s.append(1,(char)0);break;
}}}else{for(int i=0;i<Size;i++) s.append(1,(char)(rand()%(122-97+1))+97);// Lower Letters
}}
return s;}

int main ()
{

	cout << GenPass(10,1,1);
	cout << endl;
	system("PAUSE");
	return(0);
}

VB.NET :

Imports System.Text

Module Module1
    Function GenPass(Optional ByVal Str As Integer = 15, Optional ByVal Number As Boolean = True, Optional ByVal UpperChar As Boolean = True) As String
        Randomize()
        If Str > 50 Then Str = 50
        Dim SB As New StringBuilder()
        Dim rnd As New Random()
        Dim rnf As Integer = 0
        Dim c As Char
        Dim i As Integer
        For i = 0 To Str Step +1
            If (UpperChar And Number) Then
                rnf = Convert.ToInt32(2 * rnd.NextDouble() + 0)
                If (rnf = 0) Then c = Convert.ToChar(Convert.ToInt32(26 * rnd.NextDouble() + 65)).ToString().ToLower()
                If (rnf = 1) Then c = Convert.ToChar(Convert.ToInt32(26 * rnd.NextDouble() + 65))
                If (rnf = 2) Then c = Convert.ToChar(Convert.ToInt32(9 * rnd.NextDouble() + 48))
            ElseIf (UpperChar And Not Number) Then
                rnf = Convert.ToInt32(1 * rnd.NextDouble() + 0)
                If (rnf = 0) Then c = Convert.ToChar(Convert.ToInt32(26 * rnd.NextDouble() + 65)).ToString().ToLower()
                If (rnf = 1) Then c = Convert.ToChar(Convert.ToInt32(26 * rnd.NextDouble() + 65))
            ElseIf (Not UpperChar And Number) Then
                rnf = Convert.ToInt32(1 * rnd.NextDouble() + 0)
                If (rnf = 0) Then c = Convert.ToChar(Convert.ToInt32(26 * rnd.NextDouble() + 65)).ToString().ToLower()
                If (rnf = 1) Then c = Convert.ToChar(Convert.ToInt32(9 * rnd.NextDouble() + 48))
            Else
                c = Convert.ToChar(Convert.ToInt32(26 * rnd.NextDouble() + 65)).ToString().ToLower()
            End If
            SB.Append(c)
        Next
        Return SB.ToString()
    End Function

    Sub Main()

        Console.WriteLine(GenPass(44, 1, 1))
        Console.ReadKey()
    End Sub
End Module

c# :

using System;
using System.Text;
using System.Threading;
namespace genPass
{
	class MainClass
	{
		public static void Main (string[] args)
		{
	       Console.WriteLine(genPass(20,true,true));
	    Console.ReadLine();
		}
		public static String genPass(int str,bool numbers, bool upperLetter){
		char[] charArray= new char[62];
		int rate = 0;
		if(numbers) {
			rate += 9;
			for(int i=1;i=9;i++){
			charArray[i-1] = (char)(48 + i);
			}
		}if(upperLetter) {
				for(int i=0;i=25;i++){
			charArray[rate+i] = (char)(97 + i);
			} rate += 26;
				for(int i=0;i=25;i++){
			charArray[i+rate] =(char)(65 + i);
			}	
	}	else{
				for(int i=0;i=25;i++){
		charArray[i+rate] =(char)(97 + i);
			}
		}
		if(str  50){ str = 50;}
        StringBuilder strArr = new StringBuilder( ,str+1);
		int countChr = charArray.Length;
		for(int i=0;i=str;i++){
	    Random rand = new Random(DateTime.Now.Millisecond);
		Thread.Sleep(15);
		strArr.AppendFormat({0}, charArray[rand.Next(0,countChr)]);
		}
		charArray = null;
		return strArr.ToString();
		}
	}
}

BOO :

namespace sd

import System
import System.Text
import System.Threading;

def GenPass(stren as int ,numb as bool ,upperletter as bool):
	if stren > 50 : stren = 50
	elCount as int = 0
	cArr as Array
	doArray = def(arr as Array, size as int):
		if arr == null :
			return Array.CreateInstance(char , size)
		else:
		 temp = Array.CreateInstance(char , (arr.Length+size))
		 arr.CopyTo(temp,0)
		 return temp		 		
	if (numb):
		cArr = doArray(cArr,10+elCount)
		for i in range(0, 9):
			cArr.SetValue(cast(char,(i+48)),elCount)
			elCount += 1
	if (upperletter):
		cArr = doArray(cArr,25)
		for i in range(0, 25):
			cArr.SetValue(cast(char,(i+65)),elCount)
			elCount += 1
	cArr = doArray(cArr,25)
	for i in range(0, 25):
		cArr.SetValue(cast(char,(i+97)),elCount)
		elCount += 1
	randC as Random 
	tempS as StringBuilder = StringBuilder( "" ,stren+1)
	for i in range(0,stren):
		randC = Random(date.Now.Millisecond)
		Thread.Sleep(15);
		tempS.AppendFormat( "{0}", cArr[randC.Next(cArr.Length)] );
	return tempS.ToString();
	
	
print GenPass(10,true,true)
print "Press any key to continue . . . "
Console.ReadKey(true)

Autoit :

func GenPass($str=16,$numbers=1,$caseSensitive=1,$specialChr=1)
Dim $chrArray[1]
Local $temp="",$befArray=0
if Number($numbers) Then
$befArray = UBound($chrArray)
ReDim $chrArray[$befArray+9]
For $i=0 to 9 Step +1
$chrArray[$befArray-1+$i] = String(Chr(48+$i))
Next
EndIf
If Number($caseSensitive) Then
$befArray = UBound($chrArray)
ReDim $chrArray[$befArray+50]
For $i=0 to $i+25 Step +1
$chrArray[$befArray-1+$i] = String(Chr(97+$i))
Next
For $i=0 to $i+25 Step +1
$chrArray[$befArray-1+$i+25] = String(Chr(65+$i))
Next
Else
$befArray = UBound($chrArray)
ReDim $chrArray[$befArray+25]
For $i=0 to $i+25 Step +1
$chrArray[$befArray-1+$i] = String(Chr(97+$i))
Next
EndIf
If Number($specialChr) Then
EndIf
Local $arrMax = UBound($chrArray)-1
For $i=0 to $str Step +1
$temp = $temp&amp;String($chrArray[Random(0,$arrMax)])
Next
Return $temp
EndFunc

Ahl :

GenPass(str=15,numbers=1,caseSensitive=1,specialChars=1)
{
nac = 0 
If numbers = 1 
{ 
while ( nac < 10 ) 
{
charArr%nac% = % nac 
nac += 1
}
}
if caseSensitive = 1 
{
 ChrAsci1 = 97
 ChrAsci2 = 65
Loop , 25 {
charArr%nac% = % Chr(ChrAsci1)
ChrAsci1 += 1
nac += 1
}
Loop , 25 {
charArr%nac% = % Chr(ChrAsci2)
ChrAsci2 += 1
nac += 1
}
}else{
 ChrAsci1 = 97
Loop , 25 {
charArr%nac% = % Chr(ChrAsci1)
ChrAsci1 += 1
nac += 1
}
}
If specialChars = 1 
{
	
}
 Wcount = 0
 Rout = 0
 Rstr := ""
while ( Wcount < str ){
Random Rout , 0 , nac
Rstr = % Rstr . charArr%Rout%
Wcount += 1
}
Return Rstr
}

Pawn :

#include <string>

main(){

genPass(15,1,1)

return 1

}

genPass(str=15,numbers=1,upperLetters=1){
if (str > 49) str = 49;
new charNumArray[10] = { '0','1','2', '3','4','5','6','7','9' }
new charchrArray[26] = { 'a','b','c','d','e','f','g','h','i','j','k','l','m', 'n','o','p','q','r','s','t','u', 'v','w','x','y','z' }
new charChrArray[26] = { 'A','B','C','D','E','F','G','H','I','J', 'K','L','M','N','O','P','Q', 'R','S','T','U','V','W','X','Y','Z' }
new charChrAllArray[62] = { 0 }
new charRez[50] = { 0 }

if(numbers)
{
strcopy(charChrAllArray,charNumArray,sizeof(charChrAllArray))
}
if(upperLetters)
{
strcopy(charChrAllArray,charchrArray,sizeof(charChrAllArray))
strcopy(charChrAllArray,charChrArray,sizeof(charChrAllArray))
}else{
strcopy(charChrAllArray,charchrArray,sizeof(charChrAllArray))
}

for(new i=0;i<str;i++){
charRez[i] = charChrAllArray[random(62)]
}
return charRez
}

Javascript :

function GenPass(str,numbers,caseSensitive){
var chrArray = new Array(0);
var temp = "";
if(numbers){
for(i=1;i<10;i++)chrArray[chrArray.length]=i-1;
}if(caseSensitive){
for(i=1;i<=26;i++)chrArray[chrArray.length]=String.fromCharCode(96+i);
for(i=1;i<=26;i++) chrArray[chrArray.length]=String.fromCharCode(64+i);
}else{for(i=1;i<26;i++)chrArray[chrArray.length]=String.fromCharCode(96+i);}
for(i=0;i<str;i++)
temp = temp.concat(chrArray[Math.floor(Math.random()*(chrArray.length-1+1))+1]);
return temp ;
}
//alert();

document.write(GenPass(16,1,1));

Thin Basic

Function getPass(str As Number=15, numbers As Number=1, upperletter As Number=1) As String

Dim charArr(1) As String Value 0
  If numbers <> 0 Then 
    ReDim Preserve charArr(11) 
    Local i As Integer Value 0  
    For i=1 To 11 Step +1
     charArr(i) = Chr$(i+47) 
    Next
  End If
 If upperletter <> 0 Then 
 Local arrDim As Integer Value UBound(charArr)
 ReDim Preserve charArr(50+arrDim)
 For i = 0 To 25 Step +1
   charArr(arrDim+i) = Chr$(97+i)
 Next
 For i = 0 To 25 Step +1 
   charArr(25+arrDim+i) = Chr$(65+i)
 Next
 Else 
  Local arrDim As Integer Value UBound(charArr)
  ReDim Preserve charArr(26+arrDim)
  Local i As Integer Value 0
 For i = 1 To 26 Step +1
   charArr(arrDim+i) = Chr$(97+i-1)
 Next
 End If
 If(str > 50) Then str = 50
 Local rez As String Value "" 
 Local randNum As Integer Value 0
 Randomize Timer
 For i=0 To str Step +1 
 randNum = Rnd(1,UBound(charArr))
 rez &amp;= charArr(randNum)
 Next
  Function = rez
End Function

VBS

function GenPass(str,numbers,caseSensitive,specialChr)
if IsEmpty(str) then
str = 16 
End If
if IsEmpty(numbers) then
numbers = 1
End If
if IsEmpty(caseSensitive) then
caseSensitive = 1 
End If
if IsEmpty(specialChr) then
specialChr = 1 
End If
Dim chrArray
chrArray=Array(0)
Dim temp,befArray
temp=""
befArray=0
if Int(numbers) Then
befArray = UBound(chrArray)
ReDim chrArray(befArray+9)
For i=0 to 9 Step +1
chrArray(befArray+i) = CStr(Chr(48+i))
Next
End If
If Int(caseSensitive) Then
befArray = UBound(chrArray)
ReDim chrArray(befArray+50)
For i=0 to 25 Step +1
chrArray(befArray+i) = CStr(Chr(97+i))
Next
For i=0 to 25 Step +1
chrArray(befArray+25+i) = CStr(Chr(65+i))
Next
Else
befArray = UBound(chrArray)
ReDim chrArray(befArray+25)
For i=0 to 25 Step +1
chrArray(befArray+i) = CStr(Chr(97+i))
Next
End If
If Int(specialChr) Then
End If
Dim arrMax
arrMax = UBound(chrArray)
For i=0 to str Step +1
Randomize
temp = temp&amp;CStr(chrArray(Int((arrMax-0+1)*Rnd+0)))
Next
GenPass = temp
End Function 

WinBatch

#DefineFunction GenPass(Str,Numbers,UpperLetter)
if Str > 50  then Str = 50
CharArr = ""
if Numbers then
For i = 0 To 9 By +1
CharArr = CharArr : Num2Char(48+i) 
Next
endif
if UpperLetter then 
For i = 0 To 25 By +1
CharArr = CharArr : Num2Char(65+i) 
Next
endif
For i = 0 To 25 By +1
CharArr = CharArr : Num2Char(97+i) 
Next
CharArr = StrTrim(CharArr)
CharArr = ArrayFromStr(CharArr)
temp = ""
For i = 0 To Str By +1
temp = temp : CharArr[Random(ArrInfo(CharArr,1)-1)]
next
return StrTrim(temp)
#EndFunction

Php

<?php

function genPass($str=15,$numbers=1,$upperLetter=1){
if($str > 50) $str = 50; $charArray = array(0);
if($numbers):
for($i=0;$i<=9;$i++)$charArray[]=chr($i+48);
endif;if($upperLetter):
for($i=1; $i<=26;$i++) $charArray[] = chr($i+96);
for($i=1; $i<=26;$i++) $charArray[] = chr($i+64);
else:
for($i=1; $i<=26;$i++) $charArray[] = chr($i+96);
endif;$rezStr = "";
for($i=0;$i<=$str;$i++) $rezStr .= $charArray[rand(0,count($charArray)-1)];
return $rezStr;}

?>

Python

#!/usr/bin/env python
import random

def main():
    print(GenPass(50,1,1));

def GenPass( Str=15 , Number=1 , UpperLetters=1 ):
    if(Str > 50): Str = 50;
    CharArr = "";
    if(Number):
        i = 48;
        while(i <= 58 ):
            CharArr = CharArr + (str(chr(i)));
            i+=1;
    if(UpperLetters):
        i = 65;
        while(i <= 90 ):
            CharArr = CharArr + (str(chr(i)));
            i+=1;
    i = 97;
    while(i <= 122 ):
        CharArr = CharArr + (str(chr(i)));
        i+=1;
    i = 0;Temp = "";
    while(i <= Str ):
       i+=1;
       Temp = Temp + random.choice(CharArr);
    return Temp;


if __name__ == '__main__':
    main()

Ruby

def GenPass(stren,numb,upperletter)
    charArr = Array.new
      if stren>50 
        stren = 50;
      end
      if numb == true
        (0..9).each do |i|
          charArr.insert(-1,(48+i).chr) 
        end
      end
      if upperletter == true
        (0..25).each do |i|
          charArr.insert(-1,(65+i).chr) 
        end
      end
      (0..25).each do |i|
        charArr.insert(-1,(97+i).chr) 
      end
    temp = ""
    (0..stren).each do |i|
      temp = temp + charArr[rand(charArr.size)]   
    end
    return temp
  end
Share the joy

One Response

  1. Psychedelic Host noiembrie 14, 2010

Leave a Reply