def test_decode_encoded(self): random.seed(42) for i in range(142): binary = bytes([ random.randint(0, 255) for _ in range(random.randint(0, 142)) ]) self.assertEqual(binary, b45decode(b45encode(binary)))
def verify_and_decode(self, eudcc: bytes) -> Union[Dict[int, Any], bytes]: if eudcc.startswith(b"HC1:"): # Decode Base45 data. eudcc = b45decode(eudcc[4:]) # Decompress with zlib. eudcc = zlib.decompress(eudcc) # Verify and decode CWT. return cwt.decode(eudcc, keys=self._dscs)
def decrypt_contact(scan): # Base45 decode + RSA OAEP SHA256 decrypt # MurmurHash3 to compute ID for contact details msg = b45decode(scan) contact = cipher.decrypt(msg) # print('\tBase45: {}\n\tContact: {}'.format(scan, contact)) h = mmh3.hash(contact) return h, contact.decode()
def decode_and_decompress(data: bytes) -> bytes: decoded_data = base45.b45decode(data) decompressed_data = zlib.decompress(decoded_data) logger.debug( "Uncompressed data: %d bytes, %s", len(decompressed_data), binascii.hexlify(decompressed_data).decode(), ) return decompressed_data
def base45_d(txt): import base45 return base45.b45decode(txt)
import base64 import base45 import zlib import cbor2 b45data = "NCF 50.G0/3WUWGSLKH47GO0:S4KQDITFAUO8CKR7U*70YM8FN0%XCI7GWY0LCCK63D97TK0F90GECQHGWJC0FDL:4:KEPH7M/ESDD746KG7+59-F61B8746:X8//6-R8:A8 47HA7WA77:6+96WJCT3EYM8XJCS.CNF6OF63W5Q47%96XJC/$ENF6PF63W5Y96UF6ZJC+KENF6OF63W5Q47746%JC+QE$.32%E6VCHQEU$DE44NXOBJE719$QE0/D+8D-ED.24-G8$:84KCD3DX47B46IL6646I*6GVC*JC1A6G%63W56L6-96TPCBEC7ZKW.CI C14EPQE JC2/DB190%EMEDLPCG/DI CC1A NAMPCG/D%-C8C9:S9:B8O/EZKEZ967L6256V50X-K+0B3N81UELEN6RR-W7%YGO%54BIFORQ49 IEC56S*V%2S6P2*P6SJ4Q/8TMCUM01%3+922OGE$5IUTJCJOG0K5GMPMC*V21DEB67 SJ8R%KCZNAEX3P-4DOL:A6VEUA9H0NAMDBLFWJM4GGCBZ1F.1K3HWCM-RKBMKP:J+CH$30B*OW8P9FCO-D$ZCR19AEP6O0W192%O3ULI:6TUR5-NAC3TBD1GJ%JVZ:06EBJW7*Y9+JT-11K.K.W3BQDQ$31XQ%JL5%K0GDVW14LBLRURSAI143/85KM D7HMET7Q9-GOFDQRJ%G8H$N1Y8JNQB:7TD6Z.F/TPGN2VKG-MLS:LP6030VK6Q*KC5%P+Z9DPBESMJV6Y34M9M/S0DP98.JUNP" # message_bytes = base64.b64decode(base64_bytes) # message = message_bytes.decode('ascii') # print(message) zlibdata = base45.b45decode(b45data) cbordata = zlib.decompress(zlibdata) decoded = cbor2.loads(cbordata) print(cbor2.loads(decoded.value[2]))
def eventFilter(self, obj, event): var = 2 today = datetime.today().date() # getting today's date def validity(j_whole, var, date_from, vall): # Forming the date to check by adding the days from the .ini file to the date from the cert if var == 1: Date = datetime.fromtimestamp(j_whole['4']).date() elif var != 1: Date = date_from + relativedelta(months=vall) # Comparing the date of expire against today if today < Date: # Valid cert os.system( '@echo off && chcp 65001>nul && start /b /wait MessageBox.exe "The certificate is valid!" ' '"Information">nul') else: # Invalid cert os.system( '@echo off && chcp 65001>nul && start /b /wait MessageBox.exe "The certificate is invalid!" ' '"Attention!" /i:E>nul') if event.type() == QEvent.KeyPress: if event.key() == Qt.Key_Escape: self.close() """ if event.key() == Qt.Key_Enter: # print("Enter is pressed!") payload = self.lineEdit.text() payload = payload[4:] try: decoded = base45.b45decode(payload) except: exception() reset() and sys.exit() # decompress using zlib decompressed = zlib.decompress(decoded) # decode COSE message (no signature verification done) cose = CoseMessage.decode(decompressed) # decode the CBOR encoded payload converting the information to readable json struct whole = ( json.dumps(cbor2.loads(cose.payload), ensure_ascii=False, indent=2, sort_keys=True, default=str)) j_whole = json.loads(whole) dick = j_whole['-260']['1'] # the dic with the needed information for the funcs date_from = datetime.fromtimestamp(j_whole['6']).date() # date issued/valid from self.placeholder_native_name.setText() """ if event.key() == Qt.Key_Return: print("Return is pressed!") payload = self.lineEdit.text() payload = payload[4:] try: decoded = base45.b45decode(payload) except: exception() reset() and sys.exit() # decompress using zlib decompressed = zlib.decompress(decoded) # decode COSE message (no signature verification done) cose = CoseMessage.decode(decompressed) # decode the CBOR encoded payload converting the information to readable json struct whole = (json.dumps(cbor2.loads(cose.payload), ensure_ascii=False, indent=2, sort_keys=True, default=str)) j_whole = json.loads(whole) dick = j_whole['-260'][ '1'] # the dic with the needed information for the funcs date_from = datetime.fromtimestamp( j_whole['6']).date() # date issued/valid from for k, v in dick.items(): if k == "r": # recovery if v is None: # if empty will continue to the next sub-dic continue if j_whole[ '1'] == "BG": # In BG this cert is 365 days, not 180. For now... val = open("val_rec_bg.ini", "r") # Opens it in read mode vall = list(val) # Converting data to list vall = int( vall[0] ) # Taking the first (and only) item and convert it to int val.close() # Closes the file elif j_whole['1'] != "BG": val = open("val_rec.ini", "r") # Opens it in read mode vall = list(val) # Converting data to list vall = int( vall[0] ) # Taking the first (and only) item and convert it to int val.close() # Closes the file self.placeholder_native_name.setText( str(j_whole['-260']['1']['nam']['gn']) + " " + str(j_whole['-260']['1']['nam']['fn'])) self.placeholder_en_name.setText( str(j_whole['-260']['1']['nam']['gnt']) + " " + str(j_whole['-260']['1']['nam']['fnt'])) self.placeholder_date_issued.setText( str(datetime.fromtimestamp(j_whole['6']).date())) self.placeholder_valid_until.setText( str(datetime.fromtimestamp(j_whole['4']).date())) self.placeholder_cert_id.setText( str(j_whole['-260']['1']['r'][0]['ci'])) for K, V in CO.items(): if j_whole['1'] == K: c = V elif len(j_whole['1']) > 2: c = j_whole['1'] self.placeholder_CO.setText(str(c)) self.cert_type.setText("Recovery certificate") break elif k == "v": # vaccine if v is None: continue val = open("val_vac.ini", "r") vall = list(val) vall = int(vall[0]) val.close() self.placeholder_native_name.setText( str(j_whole['-260']['1']['nam']['gn']) + " " + str(j_whole['-260']['1']['nam']['fn'])) self.placeholder_en_name.setText( str(j_whole['-260']['1']['nam']['gnt']) + " " + str(j_whole['-260']['1']['nam']['fnt'])) self.placeholder_date_issued.setText( str(datetime.fromtimestamp(j_whole['6']).date())) self.placeholder_valid_until.setText( str(datetime.fromtimestamp(j_whole['4']).date())) self.placeholder_cert_id.setText( str(j_whole['-260']['1']['v'][0]['ci'])) for K, V in CO.items(): if j_whole['1'] == K: c = V elif len(j_whole['1']) > 2: c = j_whole['1'] self.placeholder_CO.setText(str(c)) self.cert_type.setText("Vaccination certificate") break elif k == "t": # test cert if v is None: continue var = 1 vall = "pft" self.placeholder_native_name.setText( str(j_whole['-260']['1']['nam']['gn']) + " " + str(j_whole['-260']['1']['nam']['fn'])) self.placeholder_en_name.setText( str(j_whole['-260']['1']['nam']['gnt']) + " " + str(j_whole['-260']['1']['nam']['fnt'])) self.placeholder_date_issued.setText( str(datetime.fromtimestamp(j_whole['6']).date())) self.placeholder_valid_until.setText( str(datetime.fromtimestamp(j_whole['4']).date())) self.placeholder_cert_id.setText( str(j_whole['-260']['1']['t'][0]['ci'])) for K, V in CO.items(): if j_whole['1'] == K: c = V elif len(j_whole['1']) > 2: c = j_whole['1'] self.placeholder_CO.setText(str(c)) self.cert_type.setText("Test certificate") validity(j_whole, var, date_from, vall) return 1 #reset() return super(MyMainWindow, self).eventFilter(obj, event)
def test_decode_bad(self): with self.assertRaises(TypeError): b45decode(42) for v in BAD_BASE45_STRINGS: with self.assertRaises(ValueError): b45decode(v)
def test_decode_good(self): for binary_ref, encoded_ref in GOOD_DATA: binary_res = b45decode(encoded_ref) self.assertEqual(binary_res, binary_ref)
def _get_compressed(base45_code): # noinspection PyBroadException try: return hexlify(b45decode(base45_code)).decode() except Exception: return None
if infile: if open(infile, "rb").read(4) == b"%PDF": # extract QR code from PDF using hard-coded index, size and bit depth. # This will only work with the official Austrian green pass PDFs. pdf = PyPDF2.PdfFileReader(open(infile, "rb")) qr_img = pdf.getPage(0)['/Resources']['/XObject']['/Im3'] qr_pil = Image.frombytes("1", (400, 400), qr_img.getData()) else: # assume image qr_pil = Image.open(infile) # decode QR code into raw bytes: qr_data_zlib_b45 = pyzbar.decode(qr_pil)[0].data # strip header ('HC1:') and decompress data: qr_data_zlib = base45.b45decode(qr_data_zlib_b45[4:]) # decompress: qr_data = zlib.decompress(qr_data_zlib) # decode cose document: (_, (headers1, headers2, cbor_data, signature)) = flynn.decoder.loads(qr_data) # decode cbor-encoded payload: data = flynn.decoder.loads(cbor_data) def date(ts): return datetime.utcfromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S') print("QR Code Issuer :", data[1]) print("QR Code Expiry :", date(data[4]))
def decode_certificate(encoded): version = encoded[0:4] if version != b'HC1:': return None compressed = encoded[4:] return zlib.decompress(b45decode(compressed))
def run(self, text): import base45 return base45.b45decode(str.encode(text)).decode( 'utf-8', errors="surrogateescape")