예제 #1
0
        public void ShouldEncryptFile()
        {
            var file = Guid.NewGuid().ToString();

            File.WriteAllText(file, "MyPassword = \"ABC\"");

            var key = Guid.NewGuid().ToString();

            var subject = new ConfigToolImplementation(new List <string> {
                file
            }, false,
                                                       key, new List <string>
            {
                ".+Password.+"
            });

            subject.Encrypt();

            var table = Toml.Parse(File.ReadAllText(file));

            var keeper = new SecretKeeper(key);

            Check.That(keeper.Decrypt(table.ToModel()["MyPassword"].ToString()))
            .IsEqualTo("ABC");
        }
예제 #2
0
        public void ShouldFailWithErrorOnCorruptedCypher()
        {
            var sc = new SecretKeeper(Security.GenerateKeyAsString());

            Check.ThatCode(() => sc.Decrypt(Convert.ToBase64String(Encoding.UTF8.GetBytes("Not a cypher"))))
            .Throws <TomlConfigurationException>()
            .AndWhichMessage()
            .Contains("corrupted");
        }
예제 #3
0
        public void ShouldDoSecretRoundTrip()
        {
            var sc = new SecretKeeper(Security.GenerateKeyAsString());

            var iLovePink = "I love pink!";

            var cypher = sc.Encrypt(iLovePink);

            Check.That(sc.Decrypt(cypher))
            .IsEqualTo(iLovePink);
        }
예제 #4
0
        private void VerifyValue(string cypherValue, string keyName)
        {
            var secretKeeper = new SecretKeeper(masterKey);

            try
            {
                secretKeeper.Decrypt(cypherValue);
            }
            catch (Exception ex)
            {
                Console.Error.WriteLine($"Failed to decrypt {keyName} from value '{cypherValue}' Error:" + ex.Message);
            }
        }
예제 #5
0
        private bool DecryptValue(string cypherValue, out string clearValue)
        {
            var secretKeeper = new SecretKeeper(masterKey);

            if (secretKeeper.IsValidCypher(cypherValue, out var thumb, out _))
            {
                secretKeeper.AssertSecretThumbnail(thumb);
                clearValue = secretKeeper.Decrypt(cypherValue);
                return(true);
            }

            clearValue = null;
            return(false);
        }
예제 #6
0
        public void ShouldFailWithExceptionIfMasterKeyIsNotMatched()
        {
            var sc = new SecretKeeper(Security.GenerateKeyAsString());

            var iLovePink = "I love pink!";

            var cypher = sc.Encrypt(iLovePink);

            sc = new SecretKeeper(Security.GenerateKeyAsString());

            Check.ThatCode(() => sc.Decrypt(cypher))
            .Throws <TomlConfigurationException>()
            .AndWhichMessage()
            .Contains("thumbnail");
        }