-------------------- Converts a key value from one stated format into requested encoding format. Args: key (any): Key value, typically either str [base64, hex] or bytes. encoding_in (str): Encoding of supplied key, as SXTKeyEncodings encoding_ou
(self, key, encoding_in:SXTKeyEncodings = SXTKeyEncodings.BYTES
, encoding_out:SXTKeyEncodings = SXTKeyEncodings.HEX)
| 156 | |
| 157 | |
| 158 | def convert_key(self, key, encoding_in:SXTKeyEncodings = SXTKeyEncodings.BYTES |
| 159 | , encoding_out:SXTKeyEncodings = SXTKeyEncodings.HEX): |
| 160 | """-------------------- |
| 161 | Converts a key value from one stated format into requested encoding format. |
| 162 | |
| 163 | Args: |
| 164 | key (any): Key value, typically either str [base64, hex] or bytes. |
| 165 | encoding_in (str): Encoding of supplied key, as SXTKeyEncodings |
| 166 | encoding_out (str): Encoding of returned key, as SXTKeyEncodings |
| 167 | |
| 168 | Return: |
| 169 | dict: Converted key the encoding_out encoding. |
| 170 | |
| 171 | Examples: |
| 172 | >>> SXTKeyManager().convert_key('0123456789abcdef', SXTKeyEncodings.HEX, SXTKeyEncodings.BASE64) |
| 173 | ASNFZ4mrze8= |
| 174 | >>> SXTKeyManager().convert_key('ASNFZ4mrze8=', SXTKeyEncodings.BASE64, SXTKeyEncodings.HEX) |
| 175 | 0123456789abcdef |
| 176 | """ |
| 177 | try: |
| 178 | # always take to bytes first |
| 179 | if not key: |
| 180 | key_bytes = bytes(b'') |
| 181 | elif encoding_in == SXTKeyEncodings.BYTES: |
| 182 | key_bytes = bytes(key) |
| 183 | elif encoding_in == SXTKeyEncodings.BASE64: |
| 184 | key_bytes = base64.b64decode(key) |
| 185 | elif encoding_in == SXTKeyEncodings.HEX: |
| 186 | key_bytes = bytes.fromhex(key) |
| 187 | |
| 188 | # format as requested encoding |
| 189 | if encoding_out == SXTKeyEncodings.BYTES: |
| 190 | key_out = key_bytes |
| 191 | elif encoding_out == SXTKeyEncodings.BASE64: |
| 192 | key_out = base64.b64encode(key_bytes).decode('utf-8') |
| 193 | elif encoding_out == SXTKeyEncodings.HEX: |
| 194 | key_out = key_bytes.hex() |
| 195 | |
| 196 | # self.logger.debug(f'Key verified and converted from {encoding_in.name} to {encoding_out.name}.') |
| 197 | return key_out |
| 198 | except Exception as ex: |
| 199 | error = ex |
| 200 | raise SxTKeyEncodingError(f'Error: {error}, going from {encoding_in.name} to {encoding_out.name}', logger=self.logger) |
| 201 | |
| 202 | |
| 203 | def get_KeyPair(self) ->KeyPair: |
no test coverage detected