Introduction

The following piece of code will create alpha-numeric password. The code will generate a random MD5 hash and truncate the output.

Code:

using
System;
using System.Text;
using System.Security.Cryptography;

public string PasswordCreation(int Length)

{

Random random = new Random();

string password = HashData(random.Next().ToString()).Substring(0, 10);

string newPass = "";

// Uppercase at random

random = new Random();

for (int i = 0; i < password.Length; i++)

{

if (random.Next(0, 2) == 1)

newPass += password.Substring(i, 1).ToUpper();

else

newPass += password.Substring(i, 1);

}

return newPass;

}

private string HashData(string Data)

{

MD5 md5 = new MD5CryptoServiceProvider();

byte[] hash = md5.ComputeHash(Encoding.ASCII.GetBytes(Data));

StringBuilder stringBuilder = new StringBuilder();

foreach (byte b in hash)

{

stringBuilder.AppendFormat("{0:x2}", b);

}

return stringBuilder.ToString();

}