Dev.to WebDev 🛠 Dev 👁 0 📖 4 min read

Implementing HIPAA Safe Harbor (45 CFR § 164.514(b)(2)) CSV De-Identification 100% Client-Side in Vanilla JS

If you have ever tried to help a hospital lab, MD/PhD student, or clinical data team share a research CSV with an external collaborator or journal repository, you know the catch-22: Cloud SaaS de-identifiers are legal

Implementing HIPAA Safe Harbor (45 CFR § 164.514(b)(2)) CSV De-Identification 100% Client-Side in Vanilla JS

If you have ever tried to help a hospital lab, MD/PhD student, or clinical data team share a research CSV with an external collaborator or journal repository, you know the catch-22:

  1. Cloud SaaS de-identifiers are legally off-limits without a signed Business Associate Agreement (BAA) and institutional IT review.
  2. Quick Excel formulas (=LEFT(A2, 3)) and ad-hoc Pandas scripts quietly violate HIPAA Safe Harbor on subtle regulatory edge cases.

Because the browser already has a fast JS engine, FileReader, and native crypto.subtle (WebCrypto SHA-256), we can run the entire HIPAA Safe Harbor 18-identifier transformation 100% locally in browser RAM — zero backend, zero uploads, verifiable by unplugging Wi-Fi or watching the Chrome DevTools Network tab.

Here are the four engineering subtleties that actually trip people up when implementing 45 CFR § 164.514(b)(2) in code, plus a live offline tool you can test in your browser right now.

1. Why zip.slice(0, 3) Violates HIPAA Safe Harbor

Under 45 CFR § 164.514(b)(2)(i)(B), you may retain the first 3 digits of a US ZIP code only if the combined geographic unit formed by all ZIP codes sharing those 3 digits contains more than 20,000 people (per Bureau of the Census data).

There are 17 restricted 3-digit ZIP prefixes whose combined population is ≤ 20,000, and all 17 MUST be changed to 000 — not kept as 3 digits:

const RESTRICTED_ZIP3_SET = new Set([
  '036', '059', '063', '102', '203', '556',
  '692', '790', '821', '823', '830', '831',
  '878', '879', '884', '890', '893',
]);

export function transformZipCell(raw) {
  const digits = String(raw ?? '').trim().replace(/[^0-9]/g, '');
  if (!digits) return '';
  // Recover leading zero stripped by Excel (e.g., 3601 -> 03601)
  const zip5 = digits.length === 4 ? `0${digits}` : digits.slice(0, 5);
  if (zip5.length < 3) return '000';
  const prefix3 = zip5.slice(0, 3);
  return RESTRICTED_ZIP3_SET.has(prefix3) ? '000' : prefix3;
}

Notice the second real-world bug that snippet fixes: Excel routinely strips leading zeros from East Coast ZIPs (03601 becomes 3601). If you do not left-pad 4-digit US ZIP integers before checking RESTRICTED_ZIP3_SET, 036 slips right through!

2. Ages > 89 (Including Decimal Ages & Birth Years)

Under 45 CFR § 164.514(b)(2)(i)(C), all ages over 89 and all elements of dates (including birth year) indicative of such age must be aggregated into a single category of 90+.

  • 89 -> stays 89 (boundary case!)
  • 89.5, 90, 93 -> 90+
  • DOB = 1933-04-12 (in reference year 2026, implied age = 93) -> cannot even keep 1933; must be capped to 90+!

3. Longitudinal Date Shifting Without Breaking Survival Analysis

In year_only mode (2024-02-10 -> 2024), you satisfy strict Safe Harbor, but clinical studies often lose length-of-stay (LOS) and 30-day readmission intervals.

To preserve exact inter-visit intervals within a cohort (when permitted by a Limited Data Set or approved protocol), compute a deterministic per-subject shift offset from (studySalt, subjectId) using a non-zero integer in [-range, +range]:

// Deterministic FNV-1a 32-bit hash -> consistent non-zero shift per MRN
function computeSubjectShiftDays(subjectKey, salt, rangeDays = 180) {
  const input = `${salt}::${subjectKey}`;
  let h = 0x811c9dc5;
  for (let i = 0; i < input.length; i++) {
    h ^= input.charCodeAt(i);
    h = Math.imul(h, 0x01000193) >>> 0;
  }
  const span = rangeDays * 2; // excludes 0
  const idx = h % span;
  return idx < rangeDays ? idx - rangeDays : idx - rangeDays + 1;
}

Every encounter row belonging to MRN-401822 shifts by the exact same number of days, so discharge_date - admit_date and visit_2 - visit_1 remain mathematically invariant!

And crucially: export the pseudonymization lookup (MRN-401822 -> SUBJ_0001) as a physically separate file (reid_key_map_DO_NOT_SHARE.csv) excluded from the shareable bundle by default, following 45 CFR § 164.514(c).

4. Verifiable WebCrypto SHA-256 IRB Audit Certificates

Journal data-availability checks and university IRBs increasingly ask how a dataset was de-identified. Using native crypto.subtle.digest('SHA-256', bytes) in the browser lets you stamp an audit report with:

  • Exact SHA-256 fingerprint of the raw input CSV
  • Exact SHA-256 fingerprint of the de-identified output CSV (verifiable later with shasum -a 256)
  • Column-by-column action manifest + exact counts of restricted 17-ZIP cells zeroed, ages >89 capped, and free-text PHI tokens scrubbed

Try It Offline in Your Browser (Zero Sign-Up)

I packaged this entire vanilla-JS engine (plus a built-in 16-row synthetic clinical cohort benchmark and a zero-dependency Python 3 CLI with bit-for-bit shift parity) into SafeHarbor Studio:

SafeHarbor Studio Offline HIPAA Demo

Disclaimer: SafeHarbor Studio is an offline technical de-identification utility and does not constitute legal advice. Researchers remain responsible for the HIPAA "no actual knowledge" prong and institutional IRB policies.

Would love feedback from anyone who has wrestled with clinical CSV de-identification, messy Excel date formats, or free-text note scrubbing in the browser!

📰 Read the original article on Dev.to WebDev

Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.