-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature: Adicionado hash no password antes de salvar no banco.
- Loading branch information
1 parent
24f1fff
commit ba96d5e
Showing
4 changed files
with
66 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
using System.Security.Cryptography; | ||
using System; | ||
using System.Linq; | ||
|
||
namespace TemplateBase.Domain.Utils | ||
{ | ||
public static class Hasher | ||
{ | ||
private const int _saltSize = 16; | ||
private const int _keySize = 32; | ||
private const int _iterations = 100000; | ||
private static readonly HashAlgorithmName _algorithm = HashAlgorithmName.SHA256; | ||
|
||
private const char segmentDelimiter = ':'; | ||
|
||
public static string Hash(string secret) | ||
{ | ||
var salt = RandomNumberGenerator.GetBytes(_saltSize); | ||
var key = Rfc2898DeriveBytes.Pbkdf2( | ||
secret, | ||
salt, | ||
_iterations, | ||
_algorithm, | ||
_keySize | ||
); | ||
return string.Join( | ||
segmentDelimiter, | ||
Convert.ToHexString(key), | ||
Convert.ToHexString(salt), | ||
_iterations, | ||
_algorithm | ||
); | ||
} | ||
|
||
public static bool Verify(string secret, string hash) | ||
{ | ||
var segments = hash.Split(segmentDelimiter); | ||
var key = Convert.FromHexString(segments[0]); | ||
var salt = Convert.FromHexString(segments[1]); | ||
var iterations = int.Parse(segments[2]); | ||
var algorithm = new HashAlgorithmName(segments[3]); | ||
var inputSecretKey = Rfc2898DeriveBytes.Pbkdf2( | ||
secret, | ||
salt, | ||
iterations, | ||
algorithm, | ||
key.Length | ||
); | ||
return key.SequenceEqual(inputSecretKey); | ||
} | ||
} | ||
} |