< Summary

Information
Class: Elsa.Identity.Contracts.ISecretHasher
Assembly: Elsa.Identity
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Identity/Contracts/ISecretHasher.cs
Line coverage
80%
Covered lines: 4
Uncovered lines: 1
Coverable lines: 5
Total lines: 84
Line coverage: 80%
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
VerifySecret(...)100%11100%
VerifySecret(...)100%11100%
GenerateSalt(...)100%210%

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Identity/Contracts/ISecretHasher.cs

#LineLine coverage
 1using System.Security.Cryptography;
 2using Elsa.Identity.Models;
 3
 4namespace Elsa.Identity.Contracts;
 5
 6/// <summary>
 7/// Represents a secret hasher.
 8/// </summary>
 9public interface ISecretHasher
 10{
 11    /// <summary>
 12    /// Hashes the secret.
 13    /// </summary>
 14    /// <param name="secret">The secret to hash.</param>
 15    /// <returns>The hashed secret.</returns>
 16    HashedSecret HashSecret(string secret);
 17
 18    /// <summary>
 19    /// Hashes the secret.
 20    /// </summary>
 21    /// <param name="secret">The secret to hash.</param>
 22    /// <param name="salt">The salt to use.</param>
 23    /// <returns>The hashed secret.</returns>
 24    HashedSecret HashSecret(string secret, byte[] salt);
 25
 26    /// <summary>
 27    /// Hashes the secret.
 28    /// </summary>
 29    /// <param name="secret">The secret to hash.</param>
 30    /// <param name="salt">The salt to use.</param>
 31    /// <returns>The hashed secret bytes. Implementations should return a self-describing format that <see cref="VerifyS
 32    byte[] HashSecret(byte[] secret, byte[] salt);
 33
 34    /// <summary>
 35    /// Verifies the secret.
 36    /// </summary>
 37    /// <param name="clearTextSecret">The secret to verify.</param>
 38    /// <param name="secret">The hashed secret.</param>
 39    /// <param name="salt">The salt.</param>
 40    /// <returns>True if the secret is valid, otherwise false.</returns>
 41    bool VerifySecret(string clearTextSecret, string secret, string salt);
 42
 43    /// <summary>
 44    /// Verifies the secret.
 45    /// </summary>
 46    /// <param name="clearTextSecret">The secret to verify.</param>
 47    /// <param name="secret">The hashed secret.</param>
 48    /// <param name="salt">The salt.</param>
 49    /// <param name="needsRehash">Whether the stored hash should be upgraded.</param>
 50    /// <returns>True if the secret is valid, otherwise false.</returns>
 51    bool VerifySecret(string clearTextSecret, string secret, string salt, out bool needsRehash)
 52    {
 153        needsRehash = false;
 154        return VerifySecret(clearTextSecret, secret, salt);
 55    }
 56
 57    /// <summary>
 58    /// Verifies the secret.
 59    /// </summary>
 60    /// <param name="clearTextSecret">The secret to verify.</param>
 61    /// <param name="hashedSecret">The hashed secret.</param>
 62    /// <returns>True if the secret is valid, otherwise false.</returns>
 63    bool VerifySecret(string clearTextSecret, HashedSecret hashedSecret);
 64
 65    /// <summary>
 66    /// Verifies the secret.
 67    /// </summary>
 68    /// <param name="clearTextSecret">The secret to verify.</param>
 69    /// <param name="hashedSecret">The hashed secret.</param>
 70    /// <param name="needsRehash">Whether the stored hash should be upgraded.</param>
 71    /// <returns>True if the secret is valid, otherwise false.</returns>
 72    bool VerifySecret(string clearTextSecret, HashedSecret hashedSecret, out bool needsRehash)
 73    {
 174        needsRehash = false;
 175        return VerifySecret(clearTextSecret, hashedSecret);
 76    }
 77
 78    /// <summary>
 79    /// Generates a salt.
 80    /// </summary>
 81    /// <param name="saltSize">The size of the salt.</param>
 82    /// <returns>The salt.</returns>
 083    byte[] GenerateSalt(int saltSize = 32) => RandomNumberGenerator.GetBytes(saltSize);
 84}