Encrypt a value

- « MongoDB\Driver\ClientEncryption::deleteKey

- MongoDB\Driver\ClientEncryption::encryptExpression »

- PHP Manual
- MongoDB\Driver\ClientEncryption
- Encrypt a value

# MongoDB\Driver\ClientEncryption::encrypt

(mongodb >=1.7.0)

MongoDB\Driver\ClientEncryption::encrypt — Encrypt a value

### Description

final public MongoDB\Driver\ClientEncryption::encrypt(mixed `$value`, ?array `$options` = `null`): MongoDB\BSON\Binary

Encrypts the value.

### Parameters

`value`

  The value to be encrypted. Any value that can be inserted into MongoDB can be encrypted using this method.

`options`

  Encryption options
  Option | Type | Description
  --- | --- | ---

  algorithm

  string

  The encryption algorithm to be used. This option is required. Specify one of the following ClientEncryption constants:
  - `MongoDB\Driver\ClientEncryption::AEAD_AES_256_CBC_HMAC_SHA_512_DETERMINISTIC`
  - `MongoDB\Driver\ClientEncryption::AEAD_AES_256_CBC_HMAC_SHA_512_RANDOM`
  - `MongoDB\Driver\ClientEncryption::ALGORITHM_INDEXED`
  - `MongoDB\Driver\ClientEncryption::ALGORITHM_UNINDEXED`
  - `MongoDB\Driver\ClientEncryption::ALGORITHM_RANGE_PREVIEW`

  contentionFactor | int | The contention factor for evaluating queries with indexed, encrypted payloads. This option only applies and may only be specified when `algorithm` is `MongoDB\Driver\ClientEncryption::ALGORITHM_INDEXED` or `MongoDB\Driver\ClientEncryption::ALGORITHM_RANGE_PREVIEW`.
  keyAltName | string | Identifies a key vault collection document by `keyAltName`. This option is mutually exclusive with `keyId` and exactly one is required.
  keyId | MongoDB\BSON\Binary | Identifies a data key by `_id`. The value is a UUID (binary subtype 4). This option is mutually exclusive with `keyAltName` and exactly one is required.

  queryType

  int

  The query type for evaluating queries with indexed, encrypted payloads. Specify one of the following ClientEncryption constants:
  - `MongoDB\Driver\ClientEncryption::QUERY_TYPE_EQUALITY`
  - `MongoDB\Driver\ClientEncryption::QUERY_TYPE_RANGE_PREVIEW`

  This option only applies and may only be specified when `algorithm` is `MongoDB\Driver\ClientEncryption::ALGORITHM_INDEXED` or `MongoDB\Driver\ClientEncryption::ALGORITHM_RANGE_PREVIEW`.

  rangeOpts

  array

  Index options for a queryable encryption field supporting "rangePreview" queries. The options below must match the values set in the `encryptedFields` of the target collection. For double and decimal128 BSON field types, `min`, `max`, and `precision` must all be set, or all be unset.

  Range index options
  Option | Type | Description
  --- | --- | ---
  min | mixed | Required if `precision` is set.
  max | mixed | Required if `precision` is set.
  sparsity | int | Required.
  precision | int | Optional. May only be set for double or decimal128 BSON field types.

### Return Values

Returns the encrypted value as MongoDB\BSON\Binary object with subtype 6.

### Errors/Exceptions

- Throws MongoDB\Driver\Exception\InvalidArgumentException on argument parsing errors.
- Throws MongoDB\Driver\Exception\EncryptionException if an error occurs while encrypting the value

### Changelog

Version | Description
--- | ---
PECL mongodb 1.14.0 | Added the `"contentionFactor"` and `"queryType"` options.

### See Also

- MongoDB\Driver\ClientEncryption::decrypt() - Decrypt a value