Alex Gaynor | 450bb4c | 2014-02-03 15:42:04 -0800 | [diff] [blame] | 1 | .. hazmat:: |
| 2 | |
| 3 | MultiBackend |
| 4 | ============ |
| 5 | |
| 6 | .. currentmodule:: cryptography.hazmat.backends.multibackend |
| 7 | |
| 8 | .. class:: MultiBackend(backends) |
| 9 | |
Alex Gaynor | db80954 | 2014-02-03 16:19:32 -0800 | [diff] [blame] | 10 | .. versionadded:: 0.2 |
| 11 | |
Alex Gaynor | 450bb4c | 2014-02-03 15:42:04 -0800 | [diff] [blame] | 12 | This class allows you to combine multiple backends into a single backend |
Alex Stapleton | 63b3de2 | 2014-02-08 09:43:16 +0000 | [diff] [blame] | 13 | that offers the combined features of all of its constituents. |
Alex Gaynor | 450bb4c | 2014-02-03 15:42:04 -0800 | [diff] [blame] | 14 | |
Alex Stapleton | faf305b | 2014-07-12 12:27:37 +0100 | [diff] [blame] | 15 | .. testsetup:: |
| 16 | |
| 17 | from cryptography import utils |
| 18 | from cryptography.exceptions import UnsupportedAlgorithm, _Reasons |
| 19 | from cryptography.hazmat.backends.interfaces import HashBackend |
| 20 | from cryptography.hazmat.backends.openssl.backend import backend as backend2 |
| 21 | |
| 22 | @utils.register_interface(HashBackend) |
| 23 | class DummyHashBackend(object): |
| 24 | def hash_supported(self, algorithm): |
| 25 | return False |
| 26 | |
| 27 | def create_hash_ctx(self, algorithm): |
| 28 | raise UnsupportedAlgorithm("", _Reasons.UNSUPPORTED_HASH) |
| 29 | |
| 30 | backend1 = DummyHashBackend() |
| 31 | |
| 32 | .. doctest:: |
Alex Gaynor | 5598850 | 2014-02-03 16:15:06 -0800 | [diff] [blame] | 33 | |
| 34 | >>> from cryptography.hazmat.backends.multibackend import MultiBackend |
| 35 | >>> from cryptography.hazmat.primitives import hashes |
| 36 | >>> backend1.hash_supported(hashes.SHA256()) |
| 37 | False |
Alex Gaynor | 1fb4524 | 2014-02-03 16:53:35 -0800 | [diff] [blame] | 38 | >>> backend2.hash_supported(hashes.SHA256()) |
Alex Gaynor | 5598850 | 2014-02-03 16:15:06 -0800 | [diff] [blame] | 39 | True |
| 40 | >>> multi_backend = MultiBackend([backend1, backend2]) |
Alex Gaynor | 1fb4524 | 2014-02-03 16:53:35 -0800 | [diff] [blame] | 41 | >>> multi_backend.hash_supported(hashes.SHA256()) |
Alex Gaynor | 5598850 | 2014-02-03 16:15:06 -0800 | [diff] [blame] | 42 | True |
| 43 | |
Alex Gaynor | 450bb4c | 2014-02-03 15:42:04 -0800 | [diff] [blame] | 44 | :param backends: A ``list`` of backend objects. Backends are checked for |
Alex Gaynor | 2b1752e | 2014-02-03 16:11:23 -0800 | [diff] [blame] | 45 | feature support in the order they appear in this list. |