def test_b85_padding(self): eq = self.assertEqual eq(base64.b85encode(b"x", pad=True), b'cmMzZ') eq(base64.b85encode(b"xx", pad=True), b'cz6H+') eq(base64.b85encode(b"xxx", pad=True), b'czAdK') eq(base64.b85encode(b"xxxx", pad=True), b'czAet') eq(base64.b85encode(b"xxxxx", pad=True), b'czAetcmMzZ') eq(base64.b85decode(b'cmMzZ'), b"x\x00\x00\x00") eq(base64.b85decode(b'cz6H+'), b"xx\x00\x00") eq(base64.b85decode(b'czAdK'), b"xxx\x00") eq(base64.b85decode(b'czAet'), b"xxxx") eq(base64.b85decode(b'czAetcmMzZ'), b"xxxxx\x00\x00\x00")
def b85encode(data): """ Encode binary data to ascii text in base85. Data must be bytes. """ if PY2: raise NotImplementedError("Python 2 can't encode data in base85.") return base64.b85encode(data).decode('ascii')
def store(self, key, value): """ Saves a value persistently to the database. """ # NOTE: value must not be <bytes> otherwise BOOM! and moreover our sqlite db always return strings as <str> entry = b85encode(json.dumps(value, ensure_ascii=False).encode()).decode() self.plugin.pyload.db.set_storage(self.plugin.classname, key, entry)
def test_b85encode(self): eq = self.assertEqual tests = { b'': b'', b'www.python.org': b'cXxL#aCvlSZ*DGca%T', bytes(range(255)): b"""009C61O)~M2nh-c3=Iws5D^j+6crX17#SKH9337X""" b"""AR!_nBqb&%[email protected]{EG;fCFflSSG&MFiI5|2yJUu=?KtV!7L`6nNNJ&ad""" b"""OifNtP*GA-R8>}2SXo+ITwPvYU}0ioWMyV&XlZI|Y;A6DaB*^Tbai%j""" b"""[email protected]>41ejE#<ukdcy;l$Dm3n3<ZJoSmMZprN9p""" b"""[email protected]|{(sHv)}tgWuEu(7hUw6(UkxVgH!yuH4^z`[email protected]#Kp$P$jQpf%+1cv""" b"""(9zP<)YaD4*xB0K+}+;a;Njxq<mKk)=;`[email protected]^!4`l`1$(#""" b"""{Qdp""", b"""abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ""" b"""[email protected]#0^&*();:<>,. []{}""": b"""VPa!sWoBn+X=-b1ZEkOHadLBXb#`}nd3r%[email protected][email protected](""" b"""Q&d$}S6EqEFflSSG&MFiI5{CeBQRbjDkv#CIy^osE+AW7dwl""", b'no padding..': b'[email protected]!Zf7no', b'zero compression\x00\x00\x00\x00': b'dS!BNAY*TBaB^jHb7^mG00000', b'zero compression\x00\x00\x00': b'dS!BNAY*TBaB^jHb7^mG0000', b"""Boundary:\x00\x00\x00\x00""": b"""LT`0$WMOi7IsgCw00""", b'Space compr: ': b'Q*dEpWgug3ZE$irARr(h', b'\xff': b'{{', b'\xff'*2: b'|Nj', b'\xff'*3: b'|Ns9', b'\xff'*4: b'|NsC0', } for data, res in tests.items(): eq(base64.b85encode(data), res) self.check_other_types(base64.b85encode, b"www.python.org", b'cXxL#aCvlSZ*DGca%T')
def dump_observation(observation, f): if 'cache' in observation: observation['cache'] = \ str(base64.b85encode(pickle.dumps(observation['cache'])), 'ascii') json.dump(observation, f) f.write("\n")
def proxy_sdns_query(self): req_bin, client_sock = self.request sdns_req = { "request_id": 1, "query": base64.b85encode(req_bin).decode("ascii"), "username": "******", "auth_token": "testtoken" } # TODO: using a new connection for each request; this is expensive... # TODO: real implementation would require certificate validation ssl_context = ssl.create_default_context() ssl_context.check_hostname = False ssl_context.verify_mode = ssl.CERT_NONE with ssl_context.wrap_socket(socket(AF_INET, SOCK_STREAM)) as sdns_sock: sdns_sock.connect((SDNS_HOST, SDNS_PORT)) # Append trailing newline to indicate the end of the request payload = bytes(json.dumps(sdns_req) + "\n", "ascii") print("Sending SDNS request: '{}'".format(payload)) sdns_sock.sendall(payload) sdns_resp = str(sdns_sock.recv(4096), "ascii") print("Got SDNS response:") print(str(sdns_resp)) try: parsed = json.loads(sdns_resp) if 'status' not in parsed or parsed['status'] != 0 or 'response' not in parsed: print("SDNS request failed") else: client_sock.sendto(base64.b85decode(parsed['response']), self.client_address) except json.JSONDecodeError as e: print("Error: JSON decode failed", e)
def sendsafe(self, data): """ Encode an arbitrary string into a safe form to pass to print().""" import base64 data = base64.b85encode(self.as_bytes(data)) return self.sendraw(data)
def serializer(x): # Serialize try: data = dumps(x) except Exception as ex: raise SerializationError(ex) # Transmit with b85 encode: safe characters and no newlines return (b'+' + base64.b85encode(data)).decode('ascii')
def installer(version=None, installer_path=_path(), template_path=os.path.join(PROJECT_ROOT, "template.py")): print( "[generate.installer] Generating installer {} (using {})".format( os.path.relpath(installer_path, PROJECT_ROOT), "pip" + version if version is not None else "latest" ) ) # Load our wrapper template with open(template_path, "r", encoding="utf8") as fp: WRAPPER_TEMPLATE = fp.read() # Get all of the versions on PyPI resp = urllib.request.urlopen("https://pypi.python.org/pypi/pip/json") data = json.loads(resp.read().decode("utf8")) versions = sorted(data["releases"].keys(), key=packaging.version.parse) # Filter our list of versions based on the given specifier s = packaging.specifiers.SpecifierSet("" if version is None else version) versions = list(s.filter(versions)) # Select the latest version that matches our specifier is latest = versions[-1] # Select the wheel file (we assume there will be only one per release) file_urls = [(x["url"], x["md5_digest"]) for x in data["releases"][latest] if x["url"].endswith(".whl")] assert len(file_urls) == 1 url, expected_hash = file_urls[0] # Fetch the file itself. data = urllib.request.urlopen(url).read() assert hashlib.md5(data).hexdigest() == expected_hash # Write out the wrapper script that will take the place of the zip script # The reason we need to do this instead of just directly executing the # zip script is that while Python will happily execute a zip script if # passed it on the file system, it will not however allow this to work if # passed it via stdin. This means that this wrapper script is required to # make ``curl https://...../get-pip.py | python`` continue to work. print("[generate.installer] Write the wrapper script with the bundled zip " "file") zipdata = base64.b85encode(data).decode("utf8") chunked = [] for i in range(0, len(zipdata), 79): chunked.append(zipdata[i : i + 79]) os.makedirs(os.path.dirname(installer_path), exist_ok=True) with open(installer_path, "w") as fp: fp.write(WRAPPER_TEMPLATE.format(version="" if version is None else version, zipfile="\n".join(chunked))) # Ensure the permissions on the newly created file oldmode = os.stat(installer_path).st_mode & 0o7777 newmode = (oldmode | 0o555) & 0o7777 os.chmod(installer_path, newmode) print("[generate.installer] Generated installer")
def generate_password(passphrase, resetCount, privateKeyHash, domain, allowSymbols, length): resetCountString = str(resetCount) if resetCount > 0 else '' secret = (passphrase + resetCountString + privateKeyHash).encode('utf-8') key = pbkdf2_hmac('sha512', secret, domain.encode('utf-8'), 100) if allowSymbols: return b85encode(key).decode('ascii')[:length] else: b64 = b64encode(key).decode('ascii') return re.sub(r'[\W_]+', '', b64).ljust(len(b64), '0')[:length]
def genCurveTweet(curveHash): tw = "Entropy for curve "+curveHash+" : " tw += createCE() + " " tw = tw[0:140] randlen = 140 - len(tw) randomS = str(base64.b85encode(os.urandom(randlen))) randomS = randomS[2:len(randomS) - 1] randomS = randomS[0:randlen] tw += randomS return tw
def tojson(self, obj): '''Hook for json.dump()''' onam = obj.__class__.__name__ m = { 'clsname': onam } if onam in ("bytes", "bytearray"): import base64 s = base64.b85encode( obj ).decode("ascii") o = Jsonator.JsonBytesWrapper( s, onam ) return o m.update(obj.__dict__) return m
def enc(s): labels = [] labels.append('s') # print(s) # print("l: ", s.__len__()) # print("..") b = str.encode(s, 'ascii') labels.append('b') # print(b) # print("l: ", b.__len__()) # print("..") b64 = base64.standard_b64encode(b) labels.append('b64') # print(b64) # print("l: ", b64.__len__()) # print("..") z = gzip.compress(b) labels.append('z') # print(z) # print("l: ", z.__len__()) # print("..") z64 = base64.standard_b64encode(z) labels.append('z64') # print(z64) # print("l: ", z64.__len__()) # print("..") z85 = base64.b85encode(z) labels.append('z85') # print(z85) # print("l: ", z85.__len__()) return ( ( s.__len__(), b.__len__(), b64.__len__(), z.__len__(), z64.__len__(), z85.__len__() ), labels )
def base85_digest(hash_): ''' Get base 85 encoded digest of hash Parameters ---------- hash_ : hash hashlib hash object. E.g. the return of hashlib.sha512() Returns ------- str base 85 encoded digest ''' return base64.b85encode(hash_.digest()).decode('ascii')
def send_invalid_message(self, envelope: Envelope) -> None: """ Handle an message that is invalid wrt a protocol. :param envelope: the envelope :return: None """ logger.warning("Invalid message wrt protocol: {}.".format( envelope.protocol_id)) encoded_envelope = base64.b85encode(envelope.encode()).decode("utf-8") reply = DefaultMessage( type=DefaultMessage.Type.ERROR, error_code=DefaultMessage.ErrorCode.INVALID_MESSAGE.value, error_msg="Invalid message.", error_data={"envelope": encoded_envelope}) self.context.outbox.put_message( to=envelope.sender, sender=self.context.agent_public_key, protocol_id=DefaultMessage.protocol_id, message=DefaultSerializer().encode(reply))
def test_decode_base85(self): if sys.version_info.major != 3 or \ sys.version_info.minor < 4: self.fail( 'Base85 support not available for the current Python version!') data_bytes = self._random_bytes() encoded_bytes = base64.b85encode(data_bytes) plugin = self._plugins.get_plugin_instance('base85') result = plugin.unprocess(encoded_bytes) self.assertIsNone( plugin.error), 'An error occurred during Base85 decoding' self.assertIsInstance( result, bytes, 'Base85 decoding result should be bytes or bytearray, ' 'got %s instead' % type(result)) self.assertEqual(data_bytes, result) data_str = self._random_str() self.assertRaises(TypeError, functools.partial(plugin.unprocess, data_str), 'Unexpected exception raised')
def onLoginSuccessed(self, uid, name): AppLog.debug('onLoginSuccessed') self._isLogin = False self.buttonLogin.showWaiting(False) self.setEnabled(True) # 用账号密码实例化github访问对象 account = self.lineEditAccount.text().strip() password = self.lineEditPassword.text().strip() Constants._Account = account Constants._Password = password Constants._Username = name # 储存账号密码 Setting.setValue('account', account) if account not in self._accounts: # 更新账号数组 self._accounts[account] = [ uid, base64.b85encode(password.encode()).decode() ] Setting.setValue('accounts', self._accounts) self.accept()
def test_long_file(self): # Setup packet = Packet('*****@*****.**', self.contact, ORIGIN_CONTACT_HEADER, FILE, self.settings) packet.long_active = True compressed = zlib.compress(os.urandom(10000), level=COMPRESSION_LEVEL) file_key = os.urandom(KEY_LENGTH) encrypted = encrypt_and_sign(compressed, key=file_key) encrypted += file_key encoded = base64.b85encode(encrypted) file_data = int_to_bytes(1000) + int_to_bytes(10000)+ b'testfile.txt' + US_BYTE + encoded packets = split_to_assembly_packets(file_data, FILE) for p in packets: packet.add_packet(p) # Test self.assertIsNone(packet.assemble_and_store_file()) self.assertTrue(os.path.isfile(f'{DIR_RX_FILES}Alice/testfile.txt')) self.assertEqual(os.path.getsize(f'{DIR_RX_FILES}Alice/testfile.txt'), 10000)
def eatCookie(cookie: str) -> Union[Any, None]: """Pass in a string generated from makeCookie to get the original data, with full error checking to ensure that the data wasn't tampered with in transit.""" pkey = cookie[:55].encode("utf-8") key = b85decode(pkey) cdata = cookie[55:-160].encode("utf-8") sha = b85encode(sha512(cdata).hexdigest().encode("utf-8")).decode("utf-8") digest = cookie[-160:] if sha != digest: warn("The data has been tampered with.") return None try: f = Fernet(key) pdata = f.decrypt(b85decode(cdata)) data = loads(pdata) return data except Exception as ex: error(f"Error decoding the cookie: {ex}") return None
def test_unauthorized_long_file_raises_fr(self): # Setup account = '*****@*****.**' contact = create_contact('Alice') contact.file_reception = False origin = ORIGIN_CONTACT_HEADER type_ = 'file' settings = Settings() packet = Packet(account, contact, origin, type_, settings) file_data = os.urandom(10000) compressed = zlib.compress(file_data, level=9) file_key = os.urandom(32) encrypted = encrypt_and_sign(compressed, key=file_key) encrypted += file_key encoded = base64.b85encode(encrypted) file_data = US_BYTE.join([b'testfile.txt', b'11.0B', b'00d 00h 00m 00s', encoded]) packets = self.mock_file_preprocessor(file_data) # Test self.assertFR("Unauthorized long file from contact.", packet.add_packet, packets[0])
def test_short_file_from_user_raises_fr(self): # Setup account = '*****@*****.**' contact = create_contact('Alice') origin = ORIGIN_USER_HEADER type_ = 'file' settings = Settings() packet = Packet(account, contact, origin, type_, settings) file_data = b'abcdefghijk' compressed = zlib.compress(file_data, level=9) file_key = os.urandom(32) encrypted = encrypt_and_sign(compressed, key=file_key) encrypted += file_key encoded = base64.b85encode(encrypted) file_data = US_BYTE.join([b'testfile.txt', b'11.0B', b'00d 00h 00m 00s', encoded]) packets = self.mock_file_preprocessor(file_data) # Test for p in packets: self.assertFR("Ignored short file from user.", packet.add_packet, p)
def login(): body = get_body() try: user_id, roles = self._auther.login( body['username'], body['password']) except (WrongPassword, UsernameNotFound): raise IncorrectUserPass('Wrong username or password') token = b85encode(urandom(26)) self._tokens[token] = f'{user_id},{"".join(roles)}' res = make_response() res.set_cookie( 'token', token, max_age=current_app.config['REDIS_TOKEN_EXPIRE'], httponly=True, secure=self.secure, samesite=self.same_site) return res
def encode( instance : Any, compress : bool = False, encoding : str = 'utf_8' ) -> str : """ Encodes BSON or JSON. Encoding may be specified if an alternative to UTF-8 is required. Arguments : instance : Any. compress : bool. encoding : str. Returns : state : str. """ if ( not encoding ) : raise ValueError( f'Encoding = {encoding}' ) state = jsonpickle.encode( instance, separators = ( ',', ':' ) ) if ( compress ) : state = str( base64.b85encode( gzip.compress( bytes( state, encoding ) ) ), encoding ) return state
def secret_hash(data): """ Create a secret hash from data. """ strings = [] for key, value in sorted(data.items()): strings.append(key) try: if isinstance(value, dict): value = sorted(value.items()) if isinstance(value, list): value = tuple(value) data = hash(value) if data != -1: strings.append(str(data)) except TypeError: pass data = "".join(strings) hash_value = md5(data.encode("utf8")).digest() return base64.b85encode(hash_value).decode("ascii")
def enc(s): labels = [] labels.append('s') # print(s) # print("l: ", s.__len__()) # print("..") b = str.encode(s, 'ascii') labels.append('b') # print(b) # print("l: ", b.__len__()) # print("..") b64 = base64.standard_b64encode(b) labels.append('b64') # print(b64) # print("l: ", b64.__len__()) # print("..") z = gzip.compress(b) labels.append('z') # print(z) # print("l: ", z.__len__()) # print("..") z64 = base64.standard_b64encode(z) labels.append('z64') # print(z64) # print("l: ", z64.__len__()) # print("..") z85 = base64.b85encode(z) labels.append('z85') # print(z85) # print("l: ", z85.__len__()) return ((s.__len__(), b.__len__(), b64.__len__(), z.__len__(), z64.__len__(), z85.__len__()), labels)
def handle(self): request = self.rfile.readline().strip().decode("ascii") print("Got request from {}: '{}'".format(self.client_address, request)) try: parsed = json.loads(request) dns_query = DNSRecord.parse(base64.b85decode(parsed['query'])) except json.JSONDecodeError as e: print("Error: JSON decode failed", e) self.send_error_rsp("Invalid JSON") return except DNSError as e: print("Error: DNS record decode failed", e) self.send_error_rsp("Invalid DNS query") return # Only looking at first question part q = dns_query.get_q() if q.qtype != QTYPE.A: print("Error: Unexpected query type {} (only A/IPv4 lookup supported)".format(q.qtype)) self.send_error_rsp("Invalid query type") return # Note: this is a very simplistic implementation that only returns A records hostname = q.qname.idna() dns_response = dns_query.reply() if hostname in DNS_RECORDS: virt_addr = DNS_RECORDS[hostname] # TODO: would generate virtual IP here and communicate with OF controller to install mapping to private IP; # for the simulation, we are hard-coding this part and not implementing communication with the OF controller dns_response.add_answer(RR(rname=hostname, rtype=QTYPE.A, ttl=DNS_TTL, rdata=A(virt_addr ))) else: # Domain not found dns_response.header.set_rcode("NXDOMAIN") json_resp = { "status": 0, "response": base64.b85encode(dns_response.pack()).decode("ascii") } self.send_json(json_resp)
def wallet_sign(wallet_privkey, message): """ static method used to sign with wallet privkey. used in conjunction with WalletService class :param wallet_privkey: private key of wallet, already imported key :param message: byte string,message to be signed, usually bytes of signature of client private key :return: bytes, digital signature """ if not isinstance(message, (bytes, str)): return None elif isinstance(message, str): message = message.encode() hash_of_message = SHA256.new(message) signer = DSS.new(wallet_privkey, mode="fips-186-3") digital_signature = signer.sign(hash_of_message) digital_signature = base64.b85encode(digital_signature).decode() return digital_signature
def launch(args: List[str]) -> None: config_dir, kitten = args[:2] kitten = resolved_kitten(kitten) del args[:2] args = [kitten] + args os.environ['KITTY_CONFIG_DIRECTORY'] = config_dir set_debug(kitten) m = import_kitten_main_module(config_dir, kitten) try: result = m['start'](args) finally: sys.stdin = sys.__stdin__ if result is not None: import json import base64 data = base64.b85encode(json.dumps(result).encode('utf-8')) sys.stdout.buffer.write(b'\[email protected]|') sys.stdout.buffer.write(data) sys.stdout.buffer.write(b'\x1b\\') sys.stderr.flush() sys.stdout.flush()
def Base85(mode, data): if mode == 0: print("[Info]Encryption is in progress......") try: data_result = base64.b85encode(data.encode()).decode() return "[Success]Your cipher_text is:" + data_result except: print( "[Fail]Encryption failed! Please check the information you gave!" ) elif mode == 1: print("[Info]Decryption is in progress......") try: data_result = base64.b85decode(data.encode()).decode() return "[Success]Your plain_text is:" + data_result except: print( "[Fail]Decryption failed! Please check the information you gave!" ) else: print("[ERROR]Invalid Mode!(encode->0/decode->1)")
def send_unsupported_skill(self, envelope: Envelope) -> None: """ Handle the received envelope in case the skill is not supported. :param envelope: the envelope :return: None """ logger.warning( "Cannot handle envelope: no handler registered for the protocol '{}'." .format(envelope.protocol_id)) encoded_envelope = base64.b85encode(envelope.encode()).decode("utf-8") reply = DefaultMessage( type=DefaultMessage.Type.ERROR, error_code=DefaultMessage.ErrorCode.UNSUPPORTED_SKILL.value, error_msg="Unsupported skill.", error_data={"envelope": encoded_envelope}) self.context.outbox.put_message( to=envelope.sender, sender=self.context.agent_public_key, protocol_id=DefaultMessage.protocol_id, message=DefaultSerializer().encode(reply))
def log_write(log_file, log_buffer, secs=1.0): previous_data = {} while True: if not log_buffer.empty(): data = log_buffer.get() diff_result = jsondiff.diff(previous_data, data) if diff_result != {}: if [str(key) for key in diff_result.keys()][0] == '$replace': diff_result = copy.deepcopy(list(diff_result.values())[0]) diff_result['time'] = time.time() - program_start_time # compress sequence diff_result = json.dumps(diff_result) diff_result = diff_result.encode('utf-8') diff_result = zlib.compress(diff_result, level=9) diff_result = base64.b85encode(diff_result) diff_result = diff_result.decode('ascii') # write sequence log_file.write("{}\n".format(diff_result).encode()) previous_data = copy.deepcopy(data) else: time.sleep(secs)
def _create_client_side_session(sio: ServerApp, user: Optional[User]): """ :param user: If the session's user was already retrieved, pass it along to avoid an extra query. :return: """ session = sio.get_session() encrypted_session = sio.fernet_encrypt.encrypt(json.dumps(session).encode("utf-8")) if user is None: user = User.get_by_id(session["user-id"]) elif user.id != session["user-id"]: raise RuntimeError(f"Provided user does not match the session's user") return { "user": user.as_json, "sessions": [ membership.session.create_list_entry() for membership in GameSessionMembership.select().where(GameSessionMembership.user == user) ], "encoded_session_b85": base64.b85encode(encrypted_session), }
def pack(source: str, *, include_cfnresponse: Optional[bool] = None): if include_cfnresponse is None: # if CloudFormation detects an import of `cfnresponse`, it'll add that # module to your Lambda environment automatically. # this regex roughly matches CloudFormation's detection logic. include_cfnresponse = bool( re.search(r"^\s*import\s+cfnresponse", source, re.MULTILINE)) attempts = [ ("import cfnresponse\n" if include_cfnresponse else "") + source ] imports = (["cfnresponse"] if include_cfnresponse else []) + ["base64"] for compressor, decompressor in ALGORITHMS: compressed = compressor(source.encode("utf-8")) packed = PACK_TEMPLATE.format( imports=",".join(imports), module=decompressor.__module__, decompressor=decompressor.__name__, encoded=base64.b85encode(compressed).decode("utf-8"), ) attempts.append(packed) return sorted(attempts, key=len)[0]
def test_noise_packet_interrupts_file(self): # Setup packet = Packet('*****@*****.**', self.contact, ORIGIN_CONTACT_HEADER, FILE, self.settings) compressed = zlib.compress(os.urandom(10000), level=COMPRESSION_LEVEL) file_key = os.urandom(KEY_LENGTH) encrypted = encrypt_and_sign(compressed, key=file_key) encrypted += file_key encoded = base64.b85encode(encrypted) file_data = int_to_bytes(1000) + int_to_bytes(10000) + b'testfile.txt' + US_BYTE + encoded packets = split_to_assembly_packets(file_data, FILE) packets = packets[:20] packets.append(byte_padding(P_N_HEADER)) # Add cancel packet for p in packets: packet.add_packet(p) # Test self.assertEqual(len(packet.assembly_pt_list), 0) # Cancel packet empties packet list self.assertFalse(packet.long_active) self.assertFalse(packet.is_complete) self.assertEqual(packet.log_masking_ctr, len(packets))
def sign(self, message): """ signs message with private key of username :param message: bytes string or string :return: bytes; signature of message using private key """ # if not already a byte string turn it to making sure if not isinstance(message, (bytes, str)): return None elif isinstance(message, str): message = message.encode() hash_of_message = SHA256.new(message) signer = DSS.new(self.privkey, mode="fips-186-3") digital_signature = signer.sign(hash_of_message) digital_signature = base64.b85encode(digital_signature).decode() return digital_signature
async def process_task(task_id: int, is_video: bool, file: bytes): cache = GlobalCache() if is_video: logger.debug(f"Video: {task_id}") file_key = "video" url = "http://ff_videocomp:3800/api/video" else: logger.debug(f"Photo: {task_id}") file_key = "face" url = "http://ff_corecomp:3800/api/core/recface" async with aiohttp.ClientSession() as session: async with session.post(url, json={file_key: b85encode(file).decode()}) as resp: if resp.status != 200: cache[task_id].status = TaskStatus.ERR cache[task_id].err = (await resp.json())["detail"] logger.warning(f"Error for {cache[task_id]}") else: cache[task_id].status = TaskStatus.DONE cache[task_id].output = await resp.json()
def send_decoding_error(self, envelope: Envelope) -> None: """ Handle a decoding error. :param envelope: the envelope :return: None """ self.context.logger.warning( "Decoding error for envelope: {}. Protocol_id='{}' and message='{!r}' are inconsistent." .format(envelope, envelope.protocol_id, envelope.message)) encoded_envelope = base64.b85encode(envelope.encode()) reply = DefaultMessage( dialogue_reference=("", ""), message_id=1, target=0, performative=DefaultMessage.Performative.ERROR, error_code=DefaultMessage.ErrorCode.DECODING_ERROR, error_msg="Decoding error.", error_data={"envelope": encoded_envelope}, ) reply.counterparty = envelope.sender self.context.outbox.put_message(message=reply)
def encode_pyspark_model(model): with tempfile.TemporaryDirectory() as dirpath: dirpath = os.path.join(dirpath, "model") # Save the model model.save(dirpath) # Create the temporary zip-file. mem_zip = BytesIO() with zipfile.ZipFile(mem_zip, "w", zipfile.ZIP_DEFLATED, compresslevel=9) as zf: # Zip the directory. for root, dirs, files in os.walk(dirpath): for file in files: rel_dir = os.path.relpath(root, dirpath) zf.write(os.path.join(root, file), os.path.join(rel_dir, file)) zipped = mem_zip.getvalue() encoded = base64.b85encode(zipped) return str(encoded, "utf-8")
def setUp(self): self.msg = ("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean condimentum consectetur purus quis" " dapibus. Fusce venenatis lacus ut rhoncus faucibus. Cras sollicitudin commodo sapien, sed bibendu" "m velit maximus in. Aliquam ac metus risus. Sed cursus ornare luctus. Integer aliquet lectus id ma" "ssa blandit imperdiet. Ut sed massa eget quam facilisis rutrum. Mauris eget luctus nisl. Sed ut el" "it iaculis, faucibus lacus eget, sodales magna. Nunc sed commodo arcu. In hac habitasse platea dic" "tumst. Integer luctus aliquam justo, at vestibulum dolor iaculis ac. Etiam laoreet est eget odio r" "utrum, vel malesuada lorem rhoncus. Cras finibus in neque eu euismod. Nulla facilisi. Nunc nec ali" "quam quam, quis ullamcorper leo. Nunc egestas lectus eget est porttitor, in iaculis felis sceleris" "que. In sem elit, fringilla id viverra commodo, sagittis varius purus. Pellentesque rutrum loborti" "s neque a facilisis. Mauris id tortor placerat, aliquam dolor ac, venenatis arcu.") self.contact = create_contact() self.settings = Settings(logfile_masking=True) compressed = zlib.compress(b'abcdefghijk', level=COMPRESSION_LEVEL) file_key = os.urandom(KEY_LENGTH) encrypted = encrypt_and_sign(compressed, key=file_key) encrypted += file_key encoded = base64.b85encode(encrypted) self.short_f_data = (int_to_bytes(1) + int_to_bytes(2) + b'testfile.txt' + US_BYTE + encoded)
def test_disabled_file_reception_raises_fr_with_end_packet(self): # Setup packet = Packet('*****@*****.**', self.contact, ORIGIN_CONTACT_HEADER, FILE, self.settings) packet.long_active = True compressed = zlib.compress(os.urandom(10000), level=COMPRESSION_LEVEL) file_key = os.urandom(KEY_LENGTH) encrypted = encrypt_and_sign(compressed, key=file_key) encrypted += file_key encoded = base64.b85encode(encrypted) file_data = int_to_bytes(1000) + int_to_bytes(10000)+ b'testfile.txt' + US_BYTE + encoded packets = split_to_assembly_packets(file_data, FILE) for p in packets[:-1]: self.assertIsNone(packet.add_packet(p)) packet.contact.file_reception = False # Test for p in packets[-1:]: self.assertFR("Alert! File reception disabled mid-transfer.", packet.add_packet, p) self.assertEqual(packet.log_masking_ctr, len(packets))
def encode(string, key): plainTextOctets = [format(x, 'b').rjust(8).replace(' ', '0')# This makes keyOctets an array with len(key) elements, for x in [ord(x) for x in string]] # where each element is an 8-long string containing a # binary representation of the unicode character of the key keyOctets = [format(x, 'b').rjust(8).replace(' ', '0') for x in # This makes keyOctets an array with len(key) elements, [ord(x) for x in key]] # where each element is an 8-long string containing a # binary representation of the unicode character of the key while len(keyOctets) < len(plainTextOctets): # This takes the key and repeats it until for octet in keyOctets: # the key is longer than the plaintext if len(keyOctets) > len(plainTextOctets): break keyOctets = keyOctets + [octet] xorCompleteOctets = [''.join([('1' if Cipher.xor(ptDigit, # This cluster**** of code will keyOctets[i][j]) else '0') for (j, # take an array of plaintext encoded ptDigit) in enumerate(ptOctet)]) for (i, # into binary and xor it against an ptOctet) in enumerate(plainTextOctets)] # array (keyOctets) that is longer than the xorCompleteInts = [int(x, base=2) for x in xorCompleteOctets] # plaintext with each element a string of #xorCompleteHex = [format(k, 'x') for k in xorCompleteInts] # length 8 with a binary encoded integer #xorCompleteHex = str(bytearray(xorCompleteInts)) xorCompleteHex = "".join([chr(x) for x in xorCompleteInts]) xorCompleteB64 = base64.b85encode(bytes(xorCompleteHex, 'utf-8')) # Converts the hex encoded encrypted input into base85 for compactness return xorCompleteB64.decode('utf-8')
async def middleware_handler(request): if request.method == 'OPTIONS': return await handler(request) session_id = request.cookies.get(Config.LP.COOKIE_NAME) session = sessions.get(session_id) if session_id else None if session and time.time() - session.ts > Config.LP.MAX_SESSION_TIME: retire(session_id) session = None logger.debug('Session %s reached max life time', session_id) if not session: while True: session_id = base64.b85encode(os.urandom(32)).decode('ascii') if not session_id in sessions: break user, role = await authenticate(request) session = Session(user, role, app.loop.call_later(Config.LP.MAX_SESSION_INACTIVITY, retire, session_id), loop = app.loop) sessions[session_id] = session request['new_session'] = True logger.debug('Starting new session %s', session_id) else: session.retire_task.cancel() session.retire_task = app.loop.call_later(Config.LP.MAX_SESSION_INACTIVITY, retire, session_id) request['new_session'] = False logger.debug('Using existing session %s', session_id) request['initial_request'] = bool(request.headers.get('Authorization')) request['session'] = session try: response = await handler(request) if not response.prepared: response.set_cookie(Config.LP.COOKIE_NAME, session_id, max_age = Config.LP.MAX_SESSION_TIME) return response except Exception: raise
def base54_func(): # 现代接口 # base64.b64encode(s, altchars=None) // Base64编码 bytes = base64.b64encode(b'luzhuo.me') # base64.b64decode(s, altchars=None, validate=False) // Base64解码, validate:True(非字母字符抛binascii.Error), False(非字母字符丢弃) bytes = base64.b64decode(bytes) # base64.standard_b64encode(s) // 标准的Base64字母表编码(同b64encode) bytes = base64.standard_b64encode(b'luzhuo.me') # base64.standard_b64decode(s) // 标准的Base64字母表解码(同b64decode) bytes = base64.standard_b64decode(bytes) # base64.urlsafe_b64encode(s) // 使用URL和文件系统安全的字母表编码 bytes = base64.urlsafe_b64encode(b'luzhuo.me') # base64.urlsafe_b64decode(s) // 使用URL和文件系统安全的字母表解码 bytes = base64.urlsafe_b64decode(bytes) # base64.b32encode(s) // Base32编码 bytes = base64.b32encode(b'luzhuo.me') # base64.b32decode(s, casefold=False, map01=None) // Base32解码 bytes = base64.b32decode(bytes) # base64.b16encode(s) // 使用Base16编码 bytes = base64.b16encode(b'luzhuo.me') # base64.b16decode(s, casefold=False) // Base16解码 bytes = base64.b16decode(bytes) # base64.a85encode(b, *, foldspaces=False, wrapcol=0, pad=False, adobe=False) // Ascii85编码 bytes = base64.a85encode(b'luzhuo.me') # base64.a85decode(b, *, foldspaces=False, adobe=False, ignorechars=b' \t\n\r\v') // Ascii85解码 bytes = base64.a85encode(bytes) # base64.b85encode(b, pad=False) // base85编码 bytes = base64.b85encode(b'luzhuo.me') # base64.b85decode(b) // base85解码 bytes = base64.b85decode(bytes) # 传统接口 # base64.encode(input, output) // 编码, input从文件读取二进制数据, output写入文件 (每76个字节后 和 末尾 插入b'\n') base64.encode(open("file.txt", "rb"), open("base.txt", "wb")) # base64.decode(input, output) // 解码 base64.decode(open("base.txt", "rb"), open("file.txt", "wb")) bytes = base64.encodebytes(b'luzhuo.me') # 编码 (每76个字节后 和 末尾 插入b'\n') bytes = base64.decodebytes(bytes) # 解码
def test_recinto1(self): """Cria chave para recinto, manda chave """ # login # manda recinto, senha # recebe chaveprivada, assina recinto os.environ['VERIFY_SIGN'] = "YES" with self.app.app.app_context(): recinto = '00001' private_key_pem, assinado = UseCases.gera_chaves_recinto( self.db_session, recinto) private_key = assinador.load_private_key(private_key_pem) assinado = assinador.sign(recinto.encode('utf-8'), private_key) assinado = b85encode(assinado).decode('utf-8') # manda recinto encriptado com chave # recebe OK com chave correta payload = {'assinado': assinado, 'recinto': recinto} token = authentication.generate_token(payload) request = Request({'Authorization': 'Bearer %s' % token}, payload) assert authentication.valida_token_e_assinatura(request, self.db_session)[0] is True # manda recinto sem encriptar, recebe erro payload = {'assinado': recinto, 'recinto': recinto} token = authentication.generate_token(payload) request = Request({'Authorization': 'Bearer %s' % token}, payload) assert authentication.valida_token_e_assinatura(request, self.db_session)[0] is False # manda assinado com outra chave, recebe erro private_key2, _ = assinador.generate_keys() assinado2 = assinador.sign(recinto.encode('utf-8'), private_key2) payload2 = {'assinado': assinado2, 'recinto': recinto} token2 = authentication.generate_token(payload2) request2 = Request({'Authorization': 'Bearer %s' % token2}, payload2) assert authentication.valida_token_e_assinatura(request2, self.db_session)[0] is False
def default(self, obj): # pylint: disable=method-hidden if isinstance(obj, bytes): return {'__type__': 'bytes', 'value': base64.b85encode(obj).decode('ascii')} if isinstance(obj, Duration): return {'__type__': 'Duration', 'value': [obj.numerator, obj.denominator]} if isinstance(obj, Pitch): return {'__type__': 'Pitch', 'value': [obj.name]} if isinstance(obj, Clef): return {'__type__': 'Clef', 'value': [obj.value]} if isinstance(obj, KeySignature): return {'__type__': 'KeySignature', 'value': [obj.name]} if isinstance(obj, TimeSignature): return {'__type__': 'TimeSignature', 'value': [obj.upper, obj.lower]} if isinstance(obj, misc.Pos2F): return {'__type__': 'Pos2F', 'value': [obj.x, obj.y]} return super().default(obj)
def generate_password(self) -> None: self.text['state'] = 'normal' self.text.delete('1.0', 'end') raw_input = self.key_value.get() + self.salt_value.get() # type: str hashers = (hashlib.md5, hashlib.sha1, hashlib.sha224, hashlib.sha256, hashlib.sha384, hashlib.sha512) output_strings = [] # type: tg.List[str] for hasher_init in hashers: hasher = hasher_init() name = hasher.name # type: str hasher.update(raw_input.encode()) digest = hasher.digest() hexdigest = hasher.hexdigest() b64_output = base64.standard_b64encode(digest).decode() a85_output = base64.a85encode(digest).decode() b85_output = base64.b85encode(digest).decode() output_strings.append( "hash: %s\nresult: %s\nbase64: %s\nascii85: %s\nbase85: %s\n" % (name, hexdigest, b64_output, a85_output, b85_output)) for output_string in output_strings: self.text.insert('end', output_string + '\n') self.text['state'] = 'disabled'
range_index = map.add("Math::Range", "classMath_1_1Range.html", flags=ResultFlag.CLASS) trie.insert("math::range", range_index) trie.insert("range", range_index) index = map.add("Math::Range::min() const", "classMath_1_1Range.html#min", suffix_length=6, flags=ResultFlag.FUNC|ResultFlag.DELETED) trie.insert("math::range::min()", index, lookahead_barriers=[4, 11]) trie.insert("range::min()", index, lookahead_barriers=[5]) trie.insert("min()", index) trie.insert("subpage", map.add("Page » Subpage", "subpage.html", flags=ResultFlag.PAGE)) trie.insert("rectangle", map.add("Rectangle", "", alias=range_index)) trie.insert("rect", map.add("Rectangle::Rect()", "", suffix_length=2, alias=range_index)) with open(basedir/'searchdata.bin', 'wb') as f: f.write(serialize_search_data(trie, map, 7)) with open(basedir/'searchdata.b85', 'wb') as f: f.write(base64.b85encode(serialize_search_data(trie, map, 7), True)) trie = Trie() map = ResultMap() trie.insert("hýždě", map.add("Hýždě", "#a", flags=ResultFlag.PAGE)) trie.insert("hárá", map.add("Hárá", "#b", flags=ResultFlag.PAGE)) with open(basedir/'unicode.bin', 'wb') as f: f.write(serialize_search_data(trie, map, 2)) trie = Trie() map = ResultMap() trie.insert("magnum", map.add("Magnum", "namespaceMagnum.html", flags=ResultFlag.NAMESPACE)) trie.insert("math", map.add("Magnum::Math", "namespaceMagnum_1_1Math.html", flags=ResultFlag.NAMESPACE)) trie.insert("geometry", map.add("Magnum::Math::Geometry", "namespaceMagnum_1_1Math_1_1Geometry.html", flags=ResultFlag.NAMESPACE))
def installer(installer_path=os.path.join(paths.CONTRIB, "get-pip.py")): print("[generate.installer] Generating installer") # Define our wrapper script WRAPPER_SCRIPT = """ #!/usr/bin/env python # # Hi There! # You may be wondering what this giant blob of binary data here is, you might # even be worried that we're up to something nefarious (good for you for being # paranoid!). This is a base85 encoding of a zip file, this zip file contains # an entire copy of pip. # # Pip is a thing that installs packages, pip itself is a package that someone # might want to install, especially if they're looking to run this get-pip.py # script. Pip has a lot of code to deal with the security of installing # packages, various edge cases on various platforms, and other such sort of # "tribal knowledge" that has been encoded in its code base. Because of this # we basically include an entire copy of pip inside this blob. We do this # because the alternatives are attempt to implement a "minipip" that probably # doesn't do things correctly and has weird edge cases, or compress pip itself # down into a single file. # # If you're wondering how this is created, it is using an invoke task located # in tasks/generate.py called "installer". It can be invoked by using # ``invoke generate.installer``. import os.path import pkgutil import shutil import sys import struct import tempfile # Useful for very coarse version differentiation. PY2 = sys.version_info[0] == 2 PY3 = sys.version_info[0] == 3 if PY3: iterbytes = iter else: def iterbytes(buf): return (ord(byte) for byte in buf) try: from base64 import b85decode except ImportError: _b85alphabet = (b"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" b"abcdefghijklmnopqrstuvwxyz!#$%&()*+-;<=>[email protected]^_`{{|}}~") def b85decode(b): _b85dec = [None] * 256 for i, c in enumerate(iterbytes(_b85alphabet)): _b85dec[c] = i padding = (-len(b)) % 5 b = b + b'~' * padding out = [] packI = struct.Struct('!I').pack for i in range(0, len(b), 5): chunk = b[i:i + 5] acc = 0 try: for c in iterbytes(chunk): acc = acc * 85 + _b85dec[c] except TypeError: for j, c in enumerate(iterbytes(chunk)): if _b85dec[c] is None: raise ValueError( 'bad base85 character at position %d' % (i + j) ) raise try: out.append(packI(acc)) except struct.error: raise ValueError('base85 overflow in hunk starting at byte %d' % i) result = b''.join(out) if padding: result = result[:-padding] return result def bootstrap(tmpdir=None): # Import pip so we can use it to install pip and maybe setuptools too import pip from pip.commands.install import InstallCommand from pip.req import InstallRequirement # Wrapper to provide default certificate with the lowest priority class CertInstallCommand(InstallCommand): def parse_args(self, args): # If cert isn't specified in config or environment, we provide our # own certificate through defaults. # This allows user to specify custom cert anywhere one likes: # config, environment variable or argv. if not self.parser.get_default_values().cert: self.parser.defaults["cert"] = cert_path # calculated below return super(CertInstallCommand, self).parse_args(args) pip.commands_dict["install"] = CertInstallCommand implicit_pip = True implicit_setuptools = True implicit_wheel = True # Check if the user has requested us not to install setuptools if "--no-setuptools" in sys.argv or os.environ.get("PIP_NO_SETUPTOOLS"): args = [x for x in sys.argv[1:] if x != "--no-setuptools"] implicit_setuptools = False else: args = sys.argv[1:] # Check if the user has requested us not to install wheel if "--no-wheel" in args or os.environ.get("PIP_NO_WHEEL"): args = [x for x in args if x != "--no-wheel"] implicit_wheel = False # We only want to implicitly install setuptools and wheel if they don't # already exist on the target platform. if implicit_setuptools: try: import setuptools # noqa implicit_setuptools = False except ImportError: pass if implicit_wheel: try: import wheel # noqa implicit_wheel = False except ImportError: pass # We want to support people passing things like 'pip<8' to get-pip.py which # will let them install a specific version. However because of the dreaded # DoubleRequirement error if any of the args look like they might be a # specific for one of our packages, then we'll turn off the implicit # install of them. for arg in args: try: req = InstallRequirement.from_line(arg) except: continue if implicit_pip and req.name == "pip": implicit_pip = False elif implicit_setuptools and req.name == "setuptools": implicit_setuptools = False elif implicit_wheel and req.name == "wheel": implicit_wheel = False # Add any implicit installations to the end of our args if implicit_pip: args += ["pip"] if implicit_setuptools: args += ["setuptools"] if implicit_wheel: args += ["wheel"] delete_tmpdir = False try: # Create a temporary directory to act as a working directory if we were # not given one. if tmpdir is None: tmpdir = tempfile.mkdtemp() delete_tmpdir = True # We need to extract the SSL certificates from requests so that they # can be passed to --cert cert_path = os.path.join(tmpdir, "cacert.pem") with open(cert_path, "wb") as cert: cert.write(pkgutil.get_data("pip._vendor.requests", "cacert.pem")) # Execute the included pip and use it to install the latest pip and # setuptools from PyPI sys.exit(pip.main(["install", "--upgrade"] + args)) finally: # Remove our temporary directory if delete_tmpdir and tmpdir: shutil.rmtree(tmpdir, ignore_errors=True) def main(): tmpdir = None try: # Create a temporary working directory tmpdir = tempfile.mkdtemp() # Unpack the zipfile into the temporary directory pip_zip = os.path.join(tmpdir, "pip.zip") with open(pip_zip, "wb") as fp: fp.write(b85decode(DATA.replace(b"\\n", b""))) # Add the zipfile to sys.path so that we can import it sys.path.insert(0, pip_zip) # Run the bootstrap bootstrap(tmpdir=tmpdir) finally: # Clean up our temporary working directory if tmpdir: shutil.rmtree(tmpdir, ignore_errors=True) DATA = b\"\"\" {zipfile} \"\"\" if __name__ == "__main__": main() """.lstrip() # Determine what the latest version of pip on PyPI is. resp = urllib.request.urlopen("https://pypi.python.org/pypi/pip/json") data = json.loads(resp.read().decode("utf8")) version = data["info"]["version"] file_urls = [ (x["url"], x["md5_digest"]) for x in data["releases"][version] if x["url"].endswith(".whl") ] assert len(file_urls) == 1 url, expected_hash = file_urls[0] # Fetch the file itself. data = urllib.request.urlopen(url).read() assert hashlib.md5(data).hexdigest() == expected_hash # Write out the wrapper script that will take the place of the zip script # The reason we need to do this instead of just directly executing the # zip script is that while Python will happily execute a zip script if # passed it on the file system, it will not however allow this to work if # passed it via stdin. This means that this wrapper script is required to # make ``curl https://...../get-pip.py | python`` continue to work. print( "[generate.installer] Write the wrapper script with the bundled zip " "file" ) zipdata = base64.b85encode(data).decode("utf8") chunked = [] for i in range(0, len(zipdata), 79): chunked.append(zipdata[i:i + 79]) with open(installer_path, "w") as fp: fp.write(WRAPPER_SCRIPT.format(zipfile="\n".join(chunked))) # Ensure the permissions on the newly created file oldmode = os.stat(installer_path).st_mode & 0o7777 newmode = (oldmode | 0o555) & 0o7777 os.chmod(installer_path, newmode) print("[generate.installer] Generated installer")
#!/usr/bin/env python3 # encoding: utf-8 # # Copyright (c) 2008 Doug Hellmann All rights reserved. # """Demonstrates base85 and ascii85 encodings. http://bugs.python.org/17618 """ #end_pymotw_header import base64 original_data = b'This is the data, in the clear.' print('Original : {} bytes {!r}'.format( len(original_data), original_data)) b64_data = base64.b64encode(original_data) print('b64 Encoded : {} bytes {!r}'.format( len(b64_data), b64_data)) b85_data = base64.b85encode(original_data) print('b85 Encoded : {} bytes {!r}'.format( len(b85_data), b85_data)) a85_data = base64.a85encode(original_data) print('a85 Encoded : {} bytes {!r}'.format( len(a85_data), a85_data))
def encode(cls, value): if not isinstance(value, bytes): value = value.encode() return base64.b85encode(value)
def inputSettings(): __main__.database['api']['ircsettings']['network'] = input( "Please enter the address of the IRC network you want to connect to.\n" ) __main__.database['api']['ircsettings']['port'] = int( input( "Please enter the port while you're at it!\n" ) ) __main__.database['api']['ircsettings']['nick'] = input( "Please enter a valid (NO STUPID SYMBOLS) IRC nick for your bot.\n" ).replace( " ", "_" ).replace( ".", "_" ).replace( ",", "_" ) __main__.database['api']['ircsettings']['password'] = base64.b85encode( ( input( "Please enter your bot's NickServ password if required, otherwise leave this blank.\n" ) ).encode( "utf-8" ), pad=True ) __main__.database['api']['ircsettings']['channels'] = [ input( "Please enter the initial channel that your bot will be joining.\n" ) ]
def add_file(self, name, fhash): self.file_data[name] = base64.b85encode(fhash.to_bytes(16, byteorder='big')).decode('utf-8')
loadDatabase() if database['version'] == 1: # Upgrade to version 2 # Version 2 changes: # - ['botInfo']['channel'] is replaced with ['botInfo']['channels'] # We now support the bot being in multiple channels at the same time! database['botInfo']['channels'] = [] database['botInfo']['channels'].append( database['botInfo']['channel'] ) del database['botInfo']['channel'] database['version'] = 2 saveDatabase() if database['version'] == 2: # Upgrade to version 3 # Version 3 changes: # - ['botInfo']['password'] is now stored in base85 format rather than plaintext. # This doesn't provide any real security, but it prevents some noob from # just opening the database in a text editor and seeing the password. database['botInfo']['password'] = base64.b85encode( database['botInfo']['password'].encode( "utf-8" ), pad=True ) database['version'] = 3 saveDatabase() if database['version'] == 3: # Upgrade to version 4 # Version 4 changes: # - Modular API system database['api'] = {} database['api']['system'] = "irc" # default to IRC database['api']['ircsettings'] = {} database['api']['ircsettings']['channels'] = database['botInfo']['chanels'] database['api']['ircsettings']['nick'] = database['botInfo']['nick'] database['api']['ircsettings']['pw'] = database['botInfo']['password'] database['api']['ircsettings']['network'] = database['botInfo']['network'] database['api']['ircsettings']['port'] = database['botInfo']['port'] del database['botInfo']['channels'] del database['botInfo']['nick']
def base85encode(str, coding = 'utf8'): if type(str)==type('ss'): a = str.encode(coding) return base64.b85encode(a).decode(coding)
loadDatabase() if database['version'] == 1: # Upgrade to version 2 # Version 2 changes: # - ['botInfo']['channel'] is replaced with ['botInfo']['channels'] # We now support the bot being in multiple channels at the same time! database['botInfo']['channels'] = [] database['botInfo']['channels'].append( database['botInfo']['channel'] ) del database['botInfo']['channel'] database['version'] = 2 saveDatabase() if database['version'] == 2: # Version 3 changes: # - ['botInfo']['password'] is now stored in base85 format rather than plaintext. # This doesn't provide any real security, but it prevents some noob from # just opening the database in a text editor and seeing the password. database['botInfo']['password'] = base64.b85encode( database['botInfo']['password'].encode( "utf-8" ), pad=True ) database['version'] = 3 saveDatabase() except IOError: # Create pybot.pickle on first start database = { "accessList" : {}, "botInfo" : { "nick" : "", "password" : base64.b85encode( "".encode( "utf-8" ), pad=True ), "network" : "", "port" : 0, "channels" : [] }, "globals" : { "cc" : "!", "reverse" : False, "debug" : False }, "version" : 3 } print( colorama.Fore.CYAN ) database['botInfo']['network'] = input( "Please enter the address of the IRC network you want to connect to.\n" ) database['botInfo']['port'] = int( input( "Please enter the port while you're at it!\n" ) ) database['botInfo']['nick'] = input( "Please enter a valid (NO STUPID SYMBOLS) IRC nick for your bot.\n" ).replace( " ", "_" ).replace( ".", "_" ).replace( ",", "_" ) database['botInfo']['password'] = base64.b85encode( ( input( "Please enter your bot's NickServ password if required, otherwise leave this blank.\n" ) ).encode( "utf-8" ), pad=True ) database['botInfo']['channels'] = [ input( "Please enter the initial channel that your bot will be joining.\n" ) ] devel = input( "Please enter your Nick so the bot will listen to you.\n" ) database['accessList'][devel] = 4 print( colorama.Fore.WHITE ) saveDatabase() ## Load up the database ASAP ##
#!/usr/bin/python3 import subprocess,zlib,base64 data=b'treetreetreefiregoldtreetreetreetreegoldtreegoldtreegoldtreegoldtreegoldtreetreegoldtreegoldtreetreetreegoldtreetreetreewaterwatertreetreewatergoldgoldtreegoldtreegoldtreegoldtreegoldtreetreegoldtreegoldtreetreetreegoldtreetreefiregoldgoldwatermoontreemoontreegoldgoldtreewatergoldgoldtreetreegoldgoldtreetreemoonwatergoldgoldwatergoldtreetreemoongoldgoldmoon' x=subprocess.check_output(['zlibrawstdio','c9'],input=data) #x=zlib.compress(data,9) #Py3 print('import zlib,base64;print(zlib.decompress(base64.b85decode("'+base64.b85encode(x).decode()+'")).decode())',end='') #Py2 #print('import zlib,base64;print zlib.decompress(base64.b64decode("'+base64.b64encode(x).decode()+'"))',end='')
import re import sys files = [ '.gitattributes', '.gitignore', 'AUTHORS', 'autogen.sh', 'COPYING', 'NEWS', 'ChangeLog', ] if __name__ == '__main__': script_path = os.path.dirname(os.path.realpath(__file__)) init_script_path = os.path.join(script_path, os.pardir, 'apertium-init.py') parser = argparse.ArgumentParser(description='Update the bootstraper script for any Apertium module') parser.add_argument('-d', '--vanillaDirectory', help='location of directory with vanilla files', default=script_path) parser.add_argument('-f', '--bootstraperScript', help='location of bootstraper script', default=init_script_path) args = parser.parse_args() encoded_files = {} for filename in files: with open(os.path.join(args.vanillaDirectory, filename), 'rb') as f: encoded_files[filename] = base64.b85encode(f.read()) for line in fileinput.input((args.bootstraperScript, ), inplace=True): sys.stdout.write(re.sub(r'^any_module_files = {.*?} # noqa: E501$', 'any_module_files = %s # noqa: E501' % repr(encoded_files), line))
def installer(installer_path=os.path.join(paths.CONTRIB, "get-pip.py")): print("[generate.installer] Generating installer") # Define our wrapper script WRAPPER_SCRIPT = """ #!/usr/bin/env python # # Hi There! # You may be wondering what this giant blob of binary data here is, you might # even be worried that we're up to something nefarious (good for you for being # paranoid!). This is a base64 encoding of a zip file, this zip file contains # an entire copy of pip. # # Pip is a thing that installs packages, pip itself is a package that someone # might want to install, especially if they're looking to run this get-pip.py # script. Pip has a lot of code to deal with the security of installing # packages, various edge cases on various platforms, and other such sort of # "tribal knowledge" that has been encoded in its code base. Because of this # we basically include an entire copy of pip inside this blob. We do this # because the alternatives are attempt to implement a "minipip" that probably # doesn't do things correctly and has weird edge cases, or compress pip itself # down into a single file. # # If you're wondering how this is created, it is using an invoke task located # in tasks/generate.py called "installer". It can be invoked by using # ``invoke generate.installer``. import os.path import pkgutil import shutil import sys import struct import tempfile # Useful for very coarse version differentiation. PY2 = sys.version_info[0] == 2 PY3 = sys.version_info[0] == 3 if PY3: iterbytes = iter else: def iterbytes(buf): return (ord(byte) for byte in buf) try: from base64 import b85decode except ImportError: _b85alphabet = (b"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" b"abcdefghijklmnopqrstuvwxyz!#$%&()*+-;<=>[email protected]^_`{{|}}~") def b85decode(b): _b85dec = [None] * 256 for i, c in enumerate(iterbytes(_b85alphabet)): _b85dec[c] = i padding = (-len(b)) % 5 b = b + b'~' * padding out = [] packI = struct.Struct('!I').pack for i in range(0, len(b), 5): chunk = b[i:i + 5] acc = 0 try: for c in iterbytes(chunk): acc = acc * 85 + _b85dec[c] except TypeError: for j, c in enumerate(iterbytes(chunk)): if _b85dec[c] is None: raise ValueError( 'bad base85 character at position %d' % (i + j) ) raise try: out.append(packI(acc)) except struct.error: raise ValueError('base85 overflow in hunk starting at byte %d' % i) result = b''.join(out) if padding: result = result[:-padding] return result def bootstrap(tmpdir=None): # Import pip so we can use it to install pip and maybe setuptools too import pip from pip.commands.install import InstallCommand # Wrapper to provide default certificate with the lowest priority class CertInstallCommand(InstallCommand): def parse_args(self, args): # If cert isn't specified in config or environment, we provide our # own certificate through defaults. # This allows user to specify custom cert anywhere one likes: # config, environment variable or argv. if not self.parser.get_default_values().cert: self.parser.defaults["cert"] = cert_path # calculated below return super(CertInstallCommand, self).parse_args(args) pip.commands_dict["install"] = CertInstallCommand # We always want to install pip packages = ["pip"] # Check if the user has requested us not to install setuptools if "--no-setuptools" in sys.argv or os.environ.get("PIP_NO_SETUPTOOLS"): args = [x for x in sys.argv[1:] if x != "--no-setuptools"] else: args = sys.argv[1:] # We want to see if setuptools is available before attempting to # install it try: import setuptools # noqa except ImportError: packages += ["setuptools"] delete_tmpdir = False try: # Create a temporary directory to act as a working directory if we were # not given one. if tmpdir is None: tmpdir = tempfile.mkdtemp() delete_tmpdir = True # We need to extract the SSL certificates from requests so that they # can be passed to --cert cert_path = os.path.join(tmpdir, "cacert.pem") with open(cert_path, "wb") as cert: cert.write(pkgutil.get_data("pip._vendor.requests", "cacert.pem")) # Execute the included pip and use it to install the latest pip and # setuptools from PyPI sys.exit(pip.main(["install", "--upgrade"] + packages + args)) finally: # Remove our temporary directory if delete_tmpdir and tmpdir: shutil.rmtree(tmpdir, ignore_errors=True) def main(): tmpdir = None try: # Create a temporary working directory tmpdir = tempfile.mkdtemp() # Unpack the zipfile into the temporary directory pip_zip = os.path.join(tmpdir, "pip.zip") with open(pip_zip, "wb") as fp: fp.write(b85decode(DATA.replace(b"\\n", b""))) # Add the zipfile to sys.path so that we can import it sys.path.insert(0, pip_zip) # Run the bootstrap bootstrap(tmpdir=tmpdir) finally: # Clean up our temporary working directory if tmpdir: shutil.rmtree(tmpdir, ignore_errors=True) DATA = b\"\"\" {zipfile} \"\"\" if __name__ == "__main__": main() """.lstrip() # Get all of the files we want to add to the zip file print("[generate.installer] Collect all the files that should be zipped") all_files = [] for root, dirs, files in os.walk(os.path.join(paths.PROJECT_ROOT, "pip")): for pyfile in files: if os.path.splitext(pyfile)[1] in {".py", ".pem", ".cfg", ".exe"}: path = os.path.join(root, pyfile) all_files.append( "/".join( path.split("/")[len(paths.PROJECT_ROOT.split("/")):] ) ) tmpdir = tempfile.mkdtemp() try: # Get a temporary path to use as staging for the pip zip zpth = os.path.join(tmpdir, "pip.zip") # Write the pip files to the zip archive print("[generate.installer] Generate the bundled zip of pip") with zipfile.ZipFile(zpth, "w", compression=zipfile.ZIP_DEFLATED) as z: for filename in all_files: z.write(os.path.join(paths.PROJECT_ROOT, filename), filename) # Get the binary data that compromises our zip file with open(zpth, "rb") as fp: data = fp.read() finally: shutil.rmtree(tmpdir, ignore_errors=True) # Write out the wrapper script that will take the place of the zip script # The reason we need to do this instead of just directly executing the # zip script is that while Python will happily execute a zip script if # passed it on the file system, it will not however allow this to work if # passed it via stdin. This means that this wrapper script is required to # make ``curl https://...../get-pip.py | python`` continue to work. print( "[generate.installer] Write the wrapper script with the bundled zip " "file" ) zipdata = base64.b85encode(data).decode("utf8") chunked = [] for i in range(0, len(zipdata), 79): chunked.append(zipdata[i:i + 79]) with open(installer_path, "w") as fp: fp.write(WRAPPER_SCRIPT.format(zipfile="\n".join(chunked))) # Ensure the permissions on the newly created file oldmode = os.stat(installer_path).st_mode & 0o7777 newmode = (oldmode | 0o555) & 0o7777 os.chmod(installer_path, newmode) print("[generate.installer] Generated installer")