def test_aes(self): crypto.DEBUG = True crypto.encrypt("this is a test", Random.bytes(32)) crypto.encrypt("this is a longer test with more than 16bytes", Random.bytes(32)) crypto.encrypt("", Random.bytes(32)) crypto.encrypt(convert.latin12unicode(b"testing accented char àáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþÿ"), Random.bytes(32)) crypto.encrypt("testing accented char àáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþÿ", Random.bytes(32))
def test_public_request(self): # MAKE SOME DATA data = { "a": { # MATCHES SERVER PATTERN "b": "good", "c": [ {"k": "good", "m": 1}, {"k": 2, "m": 2} ] }, "constant": "this is a test", "random-data": convert.bytes2base64(Random.bytes(100)) } content = json.dumps(data) response = requests.post( url=settings.url, data=content, headers={ 'Content-Type': CONTENT_TYPE } ) self.assertEqual(response.status_code, 200, "Expecting 200") about = json.loads(response.content) return about['link'], about['etl']['id'] Log.note("Data located at {{link}} id={{id}}", link=link, id=id)
def test_request(self): # MAKE SOME DATA data = { "constant": "this is a test", "random-data": convert.bytes2base64(Random.bytes(100)) } client = Client(settings.url, unwrap(settings.hawk)) # unwrap() DUE TO BUG https://github.com/kumar303/mohawk/issues/21 link, id = client.send(data) Log.note("Success! Located at {{link}} id={{id}}", link=link, id=id) # FILL THE REST OF THE FILE Log.note("Add ing {{num}} more...", num=99-id) for i in range(id + 1, storage.BATCH_SIZE): l, k = client.send(data) if l != link: Log.error("Expecting rest of data to have same link") # TEST LINK HAS DATA raw_content = requests.get(link).content content = convert.zip2bytes(raw_content) for line in convert.utf82unicode(content).split("\n"): data = convert.json2value(line) if data.etl.id == id: Log.note("Data {{id}} found", id=id) break else: Log.error("Expecting to find data at link")
def test_public_request_too_big(self): # MAKE SOME DATA data = { "a": { # MATCHES SERVER PATTERN "b": "good", "c": [ {"k": "good", "m": 1}, {"k": 2, "m": 2} ] }, "constant": "this is a test", "random-data": convert.bytes2base64(Random.bytes(500)) } content = json.dumps(data) def poster(): response = requests.post( url=settings.url, data=content, headers={ 'Content-Type': CONTENT_TYPE } ) self.assertEqual(response.status_code, 200, "Expecting 200") self.assertRaises(Exception, poster)
def test_missing_auth(self): # MAKE SOME DATA data = { "constant": "this is a test", "random-data": convert.bytes2base64(Random.bytes(100)) } response = requests.post(settings.bad_url, data=convert.unicode2utf8(convert.value2json(data))) self.assertEqual(response.status_code, 403)
def encrypt(text, _key, salt=None): """ RETURN JSON OF ENCRYPTED DATA {"salt":s, "length":l, "data":d} """ from pyLibrary.queries import jx if not isinstance(text, unicode): Log.error("only unicode is encrypted") if _key is None: Log.error("Expecting a key") if isinstance(_key, str): _key = bytearray(_key) if salt is None: salt = Random.bytes(16) data = bytearray(text.encode("utf8")) # 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(salt) output = Data() output.type = "AES256" output.salt = convert.bytes2base64(salt) output.length = len(data) encrypted = bytearray() for _, d in jx.groupby(data, size=16): encrypted.extend(aes_cbc_256.encrypt_block(d)) output.data = convert.bytes2base64(encrypted) json = convert.value2json(output) if DEBUG: test = decrypt(json, _key) if test != text: Log.error("problem with encryption") return json
def encrypt(text, _key, salt=None): """ RETURN JSON OF ENCRYPTED DATA {"salt":s, "length":l, "data":d} """ from pyLibrary.queries import jx if not isinstance(text, unicode): Log.error("only unicode is encrypted") if _key is None: Log.error("Expecting a key") if isinstance(_key, str): _key = bytearray(_key) if salt is None: salt = Random.bytes(16) data = bytearray(text.encode("utf8")) # 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(salt) output = Dict() output.type = "AES256" output.salt = convert.bytes2base64(salt) output.length = len(data) encrypted = bytearray() for _, d in jx.groupby(data, size=16): encrypted.extend(aes_cbc_256.encrypt_block(d)) output.data = convert.bytes2base64(encrypted) json = convert.value2json(output) if DEBUG: test = decrypt(json, _key) if test != text: Log.error("problem with encryption") return json
import requests from pyLibrary import convert, jsons from pyLibrary.debugs.logs import Log from pyLibrary.dot import unwrap from pyLibrary.maths.randoms import Random from modatasubmission import Client settings = jsons.ref.get("file://~/MoDataSubmissionClient.json") data={ "constant": "this is a test", "random-data": convert.bytes2base64(Random.bytes(100)) } link, id = Client(settings.url, unwrap(settings.hawk)).send(data) Log.note("Success! Located at {{link}} id={{id}}", link=link, id=id) data = convert.unicode2utf8(convert.value2json(settings.example)) response = requests.post( settings.url, data=data, headers={ 'Content-Type': b'application/json' } ) if response.status_code != 200: