コード例 #1
0
        /// <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));
        }
コード例 #2
0
        /// <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)));
        }
コード例 #3
0
        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);
        }
コード例 #4
0
 /// <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));
 }
コード例 #5
0
 /// <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));
 }
コード例 #6
0
 /// <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));
 }
コード例 #7
0
 /// <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));
 }
コード例 #8
0
 /// <summary>
 /// Signs a plaintext using current <see cref="VirgilKey"/>.
 /// </summary>
 /// <param name="virgilKey">The <see cref="VirgilKey"/> used to sign the <paramref name="plaintext"/></param>
 /// <param name="plaintext">The plaintext to be signed.</param>
 /// <returns>A new <see cref="VirgilBuffer"/> instance with generated signature.</returns>
 public static VirgilBuffer Sign(this VirgilKey virgilKey, string plaintext)
 {
     return(virgilKey.Sign(VirgilBuffer.From(plaintext)));
 }
コード例 #9
0
        /// <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));
        }
コード例 #10
0
ファイル: VirgilKey.cs プロジェクト: nau11713/virgil-sdk-net
        /// <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)));
        }
コード例 #11
0
 /// <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 }));
 }
コード例 #12
0
 /// <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 }));
 }
コード例 #13
0
 /// <summary>
 /// Signs a plaintext using current <see cref="VirgilKey"/> and then encrypt it
 /// using multiple recipient's <see cref="VirgilCard"/>s.
 /// </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="recipients">A list of recipient's <see cref="VirgilCard"/>s 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, IEnumerable <VirgilCard> recipients)
 {
     return(virgilKey.SignThenEncrypt(VirgilBuffer.From(plaintext), recipients));
 }
コード例 #14
0
 /// <summary>
 /// Signs a byte array data using current <see cref="VirgilKey"/> and then encrypt it
 /// using multiple recipient's <see cref="VirgilCard"/>s.
 /// </summary>
 /// <param name="virgilKey">The <see cref="VirgilKey"/> used to sign the <paramref name="data"/>.</param>
 /// <param name="data">The data to be encrypted.</param>
 /// <param name="recipients">A list of recipient's <see cref="VirgilCard"/>s 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, IEnumerable <VirgilCard> recipients)
 {
     return(virgilKey.SignThenEncrypt(VirgilBuffer.From(data), recipients));
 }
コード例 #15
0
 /// <summary>
 /// Signs a byte array data using current <see cref="VirgilKey"/>.
 /// </summary>
 /// <param name="virgilKey">The <see cref="VirgilKey"/> used to sign the <paramref name="data"/></param>
 /// <param name="data">The plaintext to be signed.</param>
 /// <returns>A new <see cref="VirgilBuffer"/> instance with generated signature.</returns>
 public static VirgilBuffer Sign(this VirgilKey virgilKey, byte[] data)
 {
     return(virgilKey.Sign(VirgilBuffer.From(data)));
 }
コード例 #16
0
 /// <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));
 }
コード例 #17
0
 /// <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));
 }
コード例 #18
0
 /// <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)));
 }
コード例 #19
0
 /// <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)));
 }
コード例 #20
0
 /// <summary>
 /// Decrypts a ciphertext using current <see cref="VirgilKey"/> and verifies one
 /// using specified <see cref="VirgilCard"/>.
 /// </summary>
 /// <param name="virgilKey">The <see cref="VirgilKey"/>, that represents a Private key.</param>
 /// <param name="ciphertext">The ciphertext in base64 encoded string.</param>
 /// <param name="signerCard">The signer's <see cref="VirgilCard"/>, that represents a
 /// Public key and user/device information.</param>
 /// <returns>A new <see cref="VirgilBuffer"/> instance with decrypted data.</returns>
 public static VirgilBuffer DecryptThenVerify(this VirgilKey virgilKey, string ciphertext, VirgilCard signerCard)
 {
     return(virgilKey.DecryptThenVerify(VirgilBuffer.From(ciphertext, StringEncoding.Base64), signerCard));
 }