2025-02-16 23:39:26 +01:00
|
|
|
using System.Diagnostics;
|
|
|
|
|
using Sodium;
|
2025-02-20 00:23:13 +01:00
|
|
|
using PlanTempus.Core.Entities.Users;
|
2025-02-13 00:40:42 +01:00
|
|
|
|
2025-02-20 00:23:13 +01:00
|
|
|
namespace PlanTempus.Tests
|
2025-02-13 00:40:42 +01:00
|
|
|
{
|
|
|
|
|
[TestClass]
|
|
|
|
|
public class PasswordHasherTests : TestFixture
|
|
|
|
|
{
|
2025-02-16 23:39:26 +01:00
|
|
|
|
|
|
|
|
[TestMethod]
|
|
|
|
|
public void MyTestMethod()
|
|
|
|
|
{
|
|
|
|
|
var stopwatch = Stopwatch.StartNew();
|
|
|
|
|
|
|
|
|
|
byte[] salt = PasswordHash.ScryptGenerateSalt();
|
|
|
|
|
|
|
|
|
|
// 2. Konverter password til byte[]
|
|
|
|
|
byte[] passwordBytes = System.Text.Encoding.UTF8.GetBytes("password123");
|
|
|
|
|
|
|
|
|
|
// 3. Kald ScryptHashBinary korrekt
|
|
|
|
|
byte[] hash = PasswordHash.ScryptHashBinary(
|
|
|
|
|
password: passwordBytes,
|
|
|
|
|
salt: salt, // 32-byte array
|
|
|
|
|
limit: PasswordHash.Strength.Interactive,
|
|
|
|
|
outputLength: 32
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
stopwatch.Stop();
|
|
|
|
|
}
|
2025-02-13 00:40:42 +01:00
|
|
|
[TestMethod]
|
|
|
|
|
public void HashPassword_ShouldCreateValidHashFormat()
|
|
|
|
|
{
|
|
|
|
|
// Arrange
|
|
|
|
|
string password = "TestPassword123";
|
|
|
|
|
|
|
|
|
|
// Act
|
|
|
|
|
string hashedPassword = PasswordHasher.HashPassword(password);
|
|
|
|
|
string[] parts = hashedPassword.Split('.');
|
|
|
|
|
|
|
|
|
|
// Assert
|
|
|
|
|
Assert.AreEqual(3, parts.Length);
|
2025-02-27 17:41:21 +01:00
|
|
|
Assert.AreEqual("100000", parts[0]);
|
2025-02-13 00:40:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[TestMethod]
|
|
|
|
|
public void VerifyPassword_WithCorrectPassword_ShouldReturnTrue()
|
|
|
|
|
{
|
|
|
|
|
// Arrange
|
|
|
|
|
string password = "TestPassword123";
|
|
|
|
|
string hashedPassword = PasswordHasher.HashPassword(password);
|
|
|
|
|
|
|
|
|
|
// Act
|
|
|
|
|
bool result = PasswordHasher.VerifyPassword(hashedPassword, password);
|
|
|
|
|
|
|
|
|
|
// Assert
|
|
|
|
|
Assert.IsTrue(result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[TestMethod]
|
|
|
|
|
public void VerifyPassword_WithWrongPassword_ShouldReturnFalse()
|
|
|
|
|
{
|
|
|
|
|
// Arrange
|
|
|
|
|
string correctPassword = "TestPassword123";
|
|
|
|
|
string wrongPassword = "WrongPassword123";
|
|
|
|
|
string hashedPassword = PasswordHasher.HashPassword(correctPassword);
|
|
|
|
|
|
|
|
|
|
// Act
|
|
|
|
|
bool result = PasswordHasher.VerifyPassword(hashedPassword, wrongPassword);
|
|
|
|
|
|
|
|
|
|
// Assert
|
|
|
|
|
Assert.IsFalse(result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[TestMethod]
|
|
|
|
|
public void VerifyPassword_WithInvalidHashFormat_ShouldReturnFalse()
|
|
|
|
|
{
|
|
|
|
|
// Arrange
|
|
|
|
|
string password = "TestPassword123";
|
|
|
|
|
string invalidHash = "InvalidHash";
|
|
|
|
|
|
|
|
|
|
// Act
|
|
|
|
|
bool result = PasswordHasher.VerifyPassword(invalidHash, password);
|
|
|
|
|
|
|
|
|
|
// Assert
|
|
|
|
|
Assert.IsFalse(result);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|