Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: added support for truncating crypto-hashed values to a max length #207

Merged
merged 2 commits into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
"program": "${workspaceFolder}/src/FhirPseudonymizer/bin/Debug/net7.0/FhirPseudonymizer.dll",
"program": "${workspaceFolder}/src/FhirPseudonymizer/bin/Debug/net8.0/FhirPseudonymizer.dll",
"args": [],
"cwd": "${workspaceFolder}/src/FhirPseudonymizer",
"stopAtEntry": false,
Expand Down
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,27 @@ fhirPathRules:
| `entici__Auth__OAuth__Scope` | The scope | `""` |
| `entici__Auth__OAuth__Resource` | The resource | `""` |

### Truncating Crypto-hash Length

When using the `cryptoHash` method on a value, the result is a hex-encoded string of 64 characters length.
You can truncate this to a specific maximum length using the `truncateToMaxLength` setting. For example:

```yaml
fhirPathRules:
- path: Resource.id
method: cryptoHash
truncateToMaxLength: 32
```

Will truncate the usually 64-character-long hash to the following:

```json
{
"resourceType": "Patient",
"id": "b43a73c44e6d5b57644b63d89ee90cbf"
}
```

## Dynamic rule settings

Anonymization and pseudonymization rules in the `anonymization.yaml` config file can be overridden and/or extended on a per request basis.
Expand Down
44 changes: 41 additions & 3 deletions src/FhirPseudonymizer.Tests/CryptoHashProcessorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,32 @@ public static IEnumerable<object[]> GetProcessData()
yield return new object[]
{
new FhirString("12345"),
"098fe201710ca56e73dfb56cb0c610a66900add818c6d625b44b91eaafe79022"
"098fe201710ca56e73dfb56cb0c610a66900add818c6d625b44b91eaafe79022",
};
yield return new object[]
{
new ResourceReference("Patient/12345"),
"Patient/098fe201710ca56e73dfb56cb0c610a66900add818c6d625b44b91eaafe79022"
"Patient/098fe201710ca56e73dfb56cb0c610a66900add818c6d625b44b91eaafe79022",
};
yield return new object[]
{
new FhirUri("Patient/12345"),
"Patient/098fe201710ca56e73dfb56cb0c610a66900add818c6d625b44b91eaafe79022"
"Patient/098fe201710ca56e73dfb56cb0c610a66900add818c6d625b44b91eaafe79022",
};
}

public static IEnumerable<object[]> GetTruncatedProcessData()
{
yield return new object[] { new FhirString("12345"), "098fe201710ca56e73dfb56cb0c610a6" };
yield return new object[]
{
new ResourceReference("Patient/12345"),
"Patient/098fe201710ca56e73dfb56cb0c610a6",
};
yield return new object[]
{
new FhirUri("Patient/12345"),
"Patient/098fe201710ca56e73dfb56cb0c610a6",
};
}

Expand All @@ -43,4 +58,27 @@ public void Process_HashesIdPart(DataType element, string expected)

node.Value.ToString().Should().Be(expected);
}

[Theory]
[MemberData(nameof(GetTruncatedProcessData))]
public void Process_WithTruncatedHashLengthSet_HashHasMaxLength(
DataType element,
string expected
)
{
var processor = new CryptoHashProcessor("test");

var node = ElementNode.FromElement(element.ToTypedElement());
while (!node.HasValue())
{
node = node.Children().CastElementNodes().First();
}

processor.Process(
node,
settings: new Dictionary<string, object>() { { "truncateToMaxLength", 32 } }
);

node.Value.ToString().Should().Be(expected);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
fhirVersion: R4
fhirPathRules:
- path: Resource.id
method: cryptoHash
truncateToMaxLength: 16
- path: nodesByType('Reference').reference
method: cryptoHash
truncateToMaxLength: 16
- path: Bundle.entry.fullUrl
method: cryptoHash
truncateToMaxLength: 16
- path: Bundle.entry.request.where(method = 'PUT').url
method: cryptoHash
truncateToMaxLength: 16
parameters:
cryptoHashKey: fhir-pseudonymizer
Loading
Loading