/* ---------------------------------------------------------------- *
*
* This file is part of the Xcoordination Application Space
* ("XcoAppSpace") http://www.xcoordination.com
*
* Copyright (C) 2009 Xcoordination http://www.xcoordination.com
*
* XcoAppSpace is free software; you can redistribute it and/or
* modify it under the terms of version 2 of the GNU General
* Public License as published by the Free Software Foundation.
*
* XcoAppSpace is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see http://www.gnu.org/licenses/
* or write to the Free Software Foundation, Inc., 51 Franklin
* Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* ---------------------------------------------------------------- */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
///
/// Utility class for encrypting passwords and comparing two encrypted passwords.
///
public static class PasswordUtil
{
///
/// Creates a hash for the given password string using SHA512 algorithm.
///
/// The password string to hash.
/// The hash value.
public static byte[] CreatePasswordHash(string password)
{
if (string.IsNullOrEmpty(password))
return null;
SHA1Managed hasher = new SHA1Managed();
byte[] hashed = hasher.ComputeHash(Encoding.ASCII.GetBytes(password));
hasher.Clear();
return hashed;
}
///
/// Compares two hashes, returns true if they are equal.
///
/// First hash.
/// Second hash.
/// true if the hashes are equal.
public static bool AreEqual(byte[] pw1, byte[] pw2)
{
if (pw1.Length != pw2.Length)
return false;
for (int i = 0; i < pw1.Length; i++)
{
if (pw1[i] != pw2[i])
return false;
}
return true;
}
}