Skip to content

Instantly share code, notes, and snippets.

@Grinderofl
Created October 27, 2011 09:12
Show Gist options
  • Save Grinderofl/1319120 to your computer and use it in GitHub Desktop.
Save Grinderofl/1319120 to your computer and use it in GitHub Desktop.
C# .NET: Security functions
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="Security.cs" company="">
//
// </copyright>
// <summary>
// Class to manage security functions, such as Base64 encode/decode, SHA encryption, etc.
// Requires the Random String Generator class.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
namespace Domain.Library
{
using System;
using System.Security.Cryptography;
using System.Text;
/// <summary>
/// The Security class itself
/// </summary>
public static class Security
{
#region Public Methods
/// <summary>
/// Decode Base64 string to normal
/// </summary>
/// <param name="toDecode">
/// String to decode
/// </param>
/// <returns>
/// Decoded string
/// </returns>
public static string Base64Decode(string toDecode)
{
byte[] encodedDataAsBytes = Convert.FromBase64String(toDecode);
return Encoding.ASCII.GetString(encodedDataAsBytes);
}
/// <summary>
/// Encode to Base64 string
/// </summary>
/// <param name="toEncode">
/// String to encode
/// </param>
/// <returns>
/// Encoded string
/// </returns>
public static string Base64Encode(string toEncode)
{
byte[] toEncodeAsBytes = Encoding.ASCII.GetBytes(toEncode);
return Convert.ToBase64String(toEncodeAsBytes);
}
/// <summary>
/// Use the Random String Generator to create a password
/// </summary>
/// <returns>
/// The generate password.
/// </returns>
public static string GeneratePassword()
{
return RandomStringGenerator.Generate(10);
}
/// <summary>
/// Generates salt based on timestamp.
/// </summary>
/// <returns>
/// Base64 encoded salt
/// </returns>
public static string GenerateSalt()
{
return Base64Encode(Sha512Hash(DateTime.Now.ToString()));
}
/// <summary>
/// Generate SHA512 hashed string by provided salt
/// </summary>
/// <param name="password">
/// Password to be hashed
/// </param>
/// <param name="salt">
/// Salt to be added to password
/// </param>
/// <returns>
/// SHA512 hashed password
/// </returns>
public static string HashPassword(string password, string salt)
{
return Sha512Hash(salt + password);
}
/// <summary>
/// Generate a SHA512 hashed string
/// </summary>
/// <param name="toHash">
/// String to hash
/// </param>
/// <returns>
/// SHA512 hashed string
/// </returns>
public static string Sha512Hash(string toHash)
{
var encoding = new UTF8Encoding();
byte[] data = encoding.GetBytes(toHash);
var cryptoServiceProvider = new SHA512CryptoServiceProvider();
return BitConverter.ToString(cryptoServiceProvider.ComputeHash(data));
}
#endregion
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment