8000 sign/verify uses bytes instead of String · MagicLegends/python-docs-samples@927e3cc · GitHub
[go: up one dir, main page]

Skip to content

Commit 927e3cc

Browse files
committed
sign/verify uses bytes instead of String
1 parent e85865b commit 927e3cc

File tree

2 files changed

+12
-19
lines changed

2 files changed

+12
-19
lines changed

kms/api-client/asymmetric.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ def signAsymmetric(message, client, key_path):
9595
.asymmetricSign(name=key_path,
9696
body={'digest': digest_JSON})
9797
response = request.execute()
98-
return response.get('signature', None)
98+
return base64.b64decode(response.get('signature', None))
9999
# [END kms_sign_asymmetric]
100100

101101

@@ -106,13 +106,11 @@ def verifySignatureRSA(signature, message, client, key_path):
106106
specified message
107107
"""
108108
public_key = getAsymmetricPublicKey(client, key_path)
109-
110109
digest_bytes = hashlib.sha256(message).digest()
111-
sig_bytes = base64.b64decode(signature)
112110

113111
try:
114112
# Attempt verification
115-
public_key.verify(sig_bytes,
113+
public_key.verify(signature,
116114
digest_bytes,
117115
padding.PSS(mgf=padding.MGF1(hashes.SHA256()),
118116
salt_length=32),
@@ -131,13 +129,11 @@ def verifySignatureEC(signature, message, client, key_path):
131129
for the specified message
132130
"""
133131
public_key = getAsymmetricPublicKey(client, key_path)
134-
135132
digest_bytes = hashlib.sha256(message).digest()
136-
sig_bytes = base64.b64decode(signature)
137133

138134
try:
139135
# Attempt verification
140-
public_key.verify(sig_bytes,
136+
public_key.verify(signature,
141137
digest_bytes,
142138
ec.ECDSA(utils.Prehashed(hashes.SHA256())))
143139
# No errors were thrown. Verification was successful

kms/api-client/asymmetric_test.py

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
# See the License for the specific language governing permissions and
1414
# limitations under the License.
1515

16-
import base64
1716
from os import environ
1817
from time import sleep
1918

@@ -100,14 +99,13 @@ def test_get_public_key(self):
10099
assert isinstance(ec_key, _EllipticCurvePublicKey), 'expected EC key'
101100

102101
def test_rsa_encrypt_decrypt(self):
103-
ciphertext_bytes = sample.encryptRSA(self.message_bytes,
104-
self.client,
105-
self.rsaDecrypt)
106-
ciphertext = base64.b64encode(ciphertext_bytes).decode()
107-
# ciphertext should be 344 characters with base64 and RSA 2048
108-
assert len(ciphertext) == 344, \
109-
'ciphertext should be 344 chars; got {}'.format(len(ciphertext))
110-
plaintext_bytes = sample.decryptRSA(ciphertext_bytes,
102+
ciphertext = sample.encryptRSA(self.message_bytes,
103+
self.client,
104+
self.rsaDecrypt)
105+
# ciphertext should be 256 characters with base64 and RSA 2048
106+
assert len(ciphertext) == 256, \
107+
'ciphertext should be 256 chars; got {}'.format(len(ciphertext))
108+
plaintext_bytes = sample.decryptRSA(ciphertext,
111109
self.client,
112110
self.rsaDecrypt)
113111
assert plaintext_bytes == self.message_bytes
@@ -119,9 +117,8 @@ def test_rsa_sign_verify(self):
119117
self.client,
120118
self.rsaSign)
121119
# ciphertext should be 344 characters with base64 and RSA 2048
122-
assert len(sig) == 344, \
123-
'sig should be 344 chars; got {}'.format(len(sig))
124-
assert sig[-2:] == '==', 'sig should end with =='
120+
assert len(sig) == 256, \
121+
'sig should be 256 chars; got {}'.format(len(sig))
125122
success = sample.verifySignatureRSA(sig,
126123
self.message_bytes,
127124
self.client,

0 commit comments

Comments
 (0)
0