Skip to content

Commit

Permalink
feat: added uuid4 pseudonym generator (#62)
Browse files Browse the repository at this point in the history
  • Loading branch information
chgl authored Jun 28, 2023
1 parent d8e9b02 commit 2f856da
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/Vfps.Benchmarks/PseudonymGeneratorBenchmarks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,16 @@ public class PseudonymGeneratorBenchmarks
{
private readonly CryptoRandomBase64UrlEncodedGenerator crbase64Generator = new();
private readonly HexEncodedSha256HashGenerator sha256HashGenerator = new();
private readonly Uuid4Generator uuid4Generator = new();

[Benchmark]
public string CryptoRandomBase64UrlEncodedGenerator() =>
crbase64Generator.GeneratePseudonym("test", 16);
crbase64Generator.GeneratePseudonym("test", 64);

[Benchmark]
public string HexEncodedSha256HashGenerator() =>
sha256HashGenerator.GeneratePseudonym("test", 64);

[Benchmark]
public string Uuid4HashGenerator() => uuid4Generator.GeneratePseudonym("test", 36);
}
23 changes: 23 additions & 0 deletions src/Vfps.Tests/PseudonymGeneratorTests/Uuid4GeneratorTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using Vfps.PseudonymGenerators;

namespace Vfps.Tests.PseudonymGeneratorTests;

public class Uuid4GeneratorTests
{
private readonly Uuid4Generator sut = new();

[Theory]
[InlineData("test")]
[InlineData("longer Value with an Emoji 👷‍♂️")]
public void GeneratePseudonym_WithGivenInput_ShouldGenerateExpectedPseudonyms(string input)
{
var generated = sut.GeneratePseudonym(input);
generated.Should().HaveLength(36);
}

[Fact]
public void GeneratePseudonym_WithLengthOtherThan36_ShouldThrowException()
{
sut.Invoking(s => s.GeneratePseudonym("test", 64)).Should().Throw<ArgumentException>();
}
}
2 changes: 2 additions & 0 deletions src/Vfps/Protos/vfps/api/v1/namespaces.proto
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ enum PseudonymGenerationMethod {
PSEUDONYM_GENERATION_METHOD_SECURE_RANDOM_BASE64URL_ENCODED = 1;
// SHA256-encodes the original value and uses the hex-encoded hash as the pseudonym.
PSEUDONYM_GENERATION_METHOD_SHA256_HEX_ENCODED = 2;
// The method creates a Version 4 Universally Unique Identifier (UUID) as described in RFC 4122, Sec. 4.4.
PSEUDONYM_GENERATION_METHOD_UUID4 = 3;
}

// request to create a new namespace
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public PseudonymizationMethodsLookup()
new CryptoRandomBase64UrlEncodedGenerator()
},
{ PseudonymGenerationMethod.Sha256HexEncoded, new HexEncodedSha256HashGenerator() },
{ PseudonymGenerationMethod.Uuid4, new Uuid4Generator() }
};
}

Expand Down
17 changes: 17 additions & 0 deletions src/Vfps/PseudonymGenerators/Uuid4Generator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
namespace Vfps.PseudonymGenerators;

public class Uuid4Generator : IPseudonymGenerator
{
public string GeneratePseudonym(string originalValue, uint pseudonymLength = 36)
{
if (pseudonymLength != 36)
{
throw new ArgumentOutOfRangeException(
nameof(pseudonymLength),
$"When using the {nameof(Uuid4Generator)}, the pseudonym length must be set to 36."
);
}

return Guid.NewGuid().ToString();
}
}

0 comments on commit 2f856da

Please sign in to comment.