Skip to content

Instantly share code, notes, and snippets.

@julbouln
Created July 17, 2021 20:42
Show Gist options
  • Save julbouln/8d18ac5d04b56aebe7eea6b06dd36604 to your computer and use it in GitHub Desktop.
Save julbouln/8d18ac5d04b56aebe7eea6b06dd36604 to your computer and use it in GitHub Desktop.
extraction des données du passe sanitaire incluses dans le QR-code UE
require 'uri'
require 'base64'
require 'cgi'
require 'zlib'
require 'pp'
require 'cbor'
# https://github.com/svnee/base54
module Base45
class Error < StandardError; end
CHARSET = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ $%*+-./:'
def self.encode(input)
raise Error unless input.is_a? String
bytes = input.bytes
result = ''
(0..bytes.length-1).step(2).each do |i|
if bytes.length - i > 1
x = (bytes[i] << 8) + bytes[i + 1]
e, x = x.divmod 45 * 45
d, c = x.divmod 45
result += CHARSET[c] + CHARSET[d] + CHARSET[e]
else
x = bytes[i]
d, c = x.divmod 45
result += CHARSET[c] + CHARSET[d]
end
end
result
end
def self.decode(input)
raise Error unless input.is_a? String
return [] if input.length.zero?
begin
result = []
buf = input.chars.map { |c| CHARSET.index(c) }
(0..buf.length).step(3).each do |i|
x = buf[i].to_i + buf[i + 1].to_i * 45
if buf.length - i >= 3
x = buf[i] + buf[i + 1] * 45 + buf[i + 2] * 45 * 45
x.divmod(256).each { |e| result << e }
else
result << x
end
end
result
rescue
raise Error
end
end
end
data = File.read("passsani.txt")
data = data.sub(/HC1\:/,"")
dec = Base45.decode(data).pack("C*")
begin
dec = Zlib::Inflate.inflate(dec)
rescue
# not zlib
end
cose_data = CBOR.decode(dec)
cose_value = cose_data.value
phdrs_data, _uhdrs, cose_payload, _signers = cose_value
phdrs = CBOR.decode(phdrs_data);
raw_kid = phdrs[4]
cbor_data = CBOR.decode(cose_payload)
hcert=cbor_data[-260][1]
pp hcert
data = {}
data[:firtname] = hcert["nam"]["gn"]
data[:lastname] = hcert["nam"]["fn"]
data[:birthday] = hcert["dob"]
data[:vaccination_date] = hcert["v"][0]["dt"]
data[:prophylactic_agent] = hcert["v"][0]["vp"]
data[:doses_received] = hcert["v"][0]["dn"]
data[:doses_expected] = hcert["v"][0]["sd"]
pp data
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment