Skip to content

Commit

Permalink
Updated hashing algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
madskristensen committed Aug 29, 2017
1 parent 52a3a5c commit 2e2f722
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 14 deletions.
19 changes: 11 additions & 8 deletions src/Pages/Login.cshtml.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Cryptography.KeyDerivation;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.Extensions.Configuration;
using System;
Expand Down Expand Up @@ -58,16 +59,18 @@ private void RedirectFromLogin(string query = "")

private bool VerifyHashedPassword(string password)
{
byte[] passwordBytes = Encoding.UTF8.GetBytes(password);
byte[] saltBytes = Encoding.UTF8.GetBytes(_config["user:salt"]);
byte[] saltedValue = passwordBytes.Concat(saltBytes).ToArray();

using (var sha = new SHA256Managed())
{
byte[] hash = sha.ComputeHash(saltedValue);
var hashText = BitConverter.ToString(hash).Replace("-", string.Empty);
return hashText == _config["user:password"];
}
byte[] hashBytes = KeyDerivation.Pbkdf2(
password: password,
salt: saltBytes,
prf: KeyDerivationPrf.HMACSHA1,
iterationCount: 1000,
numBytesRequested: 256 / 8
);

string hashText = BitConverter.ToString(hashBytes).Replace("-", string.Empty);
return hashText == _config["user:password"];
}
}
}
6 changes: 3 additions & 3 deletions src/appsettings.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"user": {
"username": "demo",
// Generate a new password hash with salt here https://www.convertstring.com/en/Hash/SHA256
"password": "9932330BBB4675D16E622A5FEDF2B0AB93081BEE34E23D14762A46BF4CAF0C8E", // this password is "demo"
"salt": "somestring"
// Generate a new password hash with salt here https://onlinehasher.azurewebsites.net/
"password": "EB53D045EB132825A39F59AEA3FC453F216CB088775D6E7CE4A9740611B573CD", // this password is "demo"
"salt": "any custom string"
},
"Logging": {
"IncludeScopes": false,
Expand Down
4 changes: 1 addition & 3 deletions src/imageoptimizer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,5 @@
"includes": [ "wwwroot/albums/*/*.*" ],
"lossy": false
}
],

"warmupTime": 0
]
}

0 comments on commit 2e2f722

Please sign in to comment.