Commits

Craig Phillips committed 2d6e063

Added RandomString.cs which generates random strings of specified length and also generates Hashes of strings (either from a specified string or a random string). Useful when you just need some random strings!

  • Participants
  • Parent commits a679214

Comments (0)

Files changed (1)

File RandomString.cs

+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+
+namespace MiscellaneousClasses
+{
+	public class RandomString
+	{
+		private readonly Random random = new Random();
+		private const string alphanumeric_selection = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
+		private const string symbol_selection = "!£$%^&*()-+=";
+
+		public string GenerateRandomString()
+		{
+			var length = random.Next(5, 15);
+			return GenerateRandomString(length);
+		}
+
+		public string GenerateRandomString(int length, bool onlyUseAlphaNumerics = false)
+		{
+			var characterSelection = onlyUseAlphaNumerics ? alphanumeric_selection : alphanumeric_selection + symbol_selection;
+			return GenerateRandomString(length, characterSelection);
+		}
+
+		public string GenerateRandomString(int length, string characterSelection)
+		{
+			var result = new StringBuilder();
+			for (var i = 0; i < length; i++)
+			{
+				var chr = characterSelection.Substring(random.Next(0, characterSelection.Length), 1);
+				result.Append(chr);
+			}
+			return result.ToString();
+		}
+
+		public string GenerateHashString(string stringToHash)
+		{
+			return BitConverter.ToString(new System.Security.Cryptography.SHA1CryptoServiceProvider().ComputeHash(Encoding.Unicode.GetBytes(stringToHash))).Replace("-", string.Empty);
+		}
+
+		public string GenerateRandomHashString()
+		{
+			return GenerateHashString(GenerateRandomString(1024));
+		}
+	}
+}