< Summary - Kestrun — Combined Coverage

Information
Class: Kestrun.FixedTimeEquals
Assembly: Kestrun
File(s): /home/runner/work/Kestrun/Kestrun/src/CSharp/Kestrun/SecurityUtilities.cs
Tag: Kestrun/Kestrun@9d3a582b2d63930269564a7591aa77ef297cadeb
Line coverage
90%
Covered lines: 20
Uncovered lines: 2
Coverable lines: 22
Total lines: 109
Line coverage: 90.9%
Branch coverage
83%
Covered branches: 10
Total branches: 12
Branch coverage: 83.3%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Coverage history

Coverage history 0 25 50 75 100

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
Test(...)100%44100%
Test(...)100%44100%
Test(...)50%2275%
Test(...)50%2275%
Test(...)100%11100%
Test(...)100%11100%
Test(...)100%11100%

File(s)

/home/runner/work/Kestrun/Kestrun/src/CSharp/Kestrun/SecurityUtilities.cs

#LineLine coverage
 1using System.Text;
 2
 3namespace Kestrun;
 4
 5/// <summary>
 6/// Provides constant-time comparison methods to prevent timing attacks.
 7/// </summary>
 8public static class FixedTimeEquals
 9{
 10    /// <summary>
 11    /// Compares two byte arrays in constant time to prevent timing attacks.
 12    /// </summary>
 13    /// <param name="a">First byte array.</param>
 14    /// <param name="b">Second byte array.</param>
 15    /// <returns>True if both arrays are equal, false otherwise.</returns>
 16    public static bool Test(ReadOnlySpan<byte> a, ReadOnlySpan<byte> b)
 17    {
 2118        if (a.Length != b.Length)
 19        {
 720            return false;
 21        }
 22
 1423        var diff = 0;
 33624        for (var i = 0; i < a.Length; i++)
 25        {
 15426            diff |= a[i] ^ b[i];
 27        }
 28
 1429        return diff == 0;
 30    }
 31
 32    /// <summary>
 33    /// Compares two strings in constant time to prevent timing attacks.
 34    /// </summary>
 35    /// <param name="a">First string to compare.</param>
 36    /// <param name="b">Second string to compare.</param>
 37    /// <returns>True if both strings are equal, false otherwise.</returns>
 38    public static bool Test(string? a, string? b)
 39    {
 840        if (a == null || b == null)
 41        {
 442            return false;
 43        }
 44
 445        var aBytes = Encoding.UTF8.GetBytes(a);
 446        var bBytes = Encoding.UTF8.GetBytes(b);
 447        return Test(aBytes, bBytes);
 48    }
 49
 50    /// <summary>
 51    /// Compares a byte span and a string in constant time to prevent timing attacks.
 52    /// </summary>
 53    /// <param name="a">The byte span to compare.</param>
 54    /// <param name="b">The string to compare.</param>
 55    /// <returns>True if both are equal, false otherwise.</returns>
 56    public static bool Test(ReadOnlySpan<byte> a, string? b)
 57    {
 258        if (b == null)
 59        {
 060            return false;
 61        }
 62
 263        var bBytes = Encoding.UTF8.GetBytes(b);
 264        return Test(a, bBytes);
 65    }
 66    /// <summary>
 67    /// Compares a string and a byte span in constant time to prevent timing attacks.
 68    /// </summary>
 69    /// <param name="a">The string to compare.</param>
 70    /// <param name="b">The byte span to compare.</param>
 71    /// <returns>True if both are equal, false otherwise.</returns>
 72    public static bool Test(string? a, ReadOnlySpan<byte> b)
 73    {
 274        if (a == null)
 75        {
 076            return false;
 77        }
 78
 279        var aBytes = Encoding.UTF8.GetBytes(a);
 280        return Test(aBytes, b);
 81    }
 82
 83    /// <summary>
 84    /// Compares a byte array and a string in constant time to prevent timing attacks.
 85    /// </summary>
 86    /// <param name="a">The byte array to compare.</param>
 87    /// <param name="b">The string to compare.</param>
 88    /// <returns>True if both are equal, false otherwise.</returns>
 89    public static bool Test(byte[] a, string b) =>
 290    Test(a.AsSpan(), b);
 91
 92    /// <summary>
 93    /// Compares two byte arrays in constant time to prevent timing attacks.
 94    /// </summary>
 95    /// <param name="a">First byte array.</param>
 96    /// <param name="b">Second byte array.</param>
 97    /// <returns>True if both arrays are equal, false otherwise.</returns>
 98    public static bool Test(byte[] a, byte[] b) =>
 1799    Test(a.AsSpan(), b.AsSpan());
 100
 101    /// <summary>
 102    /// Compares a string and a byte array in constant time to prevent timing attacks.
 103    /// </summary>
 104    /// <param name="a">The string to compare.</param>
 105    /// <param name="b">The byte array to compare.</param>
 106    /// <returns>True if both are equal, false otherwise.</returns>
 107    public static bool Test(string? a, byte[] b) =>
 2108    Test(a, b.AsSpan());
 109}