def test_encryption_with_empty_message():
    """
    Test if encryption return error to request with no message
    """
    try:
        EncryptionController.encrypt('')
        raise Exception('ValueError exception not raised')
    except ValueError:
        pass
def test_decryption(client):
    """
    Test if decryption return correct data in correct format
    """
    # Encrypt data
    message = 'Hello world.'
    key = EncryptionController.encrypt(message)
    decrypted_message = EncryptionController.decrypt(key)

    assert decrypted_message == message
def test_decryption_with_wrong_key():
    """
    Test if encryption return error to request with empty message
    """
    message = EncryptionController.decrypt('1234567890')

    assert message is None
def test_encryption_with_positive_ttl(client):
    """
    Test if encryption return data in correct format
    """
    key = EncryptionController.encrypt('Hello world.', 5)

    assert key is not None

    # Check if there is hash-table with specified key
    key_hash = hashlib.sha256(key).hexdigest()

    # Try to find data in Redis by key hash
    data = redis.hgetall(key_hash)

    assert data is not None
    assert b'nonce' in data
    assert b'ciphertext' in data
    assert b'tag' in data

    # Check expiration time
    assert redis.ttl(key_hash) > 0

    # Wait until key should expire
    time.sleep(5)

    data = redis.hgetall(key_hash)

    # redis.hgetall() returns empty dict when key not exists
    assert data == dict()
Exemple #5
0
def decrypt_message(key):
    message = EncryptionController.decrypt(key)

    if not message:
        return jsonify({'error': 'This message is not available'}), 404

    return jsonify({'message': message}), 200
Exemple #6
0
def encrypt_message():
    message = request.json.get('message', '').strip()
    ttl = request.json.get('ttl', 24*60*60)  # By default store 1 day

    if not message:
        return jsonify({'error': 'No message specified'}), 400

    try:
        key = EncryptionController.encrypt(message, ttl)
    except Exception as err:
        if current_app.config['DEBUG']:
            return jsonify({'error': str(err)}), 500
        else:
            # Do not show details in production mode
            return jsonify({'error': 'Something goes wrong on our side'}), 500

    return jsonify({'key': key.decode('utf-8')}), 200
def test_encryption(client):
    """
    Test if encryption return data in correct format
    """
    key = EncryptionController.encrypt('Hello world.')

    assert key is not None

    # Check if there is hash-table with specified key
    key_hash = hashlib.sha256(key).hexdigest()

    # Try to find data in Redis by key hash
    data = redis.hgetall(key_hash)

    assert data is not None
    assert b'nonce' in data
    assert b'ciphertext' in data
    assert b'tag' in data