Webiant Logo Webiant Logo
  1. No results found.

    Try your search with a different keyword or use * as a wildcard.

HashHelper.cs

using System.Security.Cryptography;

namespace Nop.Core;

/// 
/// Hash helper class
/// 
public partial class HashHelper
{
    /// 
    /// Create a data hash
    /// 
    /// The data for calculating the hash
    /// Hash algorithm
    /// The number of bytes, which will be used in the hash algorithm; leave 0 to use all array
    /// Data hash
    public static string CreateHash(byte[] data, string hashAlgorithm, int trimByteCount = 0)
    {
        ArgumentException.ThrowIfNullOrEmpty(hashAlgorithm);
            
        var algorithm = (HashAlgorithm)CryptoConfig.CreateFromName(hashAlgorithm) ?? throw new ArgumentException("Unrecognized hash name");

        if (trimByteCount > 0 && data.Length > trimByteCount)
        {
            var newData = new byte[trimByteCount];
            Array.Copy(data, newData, trimByteCount);

            return BitConverter.ToString(algorithm.ComputeHash(newData)).Replace("-", string.Empty);
        }

        return BitConverter.ToString(algorithm.ComputeHash(data)).Replace("-", string.Empty);
    }
}