Skip to content

Instantly share code, notes, and snippets.

@tenderlove
Last active Feb 5, 2022
Embed
What would you like to do?
# 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

tenderlove commented Jan 4, 2022

@sorah fixed! Thank you!

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