-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add support for update_into on CipherContext #3190
Merged
Merged
Changes from 21 commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
b5e23b2
add support for update_into on CipherContext
reaperhulk 93c8fad
another skip_if
reaperhulk b6026d6
more skip_if complexity
reaperhulk 4efd7c3
maybe do this right
reaperhulk 143b188
correct number of args
reaperhulk 09b7326
coverage for the coverage gods
reaperhulk 1f2ac83
add a cffi minimum test tox target and travis builder
reaperhulk 41c9910
extra arg
reaperhulk bca7f1f
need to actually install py35
reaperhulk bb8b152
fix
reaperhulk 35a5fa1
coverage for GCM decrypt in CC
reaperhulk 964aa62
no longer relevant
reaperhulk 9db7d9b
1.8 now
reaperhulk cd07d52
pep8
reaperhulk 0e7e747
dramatically simplify
reaperhulk eb2ed24
update docs
reaperhulk 21e12ae
remove unneeded test
reaperhulk 915cffc
changelog entry
reaperhulk 6ccdba3
test improvements
reaperhulk 827b4e0
coverage fix
reaperhulk e78e98a
add some comments to example
reaperhulk 2e57ba2
move the comments to their own line
reaperhulk 1bb3b34
fix and move comment
reaperhulk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -456,6 +456,46 @@ Interfaces | |
return bytes immediately, however in other modes it will return chunks | ||
whose size is determined by the cipher's block size. | ||
|
||
.. method:: update_into(data, buf) | ||
|
||
.. versionadded:: 1.8 | ||
|
||
.. warning:: | ||
|
||
This method allows you to avoid a memory copy by passing a writable | ||
buffer and reading the resulting data. You are responsible for | ||
correctly sizing the buffer and properly handling the data. This | ||
method should only be used when extremely high performance is a | ||
requirement and you will be making many small calls to | ||
``update_into``. | ||
|
||
:param bytes data: The data you wish to pass into the context. | ||
:param buf: A writable Python buffer that the data will be written | ||
into. This buffer should be ``len(data) + n - 1`` bytes where ``n`` | ||
is the block size (in bytes) of the cipher and mode combination. | ||
:return int: Number of bytes written. | ||
:raises NotImplementedError: This is raised if the version of ``cffi`` | ||
used is too old (this can happen on older PyPy releases). | ||
:raises ValueError: This is raised if the supplied buffer is too small. | ||
|
||
.. doctest:: | ||
|
||
>>> import os | ||
>>> from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes | ||
>>> from cryptography.hazmat.backends import default_backend | ||
>>> backend = default_backend() | ||
>>> key = os.urandom(32) | ||
>>> iv = os.urandom(16) | ||
>>> cipher = Cipher(algorithms.AES(key), modes.CBC(iv), backend=backend) | ||
>>> encryptor = cipher.encryptor() | ||
>>> buf = bytearray(31) # size the buffer to b len(data) + n - 1 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I still don't understand this comment :-) |
||
>>> len_encrypted = encryptor.update_into(b"a secret message", buf) | ||
>>> ct = bytes(buf[:len_encrypted]) + encryptor.finalize() # get the ciphertext from the buffer reading only the bytes written to it (len_encrypted) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe put the comment on the line above the code, so it doesn't wrap so long? |
||
>>> decryptor = cipher.decryptor() | ||
>>> len_decrypted = decryptor.update_into(ct, buf) | ||
>>> bytes(buf[:len_decrypted]) + decryptor.finalize() # get the plaintext from the buffer reading only the bytes written (len_decrypted) | ||
'a secret message' | ||
|
||
.. method:: finalize() | ||
|
||
:return bytes: Returns the remainder of the data. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
b len(data)
?