class SaveAttachmentTest(MailgunTestBase):
    def setUp(self):
        super(SaveAttachmentTest, self).setUp()
        (filename, file_stream) = get_attachment()
        self.attachment = FileStorage(stream=file_stream,
                                      filename=filename)

    def tearDown(self):
        super(SaveAttachmentTest, self).tearDown()
        self.attachment.close()

    def test_fileallowed(self):
        self.assertTrue(self.mailgun.file_allowed('test.txt'))
        self.assertFalse(self.mailgun.file_allowed('bob'))

    def test_save_attachments(self):
        testdir = tempfile.mkdtemp()
        self.attachment.seek(0)
        filenames = self.mailgun.save_attachments([self.attachment], testdir)
        filenames = [os.path.basename(filename) for filename in filenames]
        self.assertTrue(set(os.listdir(testdir)) == set(filenames))
        self.assertEqual(len(os.listdir(testdir)), 1)
        shutil.rmtree(testdir)
        with self.assertRaises(OSError):
            os.listdir(testdir)
Exemple #2
0
def test_Transfer_preprocess(transf):
    @transf.preprocessor
    def to_upper(filehandle, meta):
        filehandle.stream = BytesIO(filehandle.stream.read().lower())
        return filehandle

    source = FileStorage(stream=BytesIO(b'Hello World'))
    transf._preprocess(source, {})
    source.seek(0)
    assert source.read() == b'hello world'
def test_Transfer_preprocess(transf):
    @transf.preprocessor
    def to_upper(filehandle, meta):
        filehandle.stream = BytesIO(filehandle.stream.read().lower())
        return filehandle

    source = FileStorage(stream=BytesIO(b'Hello World'))
    transf._preprocess(source, {})
    source.seek(0)
    assert source.read() == b'hello world'
Exemple #4
0
def ecc_encrypt():
    if request.method == 'GET':
        return render_template('ecc/encrypt/index.html')

    message = request.files['message'].read()
    filename = 'encrypted_' + request.files['message'].filename
    kob = int(request.form['koblitz'])

    coef = int(request.form['x-coef'])
    const = int(request.form['const'])
    modulo = int(request.form['modulo'])
    if not is_prime(modulo):
        return "Error: Modulo must be prime"
    curve = EllipticCurve(coef, const, modulo)

    base_x = int(request.form['bpoint-x'])
    base_y = int(request.form['bpoint-y'])
    bpoint = Point(base_x, base_y, modulo)
    if not curve.contains(bpoint):
        return "Error: The base point is not a point in the curve"

    pubkey = request.files.get('pubkey-file', None)
    if pubkey == None:
        pubkey_x = int(request.form['pubkey-x'])
        pubkey_y = int(request.form['pubkey-y'])
        pubkey = Point(pubkey_x, pubkey_y, modulo)
    else:
        pubkey = pubkey.read().decode()
        pubkey = Point(0, 0, modulo, pubkey)
    if not curve.contains(pubkey):
        return "Error: The public key is not a point in the curve"

    plaintext = encode(curve, message, kob)
    start = default_timer()
    k = randrange(1, modulo)
    ciphertext = [
        curve.add(point, curve.multiply(k, pubkey)) for point in plaintext
    ]
    ciphertext.append(curve.multiply(k, bpoint))
    end = default_timer()
    string = ' '.join([point.print() for point in ciphertext])

    file = FileStorage(stream=BytesIO(string.encode()), filename=filename)
    file.seek(0, SEEK_END)
    size = file.tell()
    file.seek(0)
    file.save('static/' + filename)
    return render_template('ecc/encrypt/result.html',
                           plaintext=message,
                           ciphertext=string,
                           time=end - start,
                           filename=filename,
                           size=size)
Exemple #5
0
def ecc_decrypt():
    if request.method == 'GET':
        return render_template('ecc/decrypt/index.html')

    message = request.files['message'].read()
    kob = int(request.form['koblitz'])

    coef = int(request.form['x-coef'])
    const = int(request.form['const'])
    modulo = int(request.form['modulo'])
    if not is_prime(modulo):
        return "Error: Modulo must be prime"
    curve = EllipticCurve(coef, const, modulo)

    prikey = request.files.get('prikey-file', None)
    if prikey == None:
        prikey = int(request.form['prikey'])
    else:
        prikey = int(prikey.read())

    raw_ciphertext = message.decode().split(' ')
    while raw_ciphertext[-1] == '':
        del raw_ciphertext[-1]
    ciphertext = [Point(0, 0, modulo, point) for point in raw_ciphertext]
    start = default_timer()
    point_neg = curve.multiply(prikey, ciphertext[-1])
    del ciphertext[-1]
    point_neg.negate()
    plaintext = [curve.add(point, point_neg) for point in ciphertext]
    end = default_timer()
    string = decode(plaintext, kob)

    filename = request.form['filename']
    file = FileStorage(stream=BytesIO(string), filename=filename)
    file.seek(0, SEEK_END)
    size = file.tell()
    file.seek(0)
    file.save('static/' + filename)
    return render_template('ecc/decrypt/result.html',
                           plaintext=string,
                           ciphertext=message.decode(),
                           time=end - start,
                           filename=filename,
                           size=size)
Exemple #6
0
def rsa_encrypt():
    if request.method == 'GET':
        return render_template('rsa/encrypt/index.html')

    if request.files['pubkey-file']:
        rsaPublicKey = RSAPublicKey.from_json(
            request.files['pubkey-file'].read())
    else:
        rsaPublicKey = RSAPublicKey(int(request.form['n']),
                                    int(request.form['e']))

    start = default_timer()

    n_byte_length = byte_length(rsaPublicKey.n)
    chunk_byte_length = n_byte_length - 1
    if chunk_byte_length <= 0:
        return 'Modulus too small'
    message = request.files['message'].read()
    plaintext = len(message).to_bytes(4, byteorder) + message
    plaintext_chunks = [
        plaintext[i:i + chunk_byte_length]
        for i in range(0, len(plaintext), chunk_byte_length)
    ]
    ciphertext = bytes()
    for chunk in plaintext_chunks:
        ciphertext += rsaPublicKey.encrypt(int.from_bytes(
            chunk, byteorder)).to_bytes(n_byte_length, byteorder)

    end = default_timer()

    filename = 'encrypted_' + request.files['message'].filename
    ciphertext_file = FileStorage(stream=BytesIO(ciphertext),
                                  filename=filename)
    ciphertext_file.seek(0, SEEK_END)
    size = ciphertext_file.tell()
    ciphertext_file.seek(0)
    ciphertext_file.save('static/' + filename)

    return render_template('rsa/encrypt/result.html',
                           plaintext=message,
                           ciphertext=ciphertext,
                           time=end - start,
                           filename=filename,
                           size=size)
Exemple #7
0
def rsa_decrypt():
    if request.method == 'GET':
        return render_template('rsa/decrypt/index.html')

    if request.files['prikey-file']:
        rsaPrivateKey = RSAPrivateKey.from_json(
            request.files['prikey-file'].read())
    else:
        rsaPrivateKey = RSAPrivateKey(int(request.form['n']),
                                      int(request.form['d']))

    start = default_timer()

    n_byte_length = byte_length(rsaPrivateKey.n)
    if n_byte_length <= 1:
        return 'Modulus too small'
    ciphertext = request.files['message'].read()
    ciphertext_chunks = [
        ciphertext[i:i + n_byte_length]
        for i in range(0, len(ciphertext), n_byte_length)
    ]
    plaintext = bytes()
    for chunk in ciphertext_chunks:
        plaintext += rsaPrivateKey.decrypt(int.from_bytes(
            chunk, byteorder)).to_bytes(n_byte_length, byteorder)[:-1]
    plaintext = plaintext[4:int.from_bytes(plaintext[:4], byteorder) + 4]

    end = default_timer()

    filename = 'decrypted_' + request.files['message'].filename
    plaintext_file = FileStorage(stream=BytesIO(plaintext), filename=filename)
    plaintext_file.seek(0, SEEK_END)
    size = plaintext_file.tell()
    plaintext_file.seek(0)
    plaintext_file.save('static/' + filename)

    return render_template('rsa/decrypt/result.html',
                           plaintext=plaintext,
                           ciphertext=ciphertext,
                           time=end - start,
                           filename=filename,
                           size=size)
class SaveAttachmentTest(MailgunTestBase):
    def setUp(self):
        super(SaveAttachmentTest, self).setUp()
        (filename, file_stream) = get_attachment()
        self.attachment = FileStorage(stream=file_stream, filename=filename)

    def tearDown(self):
        super(SaveAttachmentTest, self).tearDown()
        self.attachment.close()

    def test_fileallowed(self):
        self.assertTrue(self.mailgun._is_file_allowed('test.txt'))
        self.assertFalse(self.mailgun._is_file_allowed('bob'))

    def test_save_attachments(self):
        testdir = tempfile.mkdtemp()
        self.attachment.seek(0)
        filenames = save_attachments([self.attachment], testdir)
        filenames = [os.path.basename(filename) for filename in filenames]
        self.assertTrue(set(os.listdir(testdir)) == set(filenames))
        self.assertEqual(len(os.listdir(testdir)), 1)
        shutil.rmtree(testdir)
        with self.assertRaises(OSError):
            os.listdir(testdir)