public void CorrectlyConvertsSecureStringToUnicodeBytes() { string candidate = "lazy 🐢🖐🏿 doc."; SecureString secureCandidate = SecureStringExtensions.StringToSecureString(candidate); byte[] bytes = SecureStringExtensions.SecureStringToBytes(secureCandidate, Encoding.Unicode); Assert.AreEqual("lazy 🐢🖐🏿 doc.", Encoding.Unicode.GetString(bytes)); }
public void Version1ConfigWillBeUpdated() { XDocument xml = XDocument.Parse(Version1Settings); Mock <IXmlFileService> fileService = new Mock <IXmlFileService>(); fileService. Setup(m => m.TryLoad(It.IsAny <string>(), out xml)). Returns(true); Mock <IDataProtectionService> dataProtectionService = new Mock <IDataProtectionService>(); dataProtectionService. Setup(m => m.Protect(It.Is <byte[]>(p => p.SequenceEqual(Encoding.ASCII.GetBytes("abcdefgh"))))). Returns("protected_abcdefgh_v2"); dataProtectionService. Setup(m => m.Unprotect(It.Is <string>(p => p == "protected_abcdefgh_v2"))). Returns(SecureStringExtensions.SecureStringToBytes(SecureStringExtensions.StringToSecureString("abcdefgh_v2"), Encoding.Unicode)); dataProtectionService. Setup(m => m.Protect(It.Is <byte[]>(p => p.SequenceEqual(Encoding.UTF8.GetBytes("abcdefgh_v2"))))). Returns("protected_abcdefgh_v3"); dataProtectionService. Setup(m => m.Unprotect(It.Is <string>(p => p == "protected_abcdefgh_v3"))). Returns(Encoding.UTF8.GetBytes("abcdefgh_v3")); dataProtectionService. Setup(m => m.Protect(It.Is <byte[]>(p => p.SequenceEqual(Encoding.UTF8.GetBytes("martinstoeckli"))))). Returns("protected_martinstoeckli_v3"); dataProtectionService. Setup(m => m.Unprotect(It.Is <string>(p => p == "protected_martinstoeckli_v3"))). Returns(Encoding.UTF8.GetBytes("martinstoeckli_v3")); SettingsServiceBase service = new TestableService(fileService.Object, dataProtectionService.Object); SettingsModel settings = service.LoadSettingsOrDefault(); Assert.AreEqual("twofish_gcm", settings.SelectedEncryptionAlgorithm); Assert.AreEqual(true, settings.AdoptCloudEncryptionAlgorithm); Assert.AreEqual("scuj2wfpdcodmgzm", settings.TransferCode); Assert.AreEqual(2, settings.TransferCodeHistory.Count); Assert.AreEqual("scuj2wfpdcodmgzm", settings.TransferCodeHistory[0]); Assert.AreEqual("b2bgiqeghfvn2ufx", settings.TransferCodeHistory[1]); Assert.AreEqual(SettingsModel.NewestSupportedRevision, settings.Revision); Assert.IsNotNull(settings.Credentials); Assert.AreEqual(CloudStorageClientFactory.CloudStorageIdWebdav, settings.Credentials.CloudStorageId); Assert.AreEqual("https://webdav.hidrive.strato.com/users/martinstoeckli/", settings.Credentials.Url); Assert.AreEqual("martinstoeckli_v3", settings.Credentials.Username); Assert.AreEqual("abcdefgh_v3", settings.Credentials.UnprotectedPassword); // Updated settings where saved fileService.Verify(m => m.TrySerializeAndSave(It.IsAny <string>(), It.IsAny <SettingsModel>()), Times.Once); }
/// <inheritdoc /> public byte[] DeriveKeyFromPassword(SecureString password, int expectedKeySizeBytes, byte[] salt, int cost) { if ((password == null) || (password.Length == 0)) { throw new CryptoException("The password cannot be empty."); } if (cost < 1) { throw new CryptoException("The cost factor is too small."); } byte[] binaryPassword = SecureStringExtensions.SecureStringToBytes(password, Encoding.UTF8); try { Rfc2898DeriveBytes kdf = new Rfc2898DeriveBytes(binaryPassword, salt, cost); byte[] result = kdf.GetBytes(expectedKeySizeBytes); return(result); } finally { CryptoUtils.CleanArray(binaryPassword); } }