30
30
MongoCryptCallback = object
31
31
32
32
from bson import _bson_to_dict , _dict_to_bson , decode , encode
33
- from bson .binary import STANDARD
33
+ from bson .binary import STANDARD , Binary
34
34
from bson .codec_options import CodecOptions
35
35
from bson .raw_bson import (DEFAULT_RAW_BSON_OPTIONS ,
36
36
RawBSONDocument ,
@@ -401,7 +401,7 @@ def encrypt(self, value, algorithm, key_id=None, key_alt_name=None):
401
401
The encrypted value, a :class:`~bson.binary.Binary` with subtype 6.
402
402
"""
403
403
# TODO: Add a required codec_options argument for encoding?
404
- doc = encode ({'v' : value })
404
+ doc = encode ({'v' : value }, codec_options = _DATA_KEY_OPTS )
405
405
if isinstance (key_id , uuid .UUID ):
406
406
raw_key_id = key_id .bytes
407
407
else :
@@ -420,10 +420,13 @@ def decrypt(self, value):
420
420
:Returns:
421
421
The decrypted BSON value.
422
422
"""
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' )
423
426
doc = encode ({'v' : value })
424
427
decrypted_doc = self ._encryption .decrypt (doc )
425
428
# 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' ]
427
430
428
431
def close (self ):
429
432
"""Release resources."""
0 commit comments