def decrypt(data, _key): """ ACCEPT BYTES -> UTF8 -> JSON -> {"salt":s, "length":l, "data":d} """ # Key and iv have not been generated or provided, bail out if _key is None: Log.error("Expecting a key") _input = get_module("mo_json").json2value(data.decode('utf8'), leaves=False, flexible=False) # Initialize encryption using key and iv key_expander_256 = key_expander.KeyExpander(256) expanded_key = key_expander_256.expand(_key) aes_cipher_256 = aes_cipher.AESCipher(expanded_key) aes_cbc_256 = cbc_mode.CBCMode(aes_cipher_256, 16) aes_cbc_256.set_iv(base642bytearray(_input.salt)) raw = base642bytearray(_input.data) out_data = bytearray() for _, e in _groupby16(raw): out_data.extend(aes_cbc_256.decrypt_block(e)) if _input.encoding: return binary_type(out_data[:_input.length:]).decode(_input.encoding) else: return binary_type(out_data[:_input.length:])
def decrypt(data, _key): """ ACCEPT BYTES -> UTF8 -> JSON -> {"salt":s, "length":l, "data":d} """ # Key and iv have not been generated or provided, bail out if _key is None: Log.error("Expecting a key") _input = get_module("mo_json").json2value(data.decode("utf8"), leaves=False, flexible=False) # Initialize encryption using key and iv key_expander_256 = key_expander.KeyExpander(256) expanded_key = key_expander_256.expand(_key) aes_cipher_256 = aes_cipher.AESCipher(expanded_key) aes_cbc_256 = cbc_mode.CBCMode(aes_cipher_256, 16) aes_cbc_256.set_iv(base642bytes(_input.salt)) raw = base642bytes(_input.data) out_data = bytearray() for _, e in _groupby16(raw): out_data.extend(aes_cbc_256.decrypt_block(e)) if _input.encoding: return binary_type(out_data[:_input.length:]).decode(_input.encoding) else: return binary_type(out_data[:_input.length:])
def bytes2base64URL(value): """ RETURN URL-FRIENDLY VERSION OF BASE64 """ if isinstance(value, bytearray): value = binary_type(value) return base64.b64encode(value, b"-_").rstrip(b"=").decode("latin1")
def bytes2base64(value): if isinstance(value, bytearray): value = binary_type(value) return base64.b64encode(value).decode("latin1")
def jx_query(path): with RegisterThread(): try: with Timer("total duration") as query_timer: preamble_timer = Timer("preamble", silent=True) with preamble_timer: if flask.request.headers.get("content-length", "") in ["", "0"]: # ASSUME A BROWSER HIT THIS POINT, SEND text/html RESPONSE BACK return Response(BLANK, status=400, headers={"Content-Type": "text/html"}) elif int(flask.request.headers["content-length"] ) > QUERY_SIZE_LIMIT: Log.error(QUERY_TOO_LARGE) request_body = flask.request.get_data().strip() text = utf82unicode(request_body) data = json2value(text) record_request(flask.request, data, None, None) if data.meta.testing: test_mode_wait(data) translate_timer = Timer("translate", silent=True) with translate_timer: frum = find_container(data['from']) result = jx.run(data, container=frum) if isinstance( result, Container ): #TODO: REMOVE THIS CHECK, jx SHOULD ALWAYS RETURN Containers result = result.format(data.format) save_timer = Timer("save") with save_timer: if data.meta.save: try: result.meta.saved_as = save_query.query_finder.save( data) except Exception as e: Log.warning("Unexpected save problem", cause=e) result.meta.timing.preamble = mo_math.round( preamble_timer.duration.seconds, digits=4) result.meta.timing.translate = mo_math.round( translate_timer.duration.seconds, digits=4) result.meta.timing.save = mo_math.round( save_timer.duration.seconds, digits=4) result.meta.timing.total = "{{TOTAL_TIME}}" # TIMING PLACEHOLDER with Timer("jsonification", silent=True) as json_timer: response_data = unicode2utf8(value2json(result)) with Timer("post timer", silent=True): # IMPORTANT: WE WANT TO TIME OF THE JSON SERIALIZATION, AND HAVE IT IN THE JSON ITSELF. # WE CHEAT BY DOING A (HOPEFULLY FAST) STRING REPLACEMENT AT THE VERY END timing_replacement = ( b'"total":' + binary_type( mo_math.round(query_timer.duration.seconds, digits=4)) + b', "jsonification":' + binary_type( mo_math.round(json_timer.duration.seconds, digits=4))) response_data = response_data.replace( b'"total":"{{TOTAL_TIME}}"', timing_replacement) Log.note("Response is {{num}} bytes in {{duration}}", num=len(response_data), duration=query_timer.duration) return Response( response_data, status=200, headers={"Content-Type": result.meta.content_type}) except Exception as e: e = Except.wrap(e) return send_error(query_timer, request_body, e)
def bytes2base64(value): if isinstance(value, bytearray): value = binary_type(value) return base64.b64encode(value).decode("utf8")