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.
//-------------------------------------------------------------------
///
/// Non-reversible encryption.
/// 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.
/// Algorythm must be dispoed by calling routine.
///
public static string HashString (this string value, HashAlgorithm algorythm)
{
return Convert.ToBase64String(algorythm.ComputeHash(System.Text.Encoding.UTF8.GetBytes(value)));
}
//-------------------------------------------------------------------
///
/// Non-reversible encryption using Sha256.
/// 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.
///
public static string HashSha256 (this string value)
{
using (HashAlgorithm algorythm = SHA256.Create())
{
return HashString(value, algorythm);
}
}
//-------------------------------------------------------------------
///
/// Non-reversible encryption using Sha384.
/// 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.
///
public static string HashSha384 (this string value)
{
using (HashAlgorithm algorythm = SHA384.Create())
{
return HashString(value, algorythm);
}
}
//-------------------------------------------------------------------
///
/// Non-reversible encryption using Sha512.
/// 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.
///
public static string HashSha512 (this string value)
{
using (HashAlgorithm algorythm = SHA512.Create())
{
return HashString(value, algorythm);
}
}
//-------------------------------------------------------------------
}
//=======================================================================
}