Пример #1
0
        public async Task TestSimpleCreateAsymmetricKeysStore()
        {
            FakeDataStore.AddFolder(@"C:\Temp");
            IDataContainer workFolder  = New <IDataContainer>(@"C:\Temp");
            AccountStorage store       = new AccountStorage(new LocalAccountService(new LogOnIdentity(EmailAddress.Parse(@"*****@*****.**"), new Passphrase("secret")), workFolder));
            UserKeyPair    userKeyPair = new UserKeyPair(EmailAddress.Parse("*****@*****.**"), 512);

            await store.ImportAsync(userKeyPair);

            Assert.That((await store.AllKeyPairsAsync()).First().KeyPair.PrivateKey, Is.Not.Null);
            Assert.That((await store.AllKeyPairsAsync()).First().KeyPair.PublicKey, Is.Not.Null);
        }
Пример #2
0
        private async Task <object> CreateAccountAction()
        {
            if (String.IsNullOrEmpty(UserEmail))
            {
                return(null);
            }

            AccountStorage accountStorage = new AccountStorage(New <LogOnIdentity, IAccountService>(new LogOnIdentity(EmailAddress.Parse(UserEmail), new Passphrase(PasswordText))));
            UserKeyPair    userKeys       = new UserKeyPair(EmailAddress.Parse(UserEmail), New <INow>().Utc, New <KeyPairService>().New());
            await accountStorage.ImportAsync(userKeys);

            return(null);
        }
        public async Task <UserKeyPair> CurrentKeyPairAsync()
        {
            if (Identity.UserEmail == EmailAddress.Empty)
            {
                throw new InvalidOperationException("The account service requires a user.");
            }

            UserAccount userAccount = LoadUserAccount();
            UserKeyPair keyPair     = userAccount.AccountKeys.Select(ak => ak.ToUserKeyPair(Identity.Passphrase)).OrderByDescending(ukp => ukp.Timestamp).FirstOrDefault();

            if (keyPair == null)
            {
                AccountStorage store = new AccountStorage(New <LogOnIdentity, IAccountService>(Identity));
                keyPair = new UserKeyPair(Identity.UserEmail, New <INow>().Utc, New <KeyPairService>().New());
                await store.ImportAsync(keyPair);
            }

            return(keyPair);
        }
Пример #4
0
        private async Task ImportFileActionAsync()
        {
            IDataStore  privateKeyData = New <IDataStore>(PrivateKeyFileName);
            Passphrase  passphrase     = new Passphrase(PasswordText);
            UserKeyPair keyPair;

            if (!UserKeyPair.TryLoad(privateKeyData.ToArray(), passphrase, out keyPair))
            {
                ImportSuccessful = false;
                return;
            }

            LogOnIdentity  identity = new LogOnIdentity(keyPair.UserEmail, passphrase);
            AccountStorage store    = new AccountStorage(New <LogOnIdentity, IAccountService>(identity));
            await store.ImportAsync(keyPair);

            ImportSuccessful = true;

            _userSettings.UserEmail = keyPair.UserEmail.Address;
            await _knownIdentities.SetDefaultEncryptionIdentity(new LogOnIdentity(await store.AllKeyPairsAsync(), passphrase));
        }
Пример #5
0
        public async Task TestEncryptCreateLoadDecryptWithAsymmetricKeysStore()
        {
            FakeDataStore.AddFolder(@"C:\Temp");
            IDataContainer workFolder = New <IDataContainer>(@"C:\Temp\");
            AccountStorage store      = new AccountStorage(new LocalAccountService(new LogOnIdentity(EmailAddress.Parse(@"*****@*****.**"), new Passphrase("secret")), workFolder));
            await Resolve.KnownIdentities.SetDefaultEncryptionIdentity(new LogOnIdentity("secret"));

            UserKeyPair userKeyPair = new UserKeyPair(EmailAddress.Parse("*****@*****.**"), 512);

            await store.ImportAsync(userKeyPair);

            string text = "AxCrypt encryption rules!";

            byte[] encryptedBytes = (await store.AllKeyPairsAsync()).First().KeyPair.PublicKey.Transform(Encoding.UTF8.GetBytes(text));

            store = new AccountStorage(new LocalAccountService(new LogOnIdentity(EmailAddress.Parse(@"*****@*****.**"), new Passphrase("secret")), workFolder));

            byte[] decryptedBytes = (await store.AllKeyPairsAsync()).First().KeyPair.PrivateKey.Transform(encryptedBytes);
            Assert.That(decryptedBytes != null);
            string decryptedText = Encoding.UTF8.GetString(decryptedBytes);

            Assert.That(text, Is.EqualTo(decryptedText));
        }