< Summary - Jellyfin

Information
Class: Emby.Server.Implementations.Cryptography.CryptographyProvider
Assembly: Emby.Server.Implementations
File(s): /srv/git/jellyfin/Emby.Server.Implementations/Cryptography/CryptographyProvider.cs
Line coverage
55%
Covered lines: 21
Uncovered lines: 17
Coverable lines: 38
Total lines: 78
Line coverage: 55.2%
Branch coverage
0%
Covered branches: 0
Total branches: 4
Branch coverage: 0%
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
get_DefaultHashMethod()100%11100%
CreatePasswordHash(...)100%11100%
Verify(...)0%2040%
GenerateSalt()100%11100%
GenerateSalt(...)100%11100%

File(s)

/srv/git/jellyfin/Emby.Server.Implementations/Cryptography/CryptographyProvider.cs

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3using System.Globalization;
 4using System.Security.Cryptography;
 5using MediaBrowser.Model.Cryptography;
 6using static MediaBrowser.Model.Cryptography.Constants;
 7
 8namespace Emby.Server.Implementations.Cryptography
 9{
 10    /// <summary>
 11    /// Class providing abstractions over cryptographic functions.
 12    /// </summary>
 13    public class CryptographyProvider : ICryptoProvider
 14    {
 15        /// <inheritdoc />
 216        public string DefaultHashMethod => "PBKDF2-SHA512";
 17
 18        /// <inheritdoc />
 19        public PasswordHash CreatePasswordHash(ReadOnlySpan<char> password)
 20        {
 221            byte[] salt = GenerateSalt();
 222            return new PasswordHash(
 223                DefaultHashMethod,
 224                Rfc2898DeriveBytes.Pbkdf2(
 225                    password,
 226                    salt,
 227                    DefaultIterations,
 228                    HashAlgorithmName.SHA512,
 229                    DefaultOutputLength),
 230                salt,
 231                new Dictionary<string, string>
 232                {
 233                    { "iterations", DefaultIterations.ToString(CultureInfo.InvariantCulture) }
 234                });
 35        }
 36
 37        /// <inheritdoc />
 38        public bool Verify(PasswordHash hash, ReadOnlySpan<char> password)
 39        {
 040            if (string.Equals(hash.Id, "PBKDF2", StringComparison.Ordinal))
 41            {
 042                return hash.Hash.SequenceEqual(
 043                    Rfc2898DeriveBytes.Pbkdf2(
 044                        password,
 045                        hash.Salt,
 046                        int.Parse(hash.Parameters["iterations"], CultureInfo.InvariantCulture),
 047                        HashAlgorithmName.SHA1,
 048                        32));
 49            }
 50
 051            if (string.Equals(hash.Id, "PBKDF2-SHA512", StringComparison.Ordinal))
 52            {
 053                return hash.Hash.SequenceEqual(
 054                    Rfc2898DeriveBytes.Pbkdf2(
 055                        password,
 056                        hash.Salt,
 057                        int.Parse(hash.Parameters["iterations"], CultureInfo.InvariantCulture),
 058                        HashAlgorithmName.SHA512,
 059                        DefaultOutputLength));
 60            }
 61
 062            throw new NotSupportedException($"Can't verify hash with id: {hash.Id}");
 63        }
 64
 65        /// <inheritdoc />
 66        public byte[] GenerateSalt()
 267            => GenerateSalt(DefaultSaltLength);
 68
 69        /// <inheritdoc />
 70        public byte[] GenerateSalt(int length)
 71        {
 272            var salt = new byte[length];
 273            using var rng = RandomNumberGenerator.Create();
 274            rng.GetNonZeroBytes(salt);
 275            return salt;
 276        }
 77    }
 78}