Skip to content

Instantly share code, notes, and snippets.

@tenderlove
Last active February 5, 2022 04:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tenderlove/6618a83cec6e28b4880ca804b375bd33 to your computer and use it in GitHub Desktop.
Save tenderlove/6618a83cec6e28b4880ca804b375bd33 to your computer and use it in GitHub Desktop.
# Smart Health Card decoder
# This decodes the SHC url thing that is stored in smart health card QR codes
str = DATA.readline
require "base64"
require "zlib"
require "json"
require "pp"
payload = str.delete_prefix("shc:/").scan(/\d\d/).map { |x| (x.to_i + 45).chr }.join
payload = payload.split(".").map { |x|
pad = x.bytesize % 4
Base64.urlsafe_decode64("#{x}#{pad == 0 ? nil : '=' * (4-pad)}")
}
pp JSON.load payload.first
zlib = Zlib::Inflate.new(-15)
pp JSON.load zlib.inflate payload[1]
__END__
shc:/1234...
Decode your health card QR code and paste the shc "URL" above
@sorah
Copy link

sorah commented Dec 23, 2021

present code attempted to add ==== as a padding when x.bytesize % 4 is zero...

payload = payload.split(".").map { |x|
  pad = x.bytesize % 4
  Base64.urlsafe_decode64("#{x}#{pad == 0 ? nil : '=' * (4-pad)}")
}

@tenderlove
Copy link
Author

@sorah fixed! Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment