8000 addressed PR comments · MagicLegends/python-docs-samples@e85865b · GitHub
[go: up one dir, main page]

Skip to content

Commit e85865b

Browse files
committed
addressed PR comments
1 parent 09927c4 commit e85865b

File tree

2 files changed

+10
-11
lines changed

2 files changed

+10
-11
lines changed

kms/api-client/asymmetric.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,10 @@ def getAsymmetricPublicKey(client, key_path):
4545
# [START kms_decrypt_rsa]
4646
def decryptRSA(ciphertext, client, key_path):
4747
"""
48-
Decrypt the given ciphertext using an 'RSA_DECRYPT_OAEP_2048_SHA256' private
49-
key stored on Cloud KMS
48+
Decrypt the input ciphertext (bytes) using an
49+
'RSA_DECRYPT_OAEP_2048_SHA256' private key stored on Cloud KMS
5050
"""
51-
request_body = {'ciphertext': ciphertext.decode()}
51+
request_body = {'ciphertext': base64.b64encode(ciphertext).decode()}
5252
request = client.projects() \
5353
.locations() \
5454
.keyRings() \
@@ -65,15 +65,14 @@ def decryptRSA(ciphertext, client, key_path):
6565
# [START kms_encrypt_rsa]
6666
def encryptRSA(plaintext, client, key_path):
6767
"""
68-
Encrypt data locally using an 'RSA_DECRYPT_OAEP_2048_SHA256' public
69-
key retrieved from Cloud KMS
68+
Encrypt the input plaintext (bytes) locally using an
69+
'RSA_DECRYPT_OAEP_2048_SHA256' public key retrieved from Cloud KMS
7070
"""
7171
public_key = getAsymmetricPublicKey(client, key_path)
7272
pad = padding.OAEP(mgf=padding.MGF1(algorithm=hashes.SHA256()),
7373
algorithm=hashes.SHA256(),
7474
label=None)
75-
ciphertext = public_key.encrypt(plaintext, pad)
76-
return base64.b64encode(ciphertext)
75+
return public_key.encrypt(plaintext, pad)
7776
# [END kms_encrypt_rsa]
7877

7978

kms/api-client/asymmetric_test.py

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

16+
import base64
1617
from os import environ
1718
from time import sleep
1819

@@ -102,11 +103,10 @@ def test_rsa_encrypt_decrypt(self):
102103
ciphertext_bytes = sample.encryptRSA(self.message_bytes,
103104
self.client,
104105
self.rsaDecrypt)
105-
ciphertext = ciphertext_bytes.decode('utf-8')
106+
ciphertext = base64.b64encode(ciphertext_bytes).decode()
106107
# ciphertext should be 344 characters with base64 and RSA 2048
107108
assert len(ciphertext) == 344, \
108109
'ciphertext should be 344 chars; got {}'.format(len(ciphertext))
109-
assert cipherte B034 xt[-2:] == '==', 'cipher text should end with =='
110110
plaintext_bytes = sample.decryptRSA(ciphertext_bytes,
111111
self.client,
112112
self.rsaDecrypt)
@@ -127,7 +127,7 @@ def test_rsa_sign_verify(self):
127127
self.client,
128128
self.rsaSign)
129129
assert success is True, 'RSA verification failed'
130-
changed_bytes = (self.message+".").encode('utf-8')
130+
changed_bytes = self.message_bytes + b'.'
131131
success = sample.verifySignatureRSA(sig,
132132
changed_bytes,
133133
self.client,
@@ -145,7 +145,7 @@ def test_ec_sign_verify(self):
145145
self.client,
146146
self.ecSign)
147147
assert success is True, 'EC verification failed'
148-
changed_bytes = (self.message+".").encode('utf-8')
148+
changed_bytes = self.message_bytes + b'.'
149149
success = sample.verifySignatureEC(sig,
150150
changed_bytes,
151151
self.client,

0 commit comments

Comments
 (0)
0