Skip to content

Instantly share code, notes, and snippets.

@ntxinh
Last active August 5, 2020 07:08
Show Gist options
  • Select an option

  • Save ntxinh/d8421902cbe7121db0c9116a99573ea4 to your computer and use it in GitHub Desktop.

Select an option

Save ntxinh/d8421902cbe7121db0c9116a99573ea4 to your computer and use it in GitHub Desktop.

Revisions

  1. ntxinh revised this gist Aug 5, 2020. 3 changed files with 8 additions and 1 deletion.
    2 changes: 2 additions & 0 deletions HashingOptions.cs
    Original file line number Diff line number Diff line change
    @@ -3,6 +3,8 @@ namespace Core.Services.Hash
    {
    public sealed class HashingOptions
    {
    public const string Hashing = "Hashing";

    public int Iterations { get; set; } = 10000;
    }
    }
    2 changes: 1 addition & 1 deletion Startup.cs
    Original file line number Diff line number Diff line change
    @@ -4,7 +4,7 @@ public class Startup
    {
    public void ConfigureServices(IServiceCollection services)
    {
    services.Configure<HashingOptions>(Configuration);
    services.Configure<HashingOptions>(Configuration.GetSection(HashingOptions.Hashing));
    services.AddScoped<IPasswordHasher, PasswordHasher>();
    }
    }
    5 changes: 5 additions & 0 deletions appsettings.json
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,5 @@
    {
    "Hashing": {
    "Iterations": 10000
    }
    }
  2. ntxinh created this gist Aug 5, 2020.
    8 changes: 8 additions & 0 deletions HashingOptions.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,8 @@
    // Source: https://medium.com/dealeron-dev/storing-passwords-in-net-core-3de29a3da4d2
    namespace Core.Services.Hash
    {
    public sealed class HashingOptions
    {
    public int Iterations { get; set; } = 10000;
    }
    }
    9 changes: 9 additions & 0 deletions IPasswordHasher.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,9 @@
    namespace Core.Services.Hash
    {
    public interface IPasswordHasher
    {
    string Hash(string password);

    (bool Verified, bool NeedsUpgrade) Check(string hash, string password);
    }
    }
    65 changes: 65 additions & 0 deletions PasswordHasher.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,65 @@
    using System;
    using System.Linq;
    using System.Security.Cryptography;
    using Microsoft.Extensions.Options;

    namespace Core.Services.Hash
    {
    public sealed class PasswordHasher : IPasswordHasher
    {
    private const int SaltSize = 16; // 128 bit
    private const int KeySize = 32; // 256 bit

    public PasswordHasher(IOptions<HashingOptions> options)
    {
    Options = options.Value;
    }

    private HashingOptions Options { get; }

    public string Hash(string password)
    {
    using (var algorithm = new Rfc2898DeriveBytes(
    password,
    SaltSize,
    Options.Iterations,
    HashAlgorithmName.SHA512))
    {
    var key = Convert.ToBase64String(algorithm.GetBytes(KeySize));
    var salt = Convert.ToBase64String(algorithm.Salt);

    return $"{Options.Iterations}.{salt}.{key}";
    }
    }

    public (bool Verified, bool NeedsUpgrade) Check(string hash, string password)
    {
    var parts = hash.Split('.', 3);

    if (parts.Length != 3)
    {
    throw new FormatException("Unexpected hash format. " +
    "Should be formatted as `{iterations}.{salt}.{hash}`");
    }

    var iterations = Convert.ToInt32(parts[0]);
    var salt = Convert.FromBase64String(parts[1]);
    var key = Convert.FromBase64String(parts[2]);

    var needsUpgrade = iterations != Options.Iterations;

    using (var algorithm = new Rfc2898DeriveBytes(
    password,
    salt,
    iterations,
    HashAlgorithmName.SHA512))
    {
    var keyToCheck = algorithm.GetBytes(KeySize);

    var verified = keyToCheck.SequenceEqual(key);

    return (verified, needsUpgrade);
    }
    }
    }
    }
    11 changes: 11 additions & 0 deletions Startup.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,11 @@
    namespace Core
    {
    public class Startup
    {
    public void ConfigureServices(IServiceCollection services)
    {
    services.Configure<HashingOptions>(Configuration);
    services.AddScoped<IPasswordHasher, PasswordHasher>();
    }
    }
    }