Skip to content

Commit

Permalink
feat: added support for truncating crypto-hashed values to a max leng…
Browse files Browse the repository at this point in the history
…th (#207)

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

* utf8bom -> utf8 & NL
  • Loading branch information
chgl authored Oct 15, 2024
1 parent e5c3b47 commit 54dae1e
Show file tree
Hide file tree
Showing 7 changed files with 32,101 additions and 13 deletions.
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

0 comments on commit 54dae1e

Please sign in to comment.