|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# Copyright 2021, CS GROUP - France, https://www.csgroup.eu/ |
| 3 | +# |
| 4 | +# This file is part of EODAG project |
| 5 | +# https://www.github.com/CS-SI/EODAG |
| 6 | +# |
| 7 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | +# you may not use this file except in compliance with the License. |
| 9 | +# You may obtain a copy of the License at |
| 10 | +# |
| 11 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | +# |
| 13 | +# Unless required by applicable law or agreed to in writing, software |
| 14 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | +# See the License for the specific language governing permissions and |
| 17 | +# limitations under the License. |
| 18 | + |
| 19 | +from cryptography.fernet import Fernet |
| 20 | + |
| 21 | + |
| 22 | +class CryptoKey: |
| 23 | + """Encapsulate a key implementation.""" |
| 24 | + |
| 25 | + def __init__(self, strkey=None, byteskey=None): |
| 26 | + if strkey: |
| 27 | + self._value = strkey.encode() |
| 28 | + elif byteskey: |
| 29 | + self._value = byteskey |
| 30 | + else: |
| 31 | + self._value = Fernet.generate_key() |
| 32 | + |
| 33 | + @staticmethod |
| 34 | + def from_str(strkey): |
| 35 | + """Build a :class:`~eodag.utils.security.CryptoKey` object from its string representation |
| 36 | +
|
| 37 | + :param strkey: The key as str |
| 38 | + :type strkey: str |
| 39 | + :returns: The :class:`~eodag.utils.security.CryptoKey` built from `strkey` |
| 40 | + :rtype: :class:`~eodag.utils.security.CryptoKey` |
| 41 | + """ |
| 42 | + return CryptoKey(strkey=strkey) |
| 43 | + |
| 44 | + @staticmethod |
| 45 | + def from_bytes(byteskey): |
| 46 | + """Build a :class:`~eodag.utils.security.CryptoKey` object from its bytes representation |
| 47 | +
|
| 48 | + :param byteskey: The key as bytes |
| 49 | + :type byteskey: bytes |
| 50 | + :returns: The :class:`~eodag.utils.security.CryptoKey` built from `byteskey` |
| 51 | + :rtype: :class:`~eodag.utils.security.CryptoKey` |
| 52 | + """ |
| 53 | + return CryptoKey(byteskey=byteskey) |
| 54 | + |
| 55 | + @property |
| 56 | + def value(self): |
| 57 | + """Read-only property on the value of the key""" |
| 58 | + return self._value |
| 59 | + |
| 60 | + def as_str(self): |
| 61 | + """Return str representation of the key""" |
| 62 | + return self.value.decode() |
| 63 | + |
| 64 | + |
| 65 | +class Crypto: |
| 66 | + """Class used for symmetric encryption of text, using a key.""" |
| 67 | + |
| 68 | + def __init__(self, key=None): |
| 69 | + self._key = key if key is not None else CryptoKey() |
| 70 | + self._impl = Fernet(self._key.value) |
| 71 | + |
| 72 | + @staticmethod |
| 73 | + def from_key(key): |
| 74 | + """Build a :class:`~eodag.utils.security.Crypto` object with a :class:`~eodag.utils.security.CryptoKey` |
| 75 | +
|
| 76 | + :param key: The key as str |
| 77 | + :type key: :class:`~eodag.utils.security.CryptoKey` |
| 78 | + :returns: The :class:`~eodag.utils.security.Crypto` object built |
| 79 | + with a :class:`~eodag.utils.security.CryptoKey` |
| 80 | + :rtype: :class:`~eodag.utils.security.Crypto` |
| 81 | + """ |
| 82 | + return Crypto(key=key) |
| 83 | + |
| 84 | + @staticmethod |
| 85 | + def from_strkey(strkey): |
| 86 | + """Build a :class:`~eodag.utils.security.Crypto` object with a key str |
| 87 | +
|
| 88 | + :param strkey: The key as str |
| 89 | + :type strkey: str |
| 90 | + :returns: The :class:`~eodag.utils.security.Crypto` object built |
| 91 | + with a :class:`~eodag.utils.security.CryptoKey`, itself built from `strkey` |
| 92 | + :rtype: :class:`~eodag.utils.security.Crypto` |
| 93 | + """ |
| 94 | + key = CryptoKey.from_str(strkey=strkey) |
| 95 | + return Crypto.from_key(key=key) |
| 96 | + |
| 97 | + @property |
| 98 | + def key(self): |
| 99 | + """Read-only property on the key used by the :class:`~eodag.utils.security.Crypto` object""" |
| 100 | + return self._key |
| 101 | + |
| 102 | + def encrypt(self, text): |
| 103 | + """Encrypt a text |
| 104 | +
|
| 105 | + :param text: The text to encrypt |
| 106 | + :type text: str |
| 107 | + :returns: The encrypted text |
| 108 | + :rtype: str |
| 109 | + """ |
| 110 | + return self._impl.encrypt(text.encode()).decode() |
| 111 | + |
| 112 | + def decrypt(self, encrypted_text): |
| 113 | + """Decrypt an encrypted text |
| 114 | +
|
| 115 | + :param encrypted_text: The encrypted text |
| 116 | + :type encrypted_text: str |
| 117 | + :returns: The decrypted text |
| 118 | + :rtype: str |
| 119 | + """ |
| 120 | + return self._impl.decrypt(encrypted_text.encode()).decode() |
0 commit comments