using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.ComponentModel;
namespace CryptoUtils {
///
/// Cryptography helper class
///
public class Cryptography {
///
/// Hash algorithm enum
///
public enum HashType : short {
[DescriptionAttribute( "SHA1CryptoServiceProvider" )]
SHA1 = 0,
[DescriptionAttribute( "SHA256Managed" )]
SHA256 = 1,
[DescriptionAttribute( "SHA384Managed" )]
SHA384 = 2,
[DescriptionAttribute( "SHA512Managed" )]
SHA512 = 3,
[DescriptionAttribute( "MD5CryptoServiceProvider" )]
MD5 = 4
}
///
/// Creates a random salt to add to a password.
///
///
public static string CreateSalt( ) {
RandomNumberGenerator rng = RandomNumberGenerator.Create( );
byte[ ] number = new byte[ 32 ];
rng.GetBytes( number );
return Convert.ToBase64String( number );
}
///
/// Creates the password hash using a given HashType algoritm.
///
/// The salt.
/// The password.
/// Type of the hash.
///
public static string CreatePasswordHash( string salt, string password, HashType hashType ) {
string hashString = string.Empty;
if ( !string.IsNullOrEmpty( password ) ) {
HashAlgorithm hashAlg = HashAlgorithm.Create( hashType.ToString( ) );
byte[ ] pwordData = Encoding.Default.GetBytes( salt + password );
byte[ ] hash = hashAlg.ComputeHash( pwordData );
hashString = Convert.ToBase64String( hash );
}
return hashString;
}
///
/// Creates the password hash using SHA256 managed hash algoritm.
///
/// The password salt.
/// The password.
///
public static string CreatePasswordHash( string salt, string password ) {
return CreatePasswordHash( salt, password, HashType.SHA256 );
}
///
/// Encrypts the password.
///
/// The password.
/// The salt.
///
public static string EncryptPassword( string password, out string salt ) {
salt = CreateSalt( );
return CreatePasswordHash( salt, password );
}
}
}