コード例 #1
0
        /// <summary>
        /// Starts an explicit encryption context.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <param name="encryptionAlgorithm">The encryption algorithm.</param>
        /// <param name="command">The BSON command.</param>
        /// <returns>A encryption context. </returns>
        public CryptContext StartExplicitEncryptionContext(Guid key, EncryptionAlgorithm encryptionAlgorithm, byte[] command)
        {
            ContextSafeHandle handle = Library.mongocrypt_ctx_new(_handle);
            
            byte[] keyBuffer = key.ToByteArray();
            unsafe
            {
                fixed (byte* p = keyBuffer)
                {
                    IntPtr ptr = (IntPtr)p;
                    using (PinnedBinary pinned = new PinnedBinary(ptr, (uint)keyBuffer.Length))
                    {
                        handle.Check(_status, Library.mongocrypt_ctx_setopt_key_id(handle, pinned.Handle));
                    }
                }
            }

            handle.Check(_status, Library.mongocrypt_ctx_setopt_algorithm(handle, Helpers.EncryptionAlgorithmToString(encryptionAlgorithm), -1));

            unsafe
            {
                fixed (byte* p = command)
                {
                    IntPtr ptr = (IntPtr)p;
                    using (PinnedBinary pinned = new PinnedBinary(ptr, (uint)command.Length))
                    {
                        handle.Check(_status, Library.mongocrypt_ctx_explicit_encrypt_init(handle, pinned.Handle));
                    }
                }
            }

            return new CryptContext(handle);
        }