Skip to content

[DICOM] Constrain cryptoHash output to the target VR's length and character repertoire - #268

Draft
Josiah Vinson (jovinson-ms) with Copilot wants to merge 3 commits into
masterfrom
copilot/ensure-crypto-hash-output-respects-dicom-vr-length
Draft

[DICOM] Constrain cryptoHash output to the target VR's length and character repertoire#268
Josiah Vinson (jovinson-ms) with Copilot wants to merge 3 commits into
masterfrom
copilot/ensure-crypto-hash-output-respects-dicom-vr-length

Conversation

Copilot AI commented Aug 13, 2026

Copy link
Copy Markdown
Contributor

cryptoHash wrote the raw 64-character SHA-256 hex digest into string elements, which violates bounded VRs (e.g. SH allows 16 characters) and made output reconstruction fail with FellowOakDicom.DicomValidationException from DicomValidation.ValidateSH.

{"tag": "PatientTelephoneNumbers", "method": "cryptohash"}   // SH

Before: 2e7acefff0307262cef6f503fa7019257f3f9d47fc987fb2c5a31ae4f4d3c022 (64 chars → validation error)
After: 2e7acefff0307262 (16 chars)

Changes

  • CryptoHashProcessor: hashed string values are now conformed to the target VR before being written:
    • truncated to DicomVR.MaximumLength (SH/AE/CS/DS → 16, UI/LO/PN → 64; VRs without a limit such as UC/UT/UR keep the full digest). IS is further capped at 9 digits so the value always fits a 32-bit signed integer.
    • mapped to the VR's character repertoire: digits only for DS/IS/UI (leading zero avoided), uppercase for CS. Output stays deterministic for a given input, key and VR.
  • Error reporting: if the generated value is still rejected (fixed-structure VRs AS/DA/DT/TM), an AnonymizerOperationException is raised naming only the tag and VR. The DicomValidationException is deliberately not chained, since it embeds the offending content.
  • Tests: per-VR expected values for AE, CS, DS, IS, SH, UI; explicit SH 16-character boundary case; determinism and matchInputStringLength cases; fixed-format VR case asserting the source value does not appear in the message. The DicomResults/Invalid-String-Format.dcm engine fixture was regenerated for the shorter outputs.
  • Docs: docs/DICOM-anonymization.md describes the per-VR constraints, notes that truncation reduces collision resistance to the retained characters (64 bits for 16 hex chars), and points to dateShift/redact for AS/DA/DT/TM.

Notes for reviewers

  • Truncation is the chosen tradeoff over rejecting bounded VRs outright; the alternative (encoding the digest more densely, e.g. base32) would still be truncated and would break existing output stability.
  • Behavior change: cryptoHash output for bounded VRs differs from previous releases, so re-anonymizing the same source data produces different values than before.

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
There may be pipelines that require an authorized user to comment /azp run to run.

Copilot AI and others added 2 commits August 13, 2026 20:08
Co-authored-by: jovinson-ms <88204686+jovinson-ms@users.noreply.github.com>
… docs

Co-authored-by: jovinson-ms <88204686+jovinson-ms@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix crypto hash output to respect DICOM VR length limits [DICOM] Constrain cryptoHash output to the target VR's length and character repertoire Aug 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[DICOM] Ensure crypto hash output respects DICOM VR length limits

2 participants