示例#1
0
def testEncryptDecryptMAC():
    """
    Verify that an encrypted plain text decrypts to the same text given the
    same key and MAC Key.

    plain_text should decrypt correctly using the same key and MAC key.
    decrypt should raise an exception when using a different MAC key.
    """
    plain_text = 'This is another test'
    input_key = Bitwarden.makeKey('password', '*****@*****.**')
    encrypt_key = Bitwarden.makeEncryptionKey(input_key)
    whole_key = Bitwarden.decrypt(encrypt_key, input_key)
    test_key = whole_key[:32]
    mac_key1 = whole_key[32:64]
    mac_key2 = whole_key[:32]

    cipher_string = Bitwarden.encrypt(plain_text, test_key, mac_key1)

    assert plain_text == Bitwarden.decrypt(cipher_string, test_key,
                                           mac_key1).decode()
    assert plain_text == Bitwarden.decrypt(
        str(CipherString.parseString(cipher_string)), test_key,
        mac_key1).decode()

    with pytest.raises(InvalidMACException):
        Bitwarden.decrypt(cipher_string, test_key, mac_key2)
        Bitwarden.decrypt(str(CipherString.parseString(cipher_string)),
                          test_key, mac_key2)
示例#2
0
def testMakeEncryptionKey():
    """
    Verify that unique encryption keys are generated, even if the keys
    are not unique.

    To test, we will make 100 encryption keys and store them in a list, then
    verify that none are unique by making them into a set and comparing the
    lengths of the list and set.
    """
    test_password = '******'
    test_salt = 'salt'

    test_key = Bitwarden.makeKey(test_password, test_salt)
    test_keys = [Bitwarden.makeEncryptionKey(test_key) for x in range(100)]

    assert len(test_keys) is len(set(test_keys))