< Summary

Information
Class: Elsa.Workflows.Hasher
Assembly: Elsa.Workflows.Core
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Workflows.Core/Services/Hasher.cs
Line coverage
100%
Covered lines: 20
Uncovered lines: 0
Coverable lines: 20
Total lines: 53
Line coverage: 100%
Branch coverage
100%
Covered branches: 4
Total branches: 4
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
Hash(...)100%11100%
Hash(...)100%11100%
Serialize(...)100%44100%

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Workflows.Core/Services/Hasher.cs

#LineLine coverage
 1using System.Security.Cryptography;
 2using System.Text;
 3using System.Text.Json;
 4using System.Text.Json.Serialization;
 5using Elsa.Expressions.Contracts;
 6using Elsa.Workflows.Serialization.Converters;
 7
 8namespace Elsa.Workflows;
 9
 10/// <inheritdoc />
 11public class Hasher : IHasher
 12{
 13    private readonly JsonSerializerOptions _serializerOptions;
 14
 11415    public Hasher(IWellKnownTypeRegistry wellKnownTypeRegistry)
 16    {
 11417        _serializerOptions = new JsonSerializerOptions
 11418        {
 11419            IncludeFields = true,
 11420            PropertyNameCaseInsensitive = true,
 11421            DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull
 11422        };
 23
 11424        _serializerOptions.Converters.Add(new TypeJsonConverter(wellKnownTypeRegistry));
 11425        _serializerOptions.Converters.Add(new ExcludeFromHashConverterFactory());
 11426    }
 27
 28    /// <inheritdoc />
 29    public string Hash(string value)
 30    {
 431231        var data = SHA256.HashData(Encoding.UTF8.GetBytes(value));
 431232        return Convert.ToHexString(data);
 33    }
 34
 35    /// <inheritdoc />
 36    public string Hash(params object?[] values)
 37    {
 935738        var strings = values.Select(Serialize).Where(x => !string.IsNullOrWhiteSpace(x)).ToList();
 431239        var input = string.Join("|", strings);
 431240        return Hash(input);
 41    }
 42
 43    private string Serialize(object? payload)
 44    {
 504545        if(payload == null)
 36246            return string.Empty;
 47
 468348        if(payload is string s)
 36949            return s;
 50
 431451        return JsonSerializer.Serialize(payload, payload.GetType(), _serializerOptions);
 52    }
 53}