def validate_infile( req_info, infile ): ''' validate the incoming data. check its hash and size return (HTTP status, message) ''' hf = HashFunc() total_read = 0 buflen = 4096 while True: inbuf = infile.read( buflen ) hf.update( inbuf ) total_read += len(inbuf) if len(inbuf) == 0: break infile_hash = hf.hexdigest() infile.seek(0) # check size if req_info.size != total_read: log.error("Size mismatch: expected %s, got %s" % (req_info.size, total_read)) return (400, "Invalid Request") # check hash if req_info.data_hash != infile_hash: log.error("Hash mismatch: expected '%s', got '%s'" % (req_info.data_hash, infile_hash)) return (400, "Invalid request") return (200, "OK")
def getFileHash(filename): sha = SHA() h = open(filename, 'rb') content = h.read(SIZE) while content: sha.update(content) content = h.read(SIZE) h.close() hashval = sha.digest() return hashval
def __init__(self, passphrase, credential): self.iv = token_bytes(16) passphrase_hasher = PassphraseHasher() passphrase_hasher.update(passphrase.encode(STRING_ENCODING)) self.passphrase_hash = passphrase_hasher.digest() key_hasher = KeyHasher() key_hasher.update(passphrase.encode(STRING_ENCODING)) key = key_hasher.digest() cipher = AES.new(key, AES_MODE, iv=self.iv) self.encrypted_credential = cipher.encrypt( pad(credential.encode(STRING_ENCODING), AES.block_size))
def get_credential(self, supplied_passphrase): passphrase_hasher = PassphraseHasher() passphrase_hasher.update(supplied_passphrase.encode(STRING_ENCODING)) supplied_passphrase_hash = passphrase_hasher.digest() if supplied_passphrase_hash != self.passphrase_hash: raise MismatchedPassphrasesException( "The supplied passphrase was not verifiable") key_hasher = KeyHasher() key_hasher.update(supplied_passphrase.encode(STRING_ENCODING)) key = key_hasher.digest() cipher = AES.new(key, AES_MODE, iv=self.iv) return unpad(cipher.decrypt(self.encrypted_credential), AES.block_size).decode(STRING_ENCODING)
def __get_chunk(self, source_id: str, file: str, checksum: hashlib.sha256, compressed: bool): with requests.get(url="{}/{}/files/{}".format( self.__data_api_url, urllib.parse.quote(source_id), file), stream=True) as resp: if not resp.ok: raise RuntimeError(resp.status_code) with open(os.path.join(self.__st_path, file), "wb") as file: if compressed: file = util.Decompress(file) buffer = resp.raw.read(self.__chunk_size) checksum.update(buffer) while buffer: file.write(buffer) buffer = resp.raw.read(self.__chunk_size) checksum.update(buffer) file.flush()
def validate_infile(req_info, infile): ''' validate the incoming data. check its hash and size return (HTTP status, message) ''' hf = HashFunc() total_read = 0 buflen = 4096 while True: inbuf = infile.read(buflen) hf.update(inbuf) total_read += len(inbuf) if len(inbuf) == 0: break infile_hash = hf.hexdigest() infile.seek(0) # check size if req_info.size != total_read: log.error("Size mismatch: expected %s, got %s" % (req_info.size, total_read)) return (400, "Invalid Request") # check hash if req_info.data_hash != infile_hash: log.error("Hash mismatch: expected '%s', got '%s'" % (req_info.data_hash, infile_hash)) return (400, "Invalid request") return (200, "OK")
def hash(self): """Return hash object for block.""" hash_object = Hash() hash_object.update(self.hash_pointer) hash_object.update(str(self.time_stamp).encode()) hash_object.update(self.comment.encode()) hash_object.update(str(self.counter).encode()) hash_object.update(str(self.difficulty).encode()) hash_object.update(self.nonce) return hash_object