Skip to content

Commit 828b155

Browse files
committed
Added options, readme, and nuget info
1 parent 01db819 commit 828b155

File tree

6 files changed

+92
-3
lines changed

6 files changed

+92
-3
lines changed

README.md

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Scrypt Password Hasher for ASP.NET Core Identity (ASP.NET Identity 3)
2+
3+
An implementation of IPasswordHasher<TUser> using [Scrypt.NET](https://github.com/viniciuschiele/Scrypt).
4+
5+
## Installation
6+
7+
```
8+
services.AddScoped<IPasswordHasher<ApplicationUser>, ScryptPasswordHasher<ApplicationUser>>();
9+
```
10+
11+
### Options
12+
13+
- **IterationCount**: int
14+
- **BlockSize**: int
15+
- **ThreadCount**: int
16+
17+
Register with:
18+
```
19+
services.Configure<ScryptPasswordHasherOptions>(options => {
20+
options.IterationCount = 16384;
21+
options.BlockSize = 8;
22+
options.ThreadCount = 1;
23+
});
24+
```

src/ScottBrady91.AspNetCore.Identity.ScryptPasswordHasher/ScottBrady91.AspNetCore.Identity.ScryptPasswordHasher.csproj

+11
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,17 @@
22

33
<PropertyGroup>
44
<TargetFramework>netstandard2.0</TargetFramework>
5+
6+
<AssemblyName>ScottBrady91.AspNetCore.Identity.ScryptPasswordHasher</AssemblyName>
7+
<PackageId>ScottBrady91.AspNetCore.Identity.ScryptPasswordHasher</PackageId>
8+
<PackageVersion>1.0.0</PackageVersion>
9+
<Authors>Scott Brady</Authors>
10+
<Description>ASP.NET Core Identity IPasswordHasher implementation using Scrypt</Description>
11+
<Copyright>Copyright (c) 2017 Scott Brady</Copyright>
12+
<PackageTags>aspnetcore;identity;scrypt;password;hashing;hash;security</PackageTags>
13+
<PackageIconUrl>https://www.scottbrady91.com/img/logos/scottbrady91.png</PackageIconUrl>
14+
<PackageProjectUrl>https://github.com/scottbrady91/ScottBrady91.AspNetCore.Identity.ScryptPasswordHasher</PackageProjectUrl>
15+
<PackageLicenseUrl>https://github.com/scottbrady91/ScottBrady91.AspNetCore.Identity.ScryptPasswordHasher/blob/master/LICENSE</PackageLicenseUrl>
516
</PropertyGroup>
617

718
<ItemGroup>

src/ScottBrady91.AspNetCore.Identity.ScryptPasswordHasher/ScryptPasswordHasher.cs

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,24 @@
11
using System;
22
using Microsoft.AspNetCore.Identity;
3+
using Microsoft.Extensions.Options;
34
using Scrypt;
45

56
namespace ScottBrady91.AspNetCore.Identity
67
{
78
public class ScryptPasswordHasher<TUser> : IPasswordHasher<TUser> where TUser : class
89
{
10+
private readonly ScryptPasswordHasherOptions options;
11+
12+
public ScryptPasswordHasher(IOptions<ScryptPasswordHasherOptions> optionsAccessor = null)
13+
{
14+
options = optionsAccessor?.Value ?? new ScryptPasswordHasherOptions();
15+
}
16+
917
public string HashPassword(TUser user, string password)
1018
{
1119
if (password == null) throw new ArgumentNullException(nameof(password));
1220

13-
var encoder = new ScryptEncoder();
21+
var encoder = new ScryptEncoder(options.IterationCount, options.BlockSize, options.ThreadCount);
1422
return encoder.Encode(password);
1523
}
1624

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace ScottBrady91.AspNetCore.Identity
2+
{
3+
public class ScryptPasswordHasherOptions
4+
{
5+
public int IterationCount { get; set; } = 16384;
6+
public int BlockSize { get; set; } = 8;
7+
public int ThreadCount { get; set; } = 1;
8+
}
9+
}

test/ScottBrady91.AspNetCore.Identity.ScryptPasswordHasher.Tests/ScottBrady91.AspNetCore.Identity.ScryptPasswordHasher.Tests.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>netcoreapp2.0</TargetFramework>
4+
<TargetFrameworks>netcoreapp2.0;net461</TargetFrameworks>
55
</PropertyGroup>
66

77
<ItemGroup>

test/ScottBrady91.AspNetCore.Identity.ScryptPasswordHasher.Tests/ScryptPasswordHasherTests.cs

+38-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using FluentAssertions;
33
using Microsoft.AspNetCore.Identity;
4+
using Microsoft.Extensions.Options;
45
using Scrypt;
56
using Xunit;
67

@@ -19,7 +20,26 @@ public void HashPassword_WithDefaultSettings_ExpectVerifiableHash()
1920
var encoder = new ScryptEncoder();
2021
encoder.Compare(password, hashedPassword).Should().BeTrue();
2122
}
22-
23+
24+
[Fact]
25+
public void HashPassword_WithCustomSettings_ExpectVerifiableHash()
26+
{
27+
var random = new Random();
28+
var iterationCount = (int)Math.Pow(2.00, random.Next(15, 20));
29+
var blockSize = random.Next(9, 12);
30+
const int threadCount = 2;
31+
32+
var password = Guid.NewGuid().ToString();
33+
34+
var hasher = new ScryptPasswordHasher<string>(
35+
new OptionsWrapper<ScryptPasswordHasherOptions>(
36+
new ScryptPasswordHasherOptions {IterationCount = iterationCount, BlockSize = blockSize, ThreadCount = threadCount}));
37+
var hashedPassword = hasher.HashPassword("", password);
38+
39+
var encoder = new ScryptEncoder();
40+
encoder.Compare(password, hashedPassword).Should().BeTrue();
41+
}
42+
2343
[Fact]
2444
public void VerifyHashedPassword_WithDefaultSettings_ExpectSuccess()
2545
{
@@ -32,6 +52,23 @@ public void VerifyHashedPassword_WithDefaultSettings_ExpectSuccess()
3252
hasher.VerifyHashedPassword("", hashedPassword, password).Should().Be(PasswordVerificationResult.Success);
3353
}
3454

55+
[Fact]
56+
public void VerifyHashedPassword_WithCustomSettings_ExpectSuccess()
57+
{
58+
var random = new Random();
59+
var iterationCount = (int)Math.Pow(2.00, random.Next(15, 20));
60+
var blockSize = random.Next(9, 12);
61+
const int threadCount = 2;
62+
63+
var password = Guid.NewGuid().ToString();
64+
var encoder = new ScryptEncoder(iterationCount, blockSize, threadCount);
65+
var hashedPassword = encoder.Encode(password);
66+
67+
var hasher = new ScryptPasswordHasher<string>();
68+
69+
hasher.VerifyHashedPassword("", hashedPassword, password).Should().Be(PasswordVerificationResult.Success);
70+
}
71+
3572
[Fact]
3673
public void VerifyHashedPassword_WhenSuppliedPasswordDoesNotMatch_ExpectFailure()
3774
{

0 commit comments

Comments
 (0)