scrub.lua

Verified
Added by iNTERFACEWARE

The “scrub” module given below redacts sensitive information from HL7 messages.

Source Code
local scrub={}

function scrub.scrub(msg)
   local Letter = 'A'
   local function redact_number(s)
      -- Replace numbers with random values; keep digit count.
      local max = 10.0 ^ #s
      local min = max / 10.0
      return math.floor(math.random() * (max - min) + min)
   end
   local function redact_word(s)
      -- Replace text with Chars.
      if Letter == 'z' then Letter = 'A' end
      Letter = string.char(Letter:byte(1) +1)
      return string.rep(Letter, #s)
   end
   -- Go line by line.
   return (msg:gsub('[^\r\n]+', function(line)
            if line:match('^MSH') or line:match('^EVN') then
               -- Keep MSH and EVN segments as-is.
               return line
            else
               -- Scrub all but the segment name.
               return line:sub(1,4) ..
               line:sub(5):gsub('%d+', redact_number)
               :gsub('[%a\128-\255]+', redact_word)
            end
         end))
end
return scrub
Description
The “scrub” module given below redacts sensitive information from HL7 messages.
Attachments
Usage Details

The “scrub” module given below redacts sensitive information from HL7 messages.  For simplicity, it considers anything not in the MSH or EVN segments to be sensitive.  Text is replaced with Xs, and numbers are randomized, while punctuation is preserved. This lets each message retain its shape, while preventing patient data from being exposed.

How to use scrub.lua:

  • Add it to your shared modules probably in a Filter component
  • Use the scrub.scrub() function to remove sensitive data