Skip to content

Commit 01db819

Browse files
committed
Initial working version using Sodium.Core. Options TODO
1 parent cb78b23 commit 01db819

File tree

5 files changed

+143
-0
lines changed

5 files changed

+143
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio 15
4+
VisualStudioVersion = 15.0.27004.2005
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{A267ABB6-E68E-4B75-9482-DB0DB2AFF700}"
7+
EndProject
8+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ScottBrady91.AspNetCore.Identity.ScryptPasswordHasher", "src\ScottBrady91.AspNetCore.Identity.ScryptPasswordHasher\ScottBrady91.AspNetCore.Identity.ScryptPasswordHasher.csproj", "{294FB442-6D65-445D-9639-B0D8103509AE}"
9+
EndProject
10+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{BD00361E-A90E-4DB8-8792-944235D0648C}"
11+
EndProject
12+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ScottBrady91.AspNetCore.Identity.ScryptPasswordHasher.Tests", "test\ScottBrady91.AspNetCore.Identity.ScryptPasswordHasher.Tests\ScottBrady91.AspNetCore.Identity.ScryptPasswordHasher.Tests.csproj", "{DEB0A55A-EDE3-49C7-9D9A-2676CDCD7B9A}"
13+
EndProject
14+
Global
15+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
16+
Debug|Any CPU = Debug|Any CPU
17+
Release|Any CPU = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
20+
{294FB442-6D65-445D-9639-B0D8103509AE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
21+
{294FB442-6D65-445D-9639-B0D8103509AE}.Debug|Any CPU.Build.0 = Debug|Any CPU
22+
{294FB442-6D65-445D-9639-B0D8103509AE}.Release|Any CPU.ActiveCfg = Release|Any CPU
23+
{294FB442-6D65-445D-9639-B0D8103509AE}.Release|Any CPU.Build.0 = Release|Any CPU
24+
{DEB0A55A-EDE3-49C7-9D9A-2676CDCD7B9A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
25+
{DEB0A55A-EDE3-49C7-9D9A-2676CDCD7B9A}.Debug|Any CPU.Build.0 = Debug|Any CPU
26+
{DEB0A55A-EDE3-49C7-9D9A-2676CDCD7B9A}.Release|Any CPU.ActiveCfg = Release|Any CPU
27+
{DEB0A55A-EDE3-49C7-9D9A-2676CDCD7B9A}.Release|Any CPU.Build.0 = Release|Any CPU
28+
EndGlobalSection
29+
GlobalSection(SolutionProperties) = preSolution
30+
HideSolutionNode = FALSE
31+
EndGlobalSection
32+
GlobalSection(NestedProjects) = preSolution
33+
{294FB442-6D65-445D-9639-B0D8103509AE} = {A267ABB6-E68E-4B75-9482-DB0DB2AFF700}
34+
{DEB0A55A-EDE3-49C7-9D9A-2676CDCD7B9A} = {BD00361E-A90E-4DB8-8792-944235D0648C}
35+
EndGlobalSection
36+
GlobalSection(ExtensibilityGlobals) = postSolution
37+
SolutionGuid = {E549FCCA-5469-4AB8-90A4-CB810EDAD0D7}
38+
EndGlobalSection
39+
EndGlobal
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netstandard2.0</TargetFramework>
5+
</PropertyGroup>
6+
7+
<ItemGroup>
8+
<PackageReference Include="Microsoft.Extensions.Identity.Core" Version="2.0.0" />
9+
<PackageReference Include="Scrypt.NET" Version="1.3.0" />
10+
</ItemGroup>
11+
12+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using System;
2+
using Microsoft.AspNetCore.Identity;
3+
using Scrypt;
4+
5+
namespace ScottBrady91.AspNetCore.Identity
6+
{
7+
public class ScryptPasswordHasher<TUser> : IPasswordHasher<TUser> where TUser : class
8+
{
9+
public string HashPassword(TUser user, string password)
10+
{
11+
if (password == null) throw new ArgumentNullException(nameof(password));
12+
13+
var encoder = new ScryptEncoder();
14+
return encoder.Encode(password);
15+
}
16+
17+
public PasswordVerificationResult VerifyHashedPassword(TUser user, string hashedPassword, string providedPassword)
18+
{
19+
if (hashedPassword == null) throw new ArgumentNullException(nameof(hashedPassword));
20+
if (providedPassword == null) throw new ArgumentNullException(nameof(providedPassword));
21+
22+
var encoder = new ScryptEncoder();
23+
var isValid = encoder.Compare(providedPassword, hashedPassword);
24+
25+
return isValid ? PasswordVerificationResult.Success : PasswordVerificationResult.Failed;
26+
}
27+
}
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netcoreapp2.0</TargetFramework>
5+
</PropertyGroup>
6+
7+
<ItemGroup>
8+
<PackageReference Include="FluentAssertions" Version="4.19.4" />
9+
<PackageReference Include="xunit" Version="2.3.0" />
10+
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.0" />
11+
</ItemGroup>
12+
13+
<ItemGroup>
14+
<ProjectReference Include="..\..\src\ScottBrady91.AspNetCore.Identity.ScryptPasswordHasher\ScottBrady91.AspNetCore.Identity.ScryptPasswordHasher.csproj" />
15+
</ItemGroup>
16+
17+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
using System;
2+
using FluentAssertions;
3+
using Microsoft.AspNetCore.Identity;
4+
using Scrypt;
5+
using Xunit;
6+
7+
namespace ScottBrady91.AspNetCore.Identity.ScryptPasswordHasher.Tests
8+
{
9+
public class ScryptPasswordHasherTests
10+
{
11+
[Fact]
12+
public void HashPassword_WithDefaultSettings_ExpectVerifiableHash()
13+
{
14+
var password = Guid.NewGuid().ToString();
15+
16+
var hasher = new ScryptPasswordHasher<string>();
17+
var hashedPassword = hasher.HashPassword("", password);
18+
19+
var encoder = new ScryptEncoder();
20+
encoder.Compare(password, hashedPassword).Should().BeTrue();
21+
}
22+
23+
[Fact]
24+
public void VerifyHashedPassword_WithDefaultSettings_ExpectSuccess()
25+
{
26+
var password = Guid.NewGuid().ToString();
27+
var encoder = new ScryptEncoder();
28+
var hashedPassword = encoder.Encode(password);
29+
30+
var hasher = new ScryptPasswordHasher<string>();
31+
32+
hasher.VerifyHashedPassword("", hashedPassword, password).Should().Be(PasswordVerificationResult.Success);
33+
}
34+
35+
[Fact]
36+
public void VerifyHashedPassword_WhenSuppliedPasswordDoesNotMatch_ExpectFailure()
37+
{
38+
var password = Guid.NewGuid().ToString();
39+
var encoder = new ScryptEncoder();
40+
var hashedPassword = encoder.Encode(Guid.NewGuid().ToString());
41+
42+
var hasher = new ScryptPasswordHasher<string>();
43+
44+
hasher.VerifyHashedPassword("", hashedPassword, password).Should().Be(PasswordVerificationResult.Failed);
45+
}
46+
}
47+
}

0 commit comments

Comments
 (0)