Created
June 15, 2010 11:58
-
-
Save lukesmith/439016 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[Serializable] | |
public class EmailAddress : IComparable<EmailAddress> | |
{ | |
public const int MaxLength = 256; | |
// Regex based on Phil Haacks http://haacked.com/archive/2007/08/21/i-knew-how-to-validate-an-email-address-until-i.aspx | |
public const string Expression = @"[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+(?:[A-Z]{2}|com|org|net|gov|mil|biz|info|mobi|name|aero|jobs|museum|uk)\b"; | |
private readonly string value; | |
public EmailAddress(string value) | |
{ | |
if (!Regex.IsMatch(value, Expression)) | |
{ | |
throw new ArgumentException(Resources.Common.Arg_NotValidEmailAddress, "value"); | |
} | |
this.value = value; | |
} | |
public static bool IsValid(string value) | |
{ | |
return Regex.IsMatch(value, Expression); | |
} | |
public int CompareTo(EmailAddress other) | |
{ | |
return this.ToString().CompareTo(other.ToString()); | |
} | |
public override string ToString() | |
{ | |
return this.value; | |
} | |
public static bool TryParse(string value, out EmailAddress result) | |
{ | |
if (IsValid(value)) | |
{ | |
result = new EmailAddress(value); | |
return true; | |
} | |
result = null; | |
return false; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Uses SaltedHash from http://www.dijksterhuis.org/creating-salted-hash-values-in-c/ | |
[Serializable] | |
public struct Password : IComparable<Password> | |
{ | |
public Password(string password, string salt) : this() | |
{ | |
this.Value = password; | |
this.Salt = salt; | |
} | |
public string Value | |
{ | |
get; | |
private set; | |
} | |
public string Salt | |
{ | |
get; | |
private set; | |
} | |
public static Password CreateFromClearText(string password) | |
{ | |
string hashedPassword; | |
string salt; | |
var saltedHash = new SaltedHash(); | |
saltedHash.GetHashAndSaltString(password, out hashedPassword, out salt); | |
return new Password(hashedPassword, salt); | |
} | |
public bool IsSameAs(string password) | |
{ | |
var saltedHash = new SaltedHash(); | |
return saltedHash.VerifyHashString(password, this.Value, this.Salt); | |
} | |
public int CompareTo(Password other) | |
{ | |
if (this.Salt.CompareTo(other.Salt) == 0 && this.Value.CompareTo(other.Value) == 0) | |
{ | |
return 0; | |
} | |
return -1; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment