Created
October 15, 2013 20:07
-
-
Save vicentedealencar/6997817 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
using System; | |
using System.Diagnostics.CodeAnalysis; | |
using System.Security.Cryptography; | |
using System.Text; | |
using Raven.Client.UniqueConstraints; | |
namespace Miyagi.Core.Models | |
{ | |
[ExcludeFromCodeCoverage] | |
public abstract class BaseUser | |
{ | |
public string Id { get; set; } | |
[UniqueConstraint] | |
public string Email { get; set; } | |
const string ConstantSalt = "z125sahl32c8@"; | |
protected string HashedPassword { get; private set; } | |
private string passwordSalt; | |
private string PasswordSalt | |
{ | |
get | |
{ | |
return passwordSalt ?? (passwordSalt = Guid.NewGuid().ToString("N")); | |
} | |
set { passwordSalt = value; } | |
} | |
public BaseUser SetPassword(string pwd) | |
{ | |
HashedPassword = GetHashedPassword(pwd); | |
return this; | |
} | |
private string GetHashedPassword(string pwd) | |
{ | |
using (var sha = SHA256.Create()) | |
{ | |
var computedHash = sha.ComputeHash(Encoding.Unicode.GetBytes(PasswordSalt + pwd + ConstantSalt)); | |
return Convert.ToBase64String(computedHash); | |
} | |
} | |
public bool ValidatePassword(string maybePwd) | |
{ | |
if (HashedPassword == null) | |
return true; | |
return HashedPassword == GetHashedPassword(maybePwd); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment