This commit is contained in:
Janus C. H. Knudsen 2025-03-07 00:17:08 +01:00
parent 0010a32248
commit ddb6abc14e
14 changed files with 754 additions and 718 deletions

View file

@ -1,89 +1,87 @@
using System.Diagnostics;
using Sodium;
using System.Text;
using PlanTempus.Core;
using Sodium;
namespace PlanTempus.Tests
namespace PlanTempus.X.TDD;
[TestClass]
public class PasswordHasherTests : TestFixture
{
[TestClass]
public class PasswordHasherTests : TestFixture
[TestMethod]
public void MyTestMethod()
{
var stopwatch = Stopwatch.StartNew();
[TestMethod]
public void MyTestMethod()
{
var stopwatch = Stopwatch.StartNew();
var salt = PasswordHash.ScryptGenerateSalt();
byte[] salt = PasswordHash.ScryptGenerateSalt();
// 2. Konverter password til byte[]
var passwordBytes = Encoding.UTF8.GetBytes("password123");
// 2. Konverter password til byte[]
byte[] passwordBytes = System.Text.Encoding.UTF8.GetBytes("password123");
// 3. Kald ScryptHashBinary korrekt
var hash = PasswordHash.ScryptHashBinary(
passwordBytes,
salt
);
// 3. Kald ScryptHashBinary korrekt
byte[] hash = PasswordHash.ScryptHashBinary(
password: passwordBytes,
salt: salt, // 32-byte array
limit: PasswordHash.Strength.Interactive,
outputLength: 32
);
stopwatch.Stop();
}
stopwatch.Stop();
}
[TestMethod]
public void HashPassword_ShouldCreateValidHashFormat()
{
// Arrange
string password = "TestPassword123";
[TestMethod]
public void HashPassword_ShouldCreateValidHashFormat()
{
// Arrange
var password = "TestPassword123";
// Act
string hashedPassword = new SecureTokenizer().TokenizeText(password);
string[] parts = hashedPassword.Split('.');
// Act
var hashedPassword = new SecureTokenizer().TokenizeText(password);
var parts = hashedPassword.Split('.');
// Assert
Assert.AreEqual(3, parts.Length);
Assert.AreEqual("100000", parts[0]);
}
// Assert
Assert.AreEqual(3, parts.Length);
Assert.AreEqual("100000", parts[0]);
}
[TestMethod]
public void VerifyPassword_WithCorrectPassword_ShouldReturnTrue()
{
// Arrange
string password = "TestPassword123";
string hashedPassword = new SecureTokenizer().TokenizeText(password);
[TestMethod]
public void VerifyPassword_WithCorrectPassword_ShouldReturnTrue()
{
// Arrange
var password = "TestPassword123";
var hashedPassword = new SecureTokenizer().TokenizeText(password);
// Act
bool result = new SecureTokenizer().VerifyToken(hashedPassword, password);
// Act
var result = new SecureTokenizer().VerifyToken(hashedPassword, password);
// Assert
Assert.IsTrue(result);
}
// Assert
Assert.IsTrue(result);
}
[TestMethod]
public void VerifyPassword_WithWrongPassword_ShouldReturnFalse()
{
// Arrange
string correctPassword = "TestPassword123";
string wrongPassword = "WrongPassword123";
string hashedPassword = new SecureTokenizer().TokenizeText(correctPassword);
[TestMethod]
public void VerifyPassword_WithWrongPassword_ShouldReturnFalse()
{
// Arrange
var correctPassword = "TestPassword123";
var wrongPassword = "WrongPassword123";
var hashedPassword = new SecureTokenizer().TokenizeText(correctPassword);
// Act
bool result = new SecureTokenizer().VerifyToken(hashedPassword, wrongPassword);
// Act
var result = new SecureTokenizer().VerifyToken(hashedPassword, wrongPassword);
// Assert
Assert.IsFalse(result);
}
// Assert
Assert.IsFalse(result);
}
[TestMethod]
public void VerifyPassword_WithInvalidHashFormat_ShouldReturnFalse()
{
// Arrange
string password = "TestPassword123";
string invalidHash = "InvalidHash";
[TestMethod]
public void VerifyPassword_WithInvalidHashFormat_ShouldReturnFalse()
{
// Arrange
var password = "TestPassword123";
var invalidHash = "InvalidHash";
// Act
bool result = new SecureTokenizer().VerifyToken(invalidHash, password);
// Act
var result = new SecureTokenizer().VerifyToken(invalidHash, password);
// Assert
Assert.IsFalse(result);
}
// Assert
Assert.IsFalse(result);
}
}