Read crypto key from keyczar JSON key file format and return parsed AESKey object. :param key_path: Absolute path to file containing crypto key in Keyczar JSON format. :type key_path: ``str`` :rtype: :class:`AESKey`
(key_path)
| 181 | |
| 182 | |
| 183 | def read_crypto_key(key_path): |
| 184 | """ |
| 185 | Read crypto key from keyczar JSON key file format and return parsed AESKey object. |
| 186 | |
| 187 | :param key_path: Absolute path to file containing crypto key in Keyczar JSON format. |
| 188 | :type key_path: ``str`` |
| 189 | |
| 190 | :rtype: :class:`AESKey` |
| 191 | """ |
| 192 | with open(key_path, "r") as fp: |
| 193 | content = fp.read() |
| 194 | |
| 195 | content = json_decode(content) |
| 196 | |
| 197 | try: |
| 198 | aes_key = AESKey( |
| 199 | aes_key_string=content["aesKeyString"], |
| 200 | hmac_key_string=content["hmacKey"]["hmacKeyString"], |
| 201 | hmac_key_size=content["hmacKey"]["size"], |
| 202 | mode=content["mode"].upper(), |
| 203 | size=content["size"], |
| 204 | ) |
| 205 | except KeyError as e: |
| 206 | msg = 'Invalid or malformed key file "%s": %s' % (key_path, six.text_type(e)) |
| 207 | raise KeyError(msg) |
| 208 | |
| 209 | return aes_key |
| 210 | |
| 211 | |
| 212 | def symmetric_encrypt(encrypt_key, plaintext): |