public bool LoginToAccount(string username, string password) { if (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(password)) { return(false); } AccountStore store = AccountStore.Create(); Account account = GetAccountFromStore(AccountStore.Create(), username); if (account == null) { return(false); } byte[] salt, hashedPassword; // Upgrade existing passwords to our new format. if (!account.Properties.ContainsKey(saltKey)) { salt = CryptoUtilities.Get256BitSalt(); hashedPassword = CryptoUtilities.GetKeyDerivation(CryptoUtilities.StringToByteArray(account.Properties[pwKey]), salt); account.Properties[pwKey] = Convert.ToBase64String(hashedPassword); account.Properties.Add(saltKey, Convert.ToBase64String(salt)); store.Save(account, serviceID); } salt = Convert.FromBase64String(account.Properties[saltKey]); hashedPassword = CryptoUtilities.GetKeyDerivation(CryptoUtilities.StringToByteArray(password), salt); return(account.Properties[pwKey] == Convert.ToBase64String(hashedPassword)); }
public bool CreateAndSaveAccount(string username, string password) { if (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(password)) { return(false); } byte[] salt = CryptoUtilities.Get256BitSalt(); byte[] hashedPassword = CryptoUtilities.GetKeyDerivation(CryptoUtilities.StringToByteArray(password), salt); AccountStore store = AccountStore.Create(); if (GetAccountFromStore(store, username) != null) { return(false); } Account account = new Account(username); account.Properties.Add(pwKey, Convert.ToBase64String(hashedPassword)); account.Properties.Add(saltKey, Convert.ToBase64String(salt)); store.Save(account, serviceID); return(true); }