def upload_file_encrypted(self, path_to_file): """ uploads a file to server encrypting along the way Parameters ---------- path_to_file : file full path name. Returns ------- the http status from the server """ try: file_encryption_wallet = self.get_file_encryption_wallet() cookies = self.authenticate() except NotaryException as e: raise NotaryException(e.error_code, e.message) document_hash = hashfile.hash_file(path_to_file) try: file_stream_encrypt.encrypt_file( path_to_file, path_to_file + ".encrypted", file_encryption_wallet.get_public_key() ) files = {"document_content": open(path_to_file + ".encrypted", "rb")} upload_response = requests.put( self.notary_server.get_document_url(self.address, document_hash), cookies=cookies, files=files, verify=False, ) return upload_response.status_code except requests.ConnectionError as e: raise NotaryException(upload_response.status_code, "Problem uploading file!")
def upload_file(self, path_to_file, encrypted=False): ''' uploads a file to server Parameters ---------- path_to_file : give a file pointer,i.e. file pointer. Need change code support file full path name. Returns ------- the http status from the server ''' if encrypted: reg_status = self.register_user_status() private_key_hex = str(reg_status['file_encryption_key']) private_key_wif = base58.base58_check_encode(0x80, private_key_hex.decode("hex")) private_key = CBitcoinSecret(private_key_wif) public_key = private_key.pub if type(path_to_file) is str: document_hash = hashfile.hash_file(path_to_file) else: document_hash = hashfile.hash_file_fp(path_to_file) cookies = self.authenticate() if cookies is not None: check_notarized = requests.get(self.notary_server.get_notarization_status_url(self.address, document_hash), cookies=cookies, verify=False) if check_notarized is not None: if check_notarized.status_code == 404: return None elif check_notarized.status_code == 200: try: cookies = requests.utils.dict_from_cookiejar(check_notarized.cookies) if encrypted: file_stream_encrypt.encrypt_file(path_to_file,path_to_file+".encrypted", public_key) files = {'document_content': open(path_to_file+".encrypted", 'rb')} else: files = {'document_content': open(path_to_file, 'rb')} upload_response = requests.put( self.notary_server.get_upload_url(self.address, document_hash), cookies=cookies, files=files, verify=False) return upload_response.status_code except requests.ConnectionError as e: print (e.message) return None
import file_stream_encrypt import wallet from bitcoinlib.core.key import CPubKey import requests requests.packages.urllib3.disable_warnings() file_name = "/Users/tssbi08/Downloads/jdk-8u65-macosx-x64.dmg" encrypted_file = "/Users/tssbi08/Downloads/encrypt_jdk-8u65-macosx-x64.dmg" decrypted_file = "/Users/tssbi08/Downloads/decrypt-8u65-macosx-x64.dmg" wallet = wallet.PlainWallet() public_key = CPubKey(wallet.get_public_key_hex().decode("hex")) file_stream_encrypt.encrypt_file(file_name,encrypted_file,public_key) file_stream_encrypt.decrypt_file(encrypted_file,decrypted_file,wallet.get_private_key_wif())