Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Security.Cryptography;
- using System.ComponentModel;
- namespace CryptoUtils {
- public class Cryptography {
- public enum HashType : short {
- [DescriptionAttribute( "SHA1CryptoServiceProvider" )]
- SHA1 = 0,
- [DescriptionAttribute( "SHA256Managed" )]
- SHA256 = 1,
- [DescriptionAttribute( "SHA384Managed" )]
- SHA384 = 2,
- [DescriptionAttribute( "SHA512Managed" )]
- SHA512 = 3,
- [DescriptionAttribute( "MD5CryptoServiceProvider" )]
- MD5 = 4
- }
- public static string CreateSalt( ) {
- RandomNumberGenerator rng = RandomNumberGenerator.Create( );
- byte[ ] number = new byte[ 32 ];
- rng.GetBytes( number );
- return Convert.ToBase64String( number );
- }
- 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;
- }
- public static string CreatePasswordHash( string salt, string password ) {
- return CreatePasswordHash( salt, password, HashType.SHA256 );
- }
- public static string EncryptPassword( string password, out string salt ) {
- salt = CreateSalt( );
- return CreatePasswordHash( salt, password );
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment