Click here to Skip to main content
15,888,733 members
Articles / Desktop Programming / WPF

Cryptographically Random Password / Data Set Creator using WPF, MVVM, and TPL

Rate me:
Please Sign up or sign in to vote.
4.33/5 (9 votes)
31 Jan 2013CPOL6 min read 25.2K   438   24  
Random data/password generator using WPF and MVVM to accomplish clean separation of GUI logic and business logic using MVVM pattern.
using System;
using System.Collections.ObjectModel;
using System.Windows.Input;
using RandPasswordGen.Helpers;
using RandPasswordGen.Models;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;


namespace RandPasswordGen.ViewModels
{
	public class MainWindowViewModel : BaseViewModel
	{
		private const int NUM_PASS_DEFAULT = 10, PASS_LEN_DEFAULT = 12;

		private RandPasswordModel _Model;
		public RandPasswordModel Model
		{
			get { return this._Model; }
			set
			{
				if (this._Model != value)
				{
					this._Model = value;
					RaisePropertyChanged(() => Model);
				}
			}
		}

		private StatsModel _StatsModel;
		public StatsModel StatsModel
		{
			get { return this._StatsModel; }
			set
			{
				if (this._StatsModel != value)
				{
					this._StatsModel = value;
					RaisePropertyChanged(() => StatsModel);
				}
			}
		}

		private string _AppStatus;
		public string AppStatus
		{
			get { return this._AppStatus; }
			set
			{
				if (this._AppStatus != value)
				{
					this._AppStatus = value;
					RaisePropertyChanged(() => AppStatus);
				}
			}
		}


		#region Commands

		public ICommand GeneratePassesCommand { get { return new DelegateCommand(GeneratePasswords); } }
		public ICommand SetDefaultsCommand { get { return new DelegateCommand(SetDefaults); } }

		#endregion

		#region Ctor
		public MainWindowViewModel()
		{
			AppStatus = "Idle";

			SetDefaults();
		}
		#endregion

		#region Command Handlers


		private void SetDefaults()
		{
			string chars = @"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*()";
			char[] allowableChars = chars.ToCharArray();
			Model = new RandPasswordModel(PASS_LEN_DEFAULT, allowableChars, NUM_PASS_DEFAULT);
			StatsModel = new StatsModel(Model.GeneratedPass);
		}

		private void GeneratePasswords()
		{
			AppStatus = "Generating passwords...";

			AsyncCallHelper.RunAync(() =>
			{
				List<string> passes = new List<string>(Model.NumPasses);
				Parallel.For(0, Model.NumPasses, i =>
				{
					int seed = RandomGenHelper.GetCryptographicallyRandomInt32();
					Random r = new Random(seed);
					string pass = RandomGenHelper.GeneratePassword(r, Model.Length, Model.AllowableChars);
					lock (passes)
					{
						passes.Add(pass);
					}
				} );

				Model.GeneratedPass = passes;
				StatsModel = new StatsModel(Model.GeneratedPass);

				AppStatus = string.Format("{0} passwords generated", Model.NumPasses);
			} );

		}


		#endregion

		
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United States United States
Sr software developer with experience in various languages and platforms, namely windows development using C#, C++, and VB, as well as various database platforms. At present, working in financial services industry in the United States.

Comments and Discussions