Skip to content

Instantly share code, notes, and snippets.

View elizabeth-young's full-sized avatar

Elizabeth Fairbairn elizabeth-young

View GitHub Profile
@elizabeth-young
elizabeth-young / DateSuffixHelper.cs
Last active January 6, 2016 08:06
Full date string methods
public static class DateSuffixHelper
{
public static string FullDate(DateTime date)
{
return date.ToString("%d") + DaySuffix(date.Day) + date.ToString(" MMMM yyyy");
}
public static string FullDateIncDay(DateTime date)
{
return date.ToString("dddd d") + DaySuffix(date.Day) + date.ToString(" MMMM yyyy");
}
@elizabeth-young
elizabeth-young / WebDriverExtensions.cs
Last active August 8, 2021 01:51
Extensions to Selenium WebDriver
public static class WebDriverExtensions
{
public static void ClickElement(this IWebElement element, Drivers driverChoice)
{
switch (driverChoice)
{
case Drivers.InternetExplorer:
element.SendKeys("\n");
break;
default:
@elizabeth-young
elizabeth-young / CryptographyHelper.cs
Last active January 6, 2016 08:06
Cryptography methods
public class CryptographyHelper
{
public static string CalculateMD5Hash(string value)
{
var md5 = MD5.Create();
byte[] hash = md5.ComputeHash(Encoding.ASCII.GetBytes(value.Trim()));
var sb = new StringBuilder();
@elizabeth-young
elizabeth-young / PasswordHash.cs
Last active July 12, 2019 21:18
Password methods to hash and validate a password
public class PasswordHash
{
private const int SALT_BYTES = 24;
private const int HASH_BYTES = 24;
private const int PBKDF2_ITERATIONS = 1000;
private const int ITERATION_INDEX = 0;
private const int SALT_INDEX = 1;
private const int PBKDF2_INDEX = 2;
@elizabeth-young
elizabeth-young / LoggedError.cs
Last active January 6, 2016 08:07
Logged Error - serializes an exception into a list of strings
[Serializable]
public class LoggedError
{
public List<string> Errors { get; set; }
public string StackTrace { get; set; }
public LoggedError() { }
public LoggedError(Exception ex)
{
InitErrorList(ex);
@elizabeth-young
elizabeth-young / SerializationHelper.cs
Last active January 6, 2016 08:07
Serializes classes to xml and back
public enum SerializationType
{
Xml,
Binary
}
public static class SerializationHelper
{
/// <summary>
/// Serializes the target into a string of the type specified
@elizabeth-young
elizabeth-young / LocalisationHelper.cs
Last active January 6, 2016 08:05
Determines culture to be used
public class LocalisationHelper
{
public static List<string> Cultures
{
get { return new[] {"en-GB", "es"}.ToList(); }
}
public static string DefaultCulture
{
get { return Cultures[0]; }
}
@elizabeth-young
elizabeth-young / BaseController.cs
Last active January 6, 2016 08:05
Base MVC Controller using cultures - to be used with LocalisationHelper
public abstract partial class BaseController : Controller
{
private const string _langCookie = "lang";
protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
SetCurrentCulture();
base.OnActionExecuting(filterContext);
}
@elizabeth-young
elizabeth-young / CollectionMinMaxLengthValidationAttribute.cs
Last active January 6, 2016 08:05
Validation attribute for ensuring a collection has a minimum and maximum length
public class CollectionMinMaxLengthValidationAttribute : ValidationAttribute
{
const string errorMessage = "{0} must contain at least {1} item(s).";
const string errorMessageWithMax = "{0} must contain between {1} and {2} item(s).";
int minLength;
int? maxLength;
public CollectionMinMaxLengthValidationAttribute(int min)
{
minLength = min;
@elizabeth-young
elizabeth-young / RegularExpressionIfAttribute.cs
Last active January 6, 2016 08:05
Validation attribute for regular expression validating property if another property is set
public enum Comparison
{
IsEqualTo,
IsNotEqualTo
}
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
public sealed class RegularExpressionIfAttribute : ValidationAttribute, IClientValidatable
{
public string RegEx { get; private set; }