< Summary

Information
Class: Elsa.Identity.Services.DefaultSecretHasher
Assembly: Elsa.Identity
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Identity/Services/DefaultSecretHasher.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 16
Coverable lines: 16
Total lines: 52
Line coverage: 0%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
HashSecret(...)100%210%
HashSecret(...)100%210%
VerifySecret(...)100%210%
VerifySecret(...)100%210%
HashSecret(...)100%210%
GenerateSalt(...)100%210%

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Identity/Services/DefaultSecretHasher.cs

#LineLine coverage
 1using System.Security.Cryptography;
 2using System.Text;
 3using Elsa.Identity.Contracts;
 4using Elsa.Identity.Models;
 5
 6namespace Elsa.Identity.Services;
 7
 8/// <inheritdoc />
 9public class DefaultSecretHasher : ISecretHasher
 10{
 11    /// <inheritdoc />
 12    public HashedSecret HashSecret(string secret)
 13    {
 014        var saltBytes = GenerateSalt();
 015        return HashSecret(secret, saltBytes);
 16    }
 17
 18    /// <inheritdoc />
 19    public HashedSecret HashSecret(string secret, byte[] salt)
 20    {
 021        var passwordBytes = Encoding.UTF8.GetBytes(secret);
 022        var hashedPassword = HashSecret(passwordBytes, salt);
 023        return HashedSecret.FromBytes(hashedPassword, salt);
 24    }
 25
 26    /// <inheritdoc />
 27    public bool VerifySecret(string clearTextSecret, string secret, string salt)
 28    {
 029        var hashedPassword = HashedSecret.FromString(secret, salt);
 030        return VerifySecret(clearTextSecret, hashedPassword);
 31    }
 32
 33    /// <inheritdoc />
 34    public bool VerifySecret(string clearTextSecret, HashedSecret hashedSecret)
 35    {
 036        var password = hashedSecret.Secret;
 037        var salt = hashedSecret.Salt;
 038        var providedHashedPassword = HashSecret(clearTextSecret, salt);
 039        return providedHashedPassword.Secret.SequenceEqual(password);
 40    }
 41
 42    /// <inheritdoc />
 43    public byte[] HashSecret(byte[] secret, byte[] salt)
 44    {
 045        using var sha256 = SHA256.Create();
 046        var passwordAndSalt = secret.Concat(salt).ToArray();
 047        return sha256.ComputeHash(passwordAndSalt);
 048    }
 49
 50    /// <inheritdoc />
 051    public byte[] GenerateSalt(int saltSize = 32) => RandomNumberGenerator.GetBytes(saltSize);
 52}