Files
Common-VS2019/SacsStandardBase/Base/Data/SacsShaHasher.cs
T
2021-05-05 15:36:28 +01:00

80 lines
3.3 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System;
using System.Collections.Generic;
using System.Security.Cryptography;
using System.Text;
namespace Sarand.Net.Standard.Base.Data
{
//=======================================================================
public static class SacsShaHasher
{
//-------------------------------------------------------------------
// Properties.
public static String ShaSaltDelim { get; } = ":";
public static String Sha256Prefix { get; } = "Sha256:";
public static String Sha384Prefix { get; } = "Sha384:";
public static String Sha512Prefix { get; } = "Sha512:";
//-------------------------------------------------------------------
// Ref. https://xamarinhelp.com/cryptography-in-xamarin-forms/
// Hash Salt.
// To add a salt to the hash, append the Salt (random string) to the value.
// The Salt can be stored with the hashed value.
// Salts do not need to be kept secret and should be different for each hash.
// This is to reduce the impact of pre-built hash tables, eg. where common passwords (‘password’) generate the same Hash.
// It also stops the same password generating the identical Hash for 2 different accounts.
//-------------------------------------------------------------------
/// <summary>
/// Non-reversible encryption.
/// <para>Result is converted to Base64 string to ensure that all bytes are properly recorded, as UTF8 doesn’t have an exact byte match for each possible byte sequence.</para>
/// <para>Algorythm must be dispoed by calling routine.</para>
/// </summary>
public static string HashString (this string value, HashAlgorithm algorythm)
{
return Convert.ToBase64String(algorythm.ComputeHash(System.Text.Encoding.UTF8.GetBytes(value)));
}
//-------------------------------------------------------------------
/// <summary>
/// Non-reversible encryption using Sha256.
/// <para>Result is converted to Base64 string to ensure that all bytes are properly recorded, as UTF8 doesn’t have an exact byte match for each possible byte sequence.</para>
/// </summary>
public static string HashSha256 (this string value)
{
using (HashAlgorithm algorythm = SHA256.Create())
{
return HashString(value, algorythm);
}
}
//-------------------------------------------------------------------
/// <summary>
/// Non-reversible encryption using Sha384.
/// <para>Result is converted to Base64 string to ensure that all bytes are properly recorded, as UTF8 doesn’t have an exact byte match for each possible byte sequence.</para>
/// </summary>
public static string HashSha384 (this string value)
{
using (HashAlgorithm algorythm = SHA384.Create())
{
return HashString(value, algorythm);
}
}
//-------------------------------------------------------------------
/// <summary>
/// Non-reversible encryption using Sha512.
/// <para>Result is converted to Base64 string to ensure that all bytes are properly recorded, as UTF8 doesn’t have an exact byte match for each possible byte sequence.</para>
/// </summary>
public static string HashSha512 (this string value)
{
using (HashAlgorithm algorythm = SHA512.Create())
{
return HashString(value, algorythm);
}
}
//-------------------------------------------------------------------
}
//=======================================================================
}