Exemplo n.º 1
0
    public void GenerateNonce_Returns24ByteArray()
    {
        // Arrange

        // Act
        var nonce = CryptoF.GenerateNonce();

        // Assert
        Assert.Equal(24, nonce.Length);
    }
Exemplo n.º 2
0
    public void Without_Type_Null_Input_Byte_Key_Returns_Empty(string input)
    {
        // Arrange
        var key = CryptoF.GenerateKey().UnsafeUnwrap();

        // Act
        var result = input.Decrypt(key);

        // Assert
        Assert.Equal(string.Empty, result);
    }
Exemplo n.º 3
0
    public void Without_Type_Incorrect_Byte_Key_Returns_Empty()
    {
        // Arrange
        var key = CryptoF.GenerateKey().UnsafeUnwrap();

        // Act
        var result = defaultInputStringEncryptedWithByteKey.Decrypt(key);

        // Assert
        Assert.Equal(string.Empty, result);
    }
Exemplo n.º 4
0
    public void Null_Input_Byte_Key_Returns_None(string input)
    {
        // Arrange
        var key = CryptoF.GenerateKey().UnsafeUnwrap();

        // Act
        var result = input.Decrypt <int>(key);

        // Assert
        result.AssertNone().AssertType <DeserialisingNullOrEmptyStringMsg>();
    }
Exemplo n.º 5
0
    public void Incorrect_Byte_Key_Returns_None()
    {
        // Arrange
        var key = CryptoF.GenerateKey().UnsafeUnwrap();

        // Act
        var result = defaultInputStringEncryptedWithByteKey.Decrypt <string>(key);

        // Assert
        result.AssertNone().AssertType <IncorrectKeyOrNonceExceptionMsg>();
    }
Exemplo n.º 6
0
    public void GenerateNonce_ReturnsUniqueValues()
    {
        // Arrange

        // Act
        var nonce1 = CryptoF.GenerateNonce();
        var nonce2 = CryptoF.GenerateNonce();

        // Assert
        Assert.NotEqual(nonce1, nonce2);
    }
Exemplo n.º 7
0
    public void Without_Type_Invalid_Json_Input_Byte_Key_Returns_Empty()
    {
        // Arrange
        var key  = CryptoF.GenerateKey().UnsafeUnwrap();
        var json = Rnd.Str;

        // Act
        var result = json.Decrypt(key);

        // Assert
        Assert.Equal(string.Empty, result);
    }
Exemplo n.º 8
0
    public void No_EncryptedContents_Returns_None_With_UnlockWhenEncryptedContentsIsNullMsg()
    {
        // Arrange
        var box = new Locked <int>();
        var key = CryptoF.GenerateKey().UnsafeUnwrap();

        // Act
        var result = box.Unlock(key);

        // Assert
        result.AssertNone().AssertType <UnlockWhenEncryptedContentsIsNoneMsg>();
    }
Exemplo n.º 9
0
    public void Incorrect_Json_Input_Byte_Key_Returns_None()
    {
        // Arrange
        var          key  = CryptoF.GenerateKey().UnsafeUnwrap();
        const string json = /*lang=json,strict*/ "{\"foo\":\"bar\"}";

        // Act
        var result = json.Decrypt <int>(key);

        // Assert
        result.AssertNone().AssertType <UnlockWhenEncryptedContentsIsNoneMsg>();
    }
Exemplo n.º 10
0
    public void Invalid_Json_Input_Byte_Key_Returns_None()
    {
        // Arrange
        var key  = CryptoF.GenerateKey().UnsafeUnwrap();
        var json = Rnd.Str;

        // Act
        var result = json.Decrypt <int>(key);

        // Assert
        result.AssertNone().AssertType <DeserialiseExceptionMsg>();
    }
Exemplo n.º 11
0
    public void Includes_A_Number()
    {
        // Arrange

        // Act
        var result = CryptoF.GeneratePassphrase();

        // Assert
        var some = result.AssertSome();

        Assert.Contains(some, x => char.IsNumber(x));
    }
Exemplo n.º 12
0
    public void Uses_Correct_Number_Of_Words(int input)
    {
        // Arrange

        // Act
        var result = CryptoF.GeneratePassphrase(input);

        // Assert
        var some = result.AssertSome().Split('-');

        Assert.Equal(input, some.Length);
    }
Exemplo n.º 13
0
    public void Makes_First_Letters_Upper_Case()
    {
        // Arrange

        // Act
        var result = CryptoF.GeneratePassphrase();

        // Assert
        var some = result.AssertSome();

        Assert.NotEqual(some, some.ToLower());
    }
Exemplo n.º 14
0
    public void NumberOfWords_Less_Than_Five_Returns_None_With_CryptographicallySecurePassphrasesMustContainAtLeastFiveWordsMsg(int input)
    {
        // Arrange

        // Act
        var result = CryptoF.GeneratePassphrase(input);

        // Assert
        var none = result.AssertNone().AssertType <CryptographicallySecurePassphrasesMustContainAtLeastFiveWordsMsg>();

        Assert.Equal(input, none.Value);
    }
Exemplo n.º 15
0
    public void Uses_Eight_Words()
    {
        // Arrange

        // Act
        var result = CryptoF.GeneratePassphrase();

        // Assert
        var some = result.AssertSome().Split('-');

        Assert.Equal(8, some.Length);
    }
Exemplo n.º 16
0
    public void GenerateKey_Returns32ByteArray()
    {
        // Arrange

        // Act
        var result = CryptoF.GenerateKey();

        // Assert
        var some = result.AssertSome();

        Assert.Equal(32, some.Length);
    }
Exemplo n.º 17
0
    public void Without_Type_Incorrect_Json_Input_Byte_Key_Returns_Empty()
    {
        // Arrange
        var          key  = CryptoF.GenerateKey().UnsafeUnwrap();
        const string json = /*lang=json,strict*/ "{\"foo\":\"bar\"}";

        // Act
        var result = json.Decrypt(key);

        // Assert
        Assert.Equal(string.Empty, result);
    }
Exemplo n.º 18
0
    public void Invalid_Key_Returns_None_With_InvalidKeyExceptionMsg()
    {
        // Arrange
        var value = Rnd.Str;
        var key   = CryptoF.GenerateKey().UnsafeUnwrap();
        var box   = new Locked <string>(value, key);

        // Act
        var result = box.Unlock(Rnd.ByteF.Get(16));

        // Assert
        result.AssertNone().AssertType <InvalidKeyExceptionMsg>();
    }
Exemplo n.º 19
0
    public void Incorrect_Key_Returns_None_With_IncorrectKeyOrNonceMsg()
    {
        // Arrange
        var value        = Rnd.Str;
        var key          = CryptoF.GenerateKey().UnsafeUnwrap();
        var incorrectKey = CryptoF.GenerateKey().UnsafeUnwrap();
        var box          = new Locked <string>(value, key);

        // Act
        var result = box.Unlock(incorrectKey);

        // Assert
        result.AssertNone().AssertType <IncorrectKeyOrNonceExceptionMsg>();
    }
Exemplo n.º 20
0
    public void GenerateKey_ReturnsUniqueValues()
    {
        // Arrange

        // Act
        var r0 = CryptoF.GenerateKey();
        var r1 = CryptoF.GenerateKey();

        // Assert
        var s0 = r0.AssertSome();
        var s1 = r1.AssertSome();

        Assert.NotEqual(s0, s1);
    }
Exemplo n.º 21
0
    public void Byte_Key_Returns_Lockable()
    {
        // Arrange
        var value = Rnd.Str;
        var key   = CryptoF.GenerateKey().UnsafeUnwrap();
        var box   = new Locked <string>(value, key);

        // Act
        var result = box.Unlock(key);

        // Assert
        var some = result.AssertSome();

        Assert.Equal(value, some.Contents);
    }
Exemplo n.º 22
0
    public void Byte_Key_Fills_With_Encrypted_Contents()
    {
        // Arrange
        var value = Rnd.Str;
        var key   = CryptoF.GenerateKey().UnsafeUnwrap();

        // Act
        var result = new Locked <string>(value, key);

        // Assert
        Assert.NotEmpty(result.Salt);
        Assert.NotEmpty(result.Nonce);
        var some = result.EncryptedContents.AssertSome();

        Assert.NotEmpty(some);
    }
Exemplo n.º 23
0
    public void Returns_Json()
    {
        // Arrange
        var value = Rnd.Str;
        var key   = CryptoF.GenerateKey().UnsafeUnwrap();
        var box   = new Locked <string>(value, key);
        var json  = string.Format("{{\"encryptedContents\":\"{0}\",\"salt\":\"{1}\",\"nonce\":\"{2}\"}}",
                                  Convert.ToBase64String(box.EncryptedContents.UnsafeUnwrap()),
                                  Convert.ToBase64String(box.Salt),
                                  Convert.ToBase64String(box.Nonce)
                                  );

        // Act
        var result = box.Serialise();

        // Assert
        var some = result.AssertSome();

        Assert.Equal(json, some);
    }
Exemplo n.º 24
0
 string input when !string.IsNullOrWhiteSpace(input) =>
 CryptoF.Hash(@this, bytes)
 .Map(
     x => Convert.ToBase64String(x),
     F.DefaultHandler
     ),