/// <summary> /// Imports the <see cref="VirgilKey"/> from buffer. /// </summary> /// <param name="keyBuffer">The buffer with Key.</param> /// <param name="keyPassword">The Key password.</param> /// <returns>An instance of <see cref="VirgilKey"/> class.</returns> public VirgilKey Import(VirgilBuffer keyBuffer, string keyPassword = null) { var privateKey = this.context.Crypto.ImportPrivateKey(keyBuffer.GetBytes(), keyPassword); var virgilKey = new VirgilKey(this.context, privateKey); return(virgilKey); }
/// <summary> /// Decrypts and verifies the data. /// </summary> /// <param name="cipherbuffer">The data to be decrypted.</param> /// <param name="card">The signer's <see cref="VirgilCard"/>.</param> /// <returns>The decrypted data, which is the original plain text before encryption.</returns> /// <exception cref="ArgumentNullException"></exception> public VirgilBuffer DecryptThenVerify(VirgilBuffer cipherbuffer, VirgilCard card) { var plaitext = this.context.Crypto .DecryptThenVerify(cipherbuffer.GetBytes(), this.privateKey, card.PublicKey); return(new VirgilBuffer(plaitext)); }
/// <summary> /// Imports a <see cref="VirgilCard"/> from specified buffer. /// </summary> /// <param name="exportedCard">A Card in string representation.</param> /// <returns>An instance of <see cref="VirgilCard"/>.</returns> public VirgilCard Import(string exportedCard) { var bufferCard = VirgilBuffer.From(exportedCard, StringEncoding.Base64); var importedCardModel = JsonSerializer.Deserialize <CardModel>(bufferCard.ToString()); return(new VirgilCard(this.context, importedCardModel)); }
/// <summary> /// Decrypts and verifies the data. /// </summary> /// <param name="cipherbuffer">The data to be decrypted.</param> /// <param name="card">A list of trusted cards, which can contains the signer's <see cref="VirgilCard"/>.</param> /// <returns>The decrypted data, which is the original plain text before encryption.</returns> /// <exception cref="ArgumentNullException"></exception> /// <example> /// <code> /// var virgil = new VirgilApi("[YOUR_ACCESS_TOKEN_HERE]"); /// // load a Virgil Key from device storage /// var bobKey = virgil.Keys.Load("[KEY_NAME]", "[OPTIONAL_KEY_PASSWORD]") /// /// // get a sender's Virgil Card /// var aliceCard = await virgil.Cards.Get("[ALICE_CARD_ID]") /// /// // decrypt the message /// var originalMessage = bobKey.DecryptThenVerify(ciphertext, aliceCard).ToString(); /// </code> /// </example> public VirgilBuffer DecryptThenVerify(VirgilBuffer cipherbuffer, params VirgilCard[] cards) { var plaitext = this.context.Crypto .DecryptThenVerify(cipherbuffer.GetBytes(), this.privateKey, cards.Select(it => it.PublicKey).ToArray()); return(new VirgilBuffer(plaitext)); }
/// <summary> /// Encrypts the specified text for <paramref name="recipient"/> Card. /// </summary> /// <param name="recipient">The list of <see cref="VirgilCard"/> recipients.</param> /// <param name="plaintext">The plaintext to be encrypted.</param> /// <returns>A new <see cref="VirgilBuffer"/> with encrypted data.</returns> /// <exception cref="ArgumentNullException"></exception> public static VirgilBuffer Encrypt(this VirgilCard recipient, string plaintext) { if (string.IsNullOrWhiteSpace(plaintext)) { throw new ArgumentException(Localization.ExceptionArgumentIsNullOrWhitespace, nameof(plaintext)); } return(recipient.Encrypt(VirgilBuffer.From(plaintext))); }
/// <summary> /// Encrypts the specified data for current <see cref="VirgilCard"/> recipient. /// </summary> /// <param name="buffer">The data to be encrypted.</param> /// <example> /// <code> /// // search for Virgil Cards /// var aliceCards = await virgil.Cards.FindAsync("alice"); /// /// var fileBuf = VirgilBuffer.FromFile("FILE_NAME_HERE"); /// /// // encrypt the buffer using found Virgil Cards /// var cipherFileBuf = aliceCards.Encrypt(fileBuf); /// </code> /// </example> public VirgilBuffer Encrypt(VirgilBuffer buffer) { if (buffer == null) { throw new ArgumentNullException(nameof(buffer)); } var cipherdata = this.context.Crypto.Encrypt(buffer.GetBytes(), this.PublicKey); return(new VirgilBuffer(cipherdata)); }
/// <summary> /// Decrypts the specified cipher data using <see cref="VirgilKey"/>. /// </summary> /// <param name="cipherBuffer">The encrypted data.</param> /// <returns>A byte array containing the result from performing the operation.</returns> /// <exception cref="ArgumentNullException"></exception> public VirgilBuffer Decrypt(VirgilBuffer cipherBuffer) { if (cipherBuffer == null) { throw new ArgumentNullException(nameof(cipherBuffer)); } var data = this.context.Crypto.Decrypt(cipherBuffer.GetBytes(), this.privateKey); return(new VirgilBuffer(data)); }
/// <summary> /// Generates a digital signature for specified data using current <see cref="VirgilKey"/>. /// </summary> /// <param name="data">The data for which the digital signature will be generated.</param> /// <returns>A new buffer that containing the result from performing the operation.</returns> /// <exception cref="ArgumentNullException"></exception> public VirgilBuffer Sign(VirgilBuffer data) { if (data == null) { throw new ArgumentNullException(nameof(data)); } var signature = this.context.Crypto.Sign(data.GetBytes(), this.privateKey); return(new VirgilBuffer(signature)); }
/// <summary> /// Encrypts the specified buffer data for list of <paramref name="recipients"/> Cards. /// </summary> /// <param name="recipients">The list of <see cref="VirgilCard"/> recipients.</param> /// <param name="buffer">The buffer data to be encrypted.</param> /// <returns>A new <see cref="VirgilBuffer"/> with encrypted data.</returns> /// <exception cref="ArgumentNullException"></exception> public static VirgilBuffer Encrypt(this IEnumerable <VirgilCard> recipients, VirgilBuffer buffer) { if (recipients == null) { throw new ArgumentNullException(nameof(recipients)); } var virgilCards = recipients.ToList(); return(virgilCards.First().Encrypt(buffer, virgilCards)); }
/// <summary> /// Encrypts and signs the data. /// </summary> /// <param name="buffer">The data to be encrypted.</param> /// <param name="recipients">The list of <see cref="VirgilCard"/> recipients.</param> /// <returns>The encrypted data</returns> /// <exception cref="ArgumentNullException"></exception> public VirgilBuffer SignThenEncrypt(VirgilBuffer buffer, IEnumerable <VirgilCard> recipients) { if (recipients == null) { throw new ArgumentNullException(nameof(recipients)); } var publicKeys = recipients.Select(pk => pk.PublicKey).ToArray(); var cipherdata = this.context.Crypto.SignThenEncrypt(buffer.GetBytes(), this.privateKey, publicKeys); return(new VirgilBuffer(cipherdata)); }
/// <summary> /// Encrypts a buffer data for list of <paramref name="recipients"/> Cards. /// </summary> /// <returns>A new <see cref="VirgilBuffer"/> with encrypted data.</returns> internal VirgilBuffer Encrypt(VirgilBuffer buffer, IEnumerable <VirgilCard> recipients) { var publicKeyRecipients = new List <IPublicKey>(); var virgilCards = recipients?.ToList(); if (virgilCards != null) { publicKeyRecipients.AddRange(virgilCards.Select(r => r.PublicKey)); } var cipherdata = this.context.Crypto.Encrypt(buffer.GetBytes(), publicKeyRecipients.ToArray()); return(new VirgilBuffer(cipherdata)); }
/// <summary> /// Verifies the specified buffer and signature with current <see cref="VirgilCard"/> recipient. /// </summary> /// <param name="buffer">The data to be verified.</param> /// <param name="signature">The signature used to verify the data integrity.</param> public bool Verify(VirgilBuffer buffer, VirgilBuffer signature) { if (buffer == null) { throw new ArgumentNullException(nameof(buffer)); } if (signature == null) { throw new ArgumentNullException(nameof(signature)); } var isValid = this.context.Crypto.Verify( buffer.GetBytes(), signature.GetBytes(), this.PublicKey); return(isValid); }
private CardModel BuildCardModel ( string identity, string identityType, Dictionary <string, string> customFields, CardScope scope, VirgilKey ownerKey ) { var cardSnapshotModel = new PublishCardSnapshotModel { Identity = identity, IdentityType = identityType, Info = new CardInfoModel { DeviceName = this.context.DeviceManager.GetDeviceName(), Device = this.context.DeviceManager.GetSystemName() }, PublicKeyData = ownerKey.ExportPublicKey().GetBytes(), Scope = scope, Data = customFields }; var snapshot = new Snapshotter().Capture(cardSnapshotModel); var snapshotFingerprint = this.context.Crypto.CalculateFingerprint(snapshot); var cardId = snapshotFingerprint.ToHEX(); var selfSignature = ownerKey.Sign(VirgilBuffer.From(snapshotFingerprint.GetValue())); var signatures = new Dictionary <string, byte[]> { [cardId] = selfSignature.GetBytes() }; var cardModel = new CardModel(cardSnapshotModel) { Id = cardId, Snapshot = snapshot, Meta = new CardMetaModel { Signatures = signatures } }; return(cardModel); }
/// <summary> /// Encrypts and signs the specified data for current enumeration of <see cref="VirgilCard"/> recipients. /// </summary> /// <param name="task">The list of <see cref="VirgilCard"/> recipients.</param> /// <param name="data">The data to be encrypted.</param> /// <param name="key">The signer's <see cref="VirgilKey"/></param> /// <returns>A new <see cref="VirgilBuffer"/> with encrypted data.</returns> /// <exception cref="ArgumentNullException"></exception> public static Task <VirgilBuffer> SignThenEncrypt(this Task <IEnumerable <VirgilCard> > task, byte[] data, VirgilKey key) { return(SignThenEncrypt(task, VirgilBuffer.From(data), key)); }
/// <summary> /// Exports the Public key value from current <see cref="VirgilKey"/>. /// </summary> /// <returns>A new <see cref="VirgilBuffer"/> that contains Public Key value.</returns> public VirgilBuffer ExportPublicKey() { var publicKey = this.context.Crypto.ExtractPublicKey(this.privateKey); return(VirgilBuffer.From(this.context.Crypto.ExportPublicKey(publicKey))); }
/// <summary> /// Encrypts and signs the specified buffer with data for current enumeration of <see cref="VirgilCard"/> recipients. /// </summary> /// <param name="task">The list of <see cref="VirgilCard"/> recipients.</param> /// <param name="buffer">The buffer data to be encrypted.</param> /// <param name="key">The signer's <see cref="VirgilKey"/></param> /// <returns>A new <see cref="VirgilBuffer"/> with encrypted data.</returns> /// <exception cref="ArgumentNullException"></exception> public static Task <VirgilBuffer> SignThenEncrypt(this Task <IEnumerable <VirgilCard> > task, VirgilBuffer buffer, VirgilKey key) { return(task.ContinueWith(t => { if (t.Exception == null && !t.Result.Any()) { throw new RecipientsNotFoundException(); } return key.SignThenEncrypt(buffer, t.Result); })); }
/// <summary> /// Encrypts and signs the specified plaintext for current enumeration of <see cref="VirgilCard"/> recipients. /// </summary> /// <param name="task">The list of <see cref="VirgilCard"/> recipients.</param> /// <param name="plaintext">The plaintext to be encrypted.</param> /// <param name="key">The signer's <see cref="VirgilKey"/></param> /// <returns>A new <see cref="VirgilBuffer"/> with encrypted data.</returns> /// <exception cref="ArgumentNullException"></exception> public static Task <VirgilBuffer> SignThenEncrypt(this Task <IEnumerable <VirgilCard> > task, string plaintext, VirgilKey key) { return(SignThenEncrypt(task, VirgilBuffer.From(plaintext), key)); }
/// <summary> /// Verifies that a digital signature is valid for specified text. /// </summary> /// <param name="recipient">The <see cref="VirgilCard"/> recipient.</param> /// <param name="data">The data to be signed.</param> /// <param name="signature">The signature.</param> /// <returns><c>true</c> if the signature is valid; otherwise, <c>false</c>.</returns> /// <exception cref="ArgumentException"></exception> public static bool Verify(this VirgilCard recipient, byte[] data, VirgilBuffer signature) { return(Verify(recipient, VirgilBuffer.From(data), signature)); }
/// <summary> /// Verifies that a digital signature is valid for specified text. /// </summary> /// <param name="recipient">The <see cref="VirgilCard"/> recipient.</param> /// <param name="buffer">The text.</param> /// <param name="signature">The signature.</param> /// <returns><c>true</c> if the signature is valid; otherwise, <c>false</c>.</returns> /// <exception cref="ArgumentException"></exception> public static bool Verify(this VirgilCard recipient, VirgilBuffer buffer, VirgilBuffer signature) { return(recipient.Verify(buffer, signature)); }
/// <summary> /// Verifies that a digital signature is valid for specified text. /// </summary> /// <param name="task">The <see cref="VirgilCard"/> recipient.</param> /// <param name="data">The data to be signed.</param> /// <param name="signature">The signature.</param> /// <returns><c>true</c> if the signature is valid; otherwise, <c>false</c>.</returns> /// <exception cref="ArgumentException"></exception> public static Task <bool> Verify(this Task <VirgilCard> task, byte[] data, VirgilBuffer signature) { return(Verify(task, VirgilBuffer.From(data), signature)); }
/// <summary> /// Encrypts the plaintext for current <see cref="VirgilCard"/> recipient. /// </summary> /// <param name="task">The <see cref="VirgilCard"/> recipient</param> /// <param name="plaintext">The plaintext to be encrypted.</param> /// <returns>A new <see cref="VirgilBuffer"/> with encrypted data.</returns> /// <exception cref="ArgumentNullException"></exception> public static Task <VirgilBuffer> Encrypt(this Task <VirgilCard> task, string plaintext) { return(Encrypt(task, VirgilBuffer.From(plaintext))); }
/// <summary> /// Verifies that a digital signature is valid for specified text. /// </summary> /// <param name="task">The <see cref="VirgilCard"/> recipient.</param> /// <param name="buffer">The text.</param> /// <param name="signature">The signature.</param> /// <returns><c>true</c> if the signature is valid; otherwise, <c>false</c>.</returns> /// <exception cref="ArgumentException"></exception> public static Task <bool> Verify(this Task <VirgilCard> task, VirgilBuffer buffer, VirgilBuffer signature) { return(task.ContinueWith(t => t.Result.Verify(buffer, signature))); }
/// <summary> /// Verifies that a digital signature is valid for specified text. /// </summary> /// <param name="task">The <see cref="VirgilCard"/> recipient.</param> /// <param name="text">The text.</param> /// <param name="signature">The signature.</param> /// <returns><c>true</c> if the signature is valid; otherwise, <c>false</c>.</returns> /// <exception cref="ArgumentException"></exception> public static Task <bool> Verify(this Task <VirgilCard> task, string text, VirgilBuffer signature) { return(Verify(task, VirgilBuffer.From(text), signature)); }
/// <summary> /// Exports a current <see cref="VirgilCard"/> instance into base64 encoded string. /// </summary> /// <returns>A string that represents a <see cref="VirgilCard"/>.</returns> /// <example> /// <code> /// // export a Virgil Card to string /// var exportedAliceCard = aliceCard.Export(); /// </code> /// </example> /// How to get aliceCard <see cref="ICardsManager.Create(string, VirgilKey, string, Dictionary{string, string})"/> public string Export() { var serializedCard = JsonSerializer.Serialize(this.card); return(VirgilBuffer.From(serializedCard).ToString(StringEncoding.Base64)); }
/// <summary> /// Encrypts the specified data for current <see cref="VirgilCard" /> recipient. /// </summary> /// <param name="task">The <see cref="VirgilCard" /> recipient</param> /// <param name="buffer">The buffer with data to be encrypted.</param> /// <param name="key">The signer's key.</param> /// <returns> /// A new <see cref="VirgilBuffer" /> with encrypted data. /// </returns> /// <exception cref="ArgumentNullException"></exception> public static Task <VirgilBuffer> SignThenEncrypt(this Task <VirgilCard> task, VirgilBuffer buffer, VirgilKey key) { return(task.ContinueWith(t => key.SignThenEncrypt(buffer, new[] { t.Result }))); }
/// <summary> /// Signs a byte array data using current <see cref="VirgilKey"/> and then encrypt it /// using recipient's <see cref="VirgilCard"/>. /// </summary> /// <param name="virgilKey">The <see cref="VirgilKey"/> used to sign the <paramref name="data"/>.</param> /// <param name="data">The plaintext to be encrypted.</param> /// <param name="recipient">The recipient's <see cref="VirgilCard"/> used to /// encrypt the <paramref name="data"/>.</param> /// <returns>A new <see cref="VirgilBuffer"/> instance with encrypted data.</returns> public static VirgilBuffer SignThenEncrypt(this VirgilKey virgilKey, byte[] data, VirgilCard recipient) { return(virgilKey.SignThenEncrypt(VirgilBuffer.From(data), new[] { recipient })); }
/// <summary> /// Signs the plaintext using current <see cref="VirgilKey"/> and then encrypt it /// using recipient's <see cref="VirgilCard"/>. /// </summary> /// <param name="virgilKey">The <see cref="VirgilKey"/> used to sign the <paramref name="plaintext"/>.</param> /// <param name="plaintext">The plaintext to be encrypted.</param> /// <param name="recipient">The recipient's <see cref="VirgilCard"/> used to /// encrypt the <paramref name="plaintext"/>.</param> /// <returns>A new <see cref="VirgilBuffer"/> instance with encrypted data.</returns> public static VirgilBuffer SignThenEncrypt(this VirgilKey virgilKey, string plaintext, VirgilCard recipient) { return(virgilKey.SignThenEncrypt(VirgilBuffer.From(plaintext), new [] { recipient })); }
/// <summary> /// Verifies that a digital signature is valid for specified text. /// </summary> /// <param name="recipient">The <see cref="VirgilCard"/> recipient.</param> /// <param name="text">The text.</param> /// <param name="signature">The signature.</param> /// <returns><c>true</c> if the signature is valid; otherwise, <c>false</c>.</returns> /// <exception cref="ArgumentException"></exception> public static bool Verify(this VirgilCard recipient, string text, VirgilBuffer signature) { return(Verify(recipient, VirgilBuffer.From(text), signature)); }
/// <summary> /// Encrypts the specified data for current <see cref="VirgilCard"/> recipient. /// </summary> /// <param name="task">The <see cref="VirgilCard"/> recipient</param> /// <param name="data">The data to be encrypted.</param> /// <returns>A new <see cref="VirgilBuffer"/> with encrypted data.</returns> /// <exception cref="ArgumentNullException"></exception> public static Task <VirgilBuffer> Encrypt(this Task <VirgilCard> task, byte[] data) { return(Encrypt(task, VirgilBuffer.From(data))); }
/// <summary> /// Encrypts the specified buffer with data for current <see cref="VirgilCard"/> recipient. /// </summary> /// <param name="task">The <see cref="VirgilCard"/> recipient</param> /// <param name="buffer">The buffer with data to be encrypted.</param> /// <returns>A new <see cref="VirgilBuffer"/> with encrypted data.</returns> /// <exception cref="ArgumentNullException"></exception> public static Task <VirgilBuffer> Encrypt(this Task <VirgilCard> task, VirgilBuffer buffer) { return(task.ContinueWith(t => t.Result.Encrypt(buffer))); }