コード例 #1
0
        /// <summary>Creates a CryptClient with the specified options.</summary>
        /// <param name="options">The options.</param>
        /// <returns>A CryptClient</returns>
        public static CryptClient Create(CryptOptions options)
        {
            var handle = Library.mongocrypt_new();

            var status = new Status();

            // The below code can be avoided on Windows. So, we don't call it on this system
            // to avoid restrictions on target frameworks that present in some of below
            if (OperatingSystemHelper.CurrentOperatingSystem != OperatingSystemPlatform.Windows)
            {
                handle.Check(
                    status,
                    Library.mongocrypt_setopt_crypto_hooks(
                        handle,
                        __crypto256EncryptCallback,
                        __crypto256DecryptCallback,
                        __randomCallback,
                        __cryptoHmacSha512Callback,
                        __cryptoHmacSha256Callback,
                        __cryptoHashCallback,
                        IntPtr.Zero));

                handle.Check(
                    status,
                    Library.mongocrypt_setopt_crypto_hook_sign_rsaes_pkcs1_v1_5(
                        handle,
                        __signRsaesPkcs1HmacCallback,
                        IntPtr.Zero));
            }

            foreach (var kmsCredentials in options.KmsCredentials)
            {
                kmsCredentials.SetCredentials(handle, status);
            }

            if (options.Schema != null)
            {
                unsafe
                {
                    fixed(byte *schema = options.Schema)
                    {
                        var schemaPtr = (IntPtr)schema;

                        using (var pinnedSchema = new PinnedBinary(schemaPtr, (uint)options.Schema.Length))
                        {
                            handle.Check(status, Library.mongocrypt_setopt_schema_map(handle, schema: pinnedSchema.Handle));
                        }
                    }
                }
            }
            Library.mongocrypt_init(handle);

            return(new CryptClient(handle, status));
        }