public static async Task <PersonalCard> LoadLatest(IdentityInfo token, string privateKeyPassword = null) { var services = ServiceLocator.Services; var searchResult = await services.Cards.Search(token.Value, token.Type) .ConfigureAwait(false); var card = searchResult .OrderByDescending(it => it.CreatedAt) .Select(it => new { PublicKeyId = it.PublicKey.Id, Id = it.Id }) .FirstOrDefault(); if (card == null) { throw new CardNotFoundException("Card not found"); } var grabResponse = await services.PrivateKeys.Get(card.Id, token) .ConfigureAwait(false); if (!VirgilKeyPair.CheckPrivateKeyPassword(grabResponse.PrivateKey, privateKeyPassword.GetBytes())) { throw new WrongPrivateKeyPasswordException("Wrong password"); } var privateKey = new PrivateKey(grabResponse.PrivateKey); var cards = await services.Cards.GetCardsRealtedToThePublicKey(card.PublicKeyId, card.Id, privateKey, privateKeyPassword) .ConfigureAwait(false); return (cards.Select(it => new PersonalCard(it, privateKey)) .OrderByDescending(it => it.CreatedAt) .FirstOrDefault()); }
public async Task Confirm(string code) { var token = await this.request.Confirm(code); this.state = States.Confirmed; try { this.privateKeyResponse = await this.DownloadPrivateKey(token); } catch (VirgilPrivateServicesException e) when(e.ErrorCode == 40020) { throw new PrivateKeyNotFoundException(); } this.state = States.PrivateKeyDownloaded; if (VirgilKeyPair.IsPrivateKeyEncrypted(this.privateKeyResponse.PrivateKey) && !VirgilKeyPair.CheckPrivateKeyPassword( this.privateKeyResponse.PrivateKey, Encoding.UTF8.GetBytes(this.password)) ) { throw new WrongPrivateKeyPasswordException("Wrong password"); } var card = new PersonalCard(this.recipientCard, new PrivateKey(this.privateKeyResponse.PrivateKey)); this.aggregator.Publish(new CardLoaded(card, this.password)); this.state = States.Finished; }
public void DecryptWithAnotherPassword(string anotherPassword) { if (VirgilKeyPair.IsPrivateKeyEncrypted(this.privateKeyResponse.PrivateKey) && !VirgilKeyPair.CheckPrivateKeyPassword( this.privateKeyResponse.PrivateKey, Encoding.UTF8.GetBytes(anotherPassword)) ) { throw new WrongPrivateKeyPasswordException("Wrong password"); } var card = new PersonalCard(this.recipientCard, new PrivateKey(this.privateKeyResponse.PrivateKey)); this.aggregator.Publish(new CardLoaded(card, anotherPassword)); }
public bool IsPasswordValid(string anotherPassword) { return(VirgilKeyPair.CheckPrivateKeyPassword(this.privateKeyResponse.PrivateKey, Encoding.UTF8.GetBytes(anotherPassword))); }
public bool CheckPrivateKeyPassword(string password) { return(VirgilKeyPair.CheckPrivateKeyPassword(this.PrivateKey.Data, password.GetBytes())); }