3030 MongoCryptCallback = object
3131
3232from bson import _bson_to_dict , _dict_to_bson , decode , encode
33- from bson .binary import STANDARD
33+ from bson .binary import STANDARD , Binary
3434from bson .codec_options import CodecOptions
3535from bson .raw_bson import (DEFAULT_RAW_BSON_OPTIONS ,
3636 RawBSONDocument ,
@@ -401,7 +401,7 @@ def encrypt(self, value, algorithm, key_id=None, key_alt_name=None):
401401 The encrypted value, a :class:`~bson.binary.Binary` with subtype 6.
402402 """
403403 # TODO: Add a required codec_options argument for encoding?
404- doc = encode ({'v' : value })
404+ doc = encode ({'v' : value }, codec_options = _DATA_KEY_OPTS )
405405 if isinstance (key_id , uuid .UUID ):
406406 raw_key_id = key_id .bytes
407407 else :
@@ -420,10 +420,13 @@ def decrypt(self, value):
420420 :Returns:
421421 The decrypted BSON value.
422422 """
423+ if not (isinstance (value , Binary ) and value .subtype == 6 ):
424+ raise TypeError (
425+ 'value to decrypt must be a bson.binary.Binary with subtype 6' )
423426 doc = encode ({'v' : value })
424427 decrypted_doc = self ._encryption .decrypt (doc )
425428 # TODO: Add a required codec_options argument for decoding?
426- return decode (decrypted_doc )['v' ]
429+ return decode (decrypted_doc , codec_options = _DATA_KEY_OPTS )['v' ]
427430
428431 def close (self ):
429432 """Release resources."""
0 commit comments