|
4 | 4 | # -------------------------------------------------------------------------------------------- |
5 | 5 |
|
6 | 6 | # pylint: disable=too-many-lines |
| 7 | +import base64 |
7 | 8 | import codecs |
8 | 9 | import hashlib |
9 | 10 | import json |
@@ -1127,19 +1128,31 @@ def decrypt_key(cmd, client, algorithm, value, iv=None, tag=None, aad=None, |
1127 | 1128 |
|
1128 | 1129 |
|
1129 | 1130 | def sign_key(cmd, client, algorithm, digest, name=None, version=None): |
| 1131 | + if '256' in algorithm: |
| 1132 | + hash_func = hashlib.sha256 |
| 1133 | + elif '384' in algorithm: |
| 1134 | + hash_func = hashlib.sha384 |
| 1135 | + else: |
| 1136 | + hash_func = hashlib.sha512 |
1130 | 1137 | SignatureAlgorithm = cmd.loader.get_sdk('SignatureAlgorithm', mod='crypto._enums', |
1131 | 1138 | resource_type=ResourceType.DATA_KEYVAULT_KEYS) |
1132 | 1139 | crypto_client = client.get_cryptography_client(name, key_version=version) |
1133 | | - return crypto_client.sign(SignatureAlgorithm(algorithm), digest.encode('utf-8')) |
| 1140 | + return crypto_client.sign(SignatureAlgorithm(algorithm), |
| 1141 | + hash_func(base64.b64decode(digest.encode('utf-8'))).digest()) |
1134 | 1142 |
|
1135 | 1143 |
|
1136 | 1144 | def verify_key(cmd, client, algorithm, digest, signature, name=None, version=None): |
1137 | | - import base64 |
| 1145 | + if '256' in algorithm: |
| 1146 | + hash_func = hashlib.sha256 |
| 1147 | + elif '384' in algorithm: |
| 1148 | + hash_func = hashlib.sha384 |
| 1149 | + else: |
| 1150 | + hash_func = hashlib.sha512 |
1138 | 1151 | SignatureAlgorithm = cmd.loader.get_sdk('SignatureAlgorithm', mod='crypto._enums', |
1139 | 1152 | resource_type=ResourceType.DATA_KEYVAULT_KEYS) |
1140 | 1153 | crypto_client = client.get_cryptography_client(name, key_version=version) |
1141 | 1154 | return crypto_client.verify(SignatureAlgorithm(algorithm), |
1142 | | - digest.encode('utf-8'), |
| 1155 | + hash_func(base64.b64decode(digest.encode('utf-8'))).digest(), |
1143 | 1156 | base64.b64decode(signature.encode('utf-8'))) |
1144 | 1157 |
|
1145 | 1158 |
|
@@ -1493,7 +1506,6 @@ def download_secret(client, file_path, name=None, encoding=None, version=''): # |
1493 | 1506 | f.write(secret_value) |
1494 | 1507 | else: |
1495 | 1508 | if encoding == 'base64': |
1496 | | - import base64 |
1497 | 1509 | decoded = base64.b64decode(secret_value) |
1498 | 1510 | elif encoding == 'hex': |
1499 | 1511 | import binascii |
@@ -1558,7 +1570,6 @@ def download_certificate(client, file_path, certificate_name=None, encoding='PEM |
1558 | 1570 | if encoding == 'DER': |
1559 | 1571 | f.write(cert) |
1560 | 1572 | else: |
1561 | | - import base64 |
1562 | 1573 | encoded = base64.encodebytes(cert) |
1563 | 1574 | if isinstance(encoded, bytes): |
1564 | 1575 | encoded = encoded.decode("utf-8") |
|
0 commit comments