1313# See the License for the specific language governing permissions and
1414# limitations under the License.rom googleapiclient import discovery
1515
16+ # [START kms_asymmetric_imports]
1617import base64
1718import hashlib
1819
1920from cryptography .exceptions import InvalidSignature
2021from cryptography .hazmat .backends import default_backend
2122from cryptography .hazmat .primitives import hashes , serialization
2223from cryptography .hazmat .primitives .asymmetric import ec , padding , utils
24+ # [END kms_asymmetric_imports]
2325
2426
2527# [START kms_get_asymmetric_public]
@@ -43,35 +45,34 @@ def getAsymmetricPublicKey(client, key_path):
4345# [START kms_decrypt_rsa]
4446def decryptRSA (ciphertext , client , key_path ):
4547 """
46- Decrypt a given ciphertext using an 'RSA_DECRYPT_OAEP_2048_SHA256' private
47- 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
4850 """
51+ request_body = {'ciphertext' : base64 .b64encode (ciphertext ).decode ('utf-8' )}
4952 request = client .projects () \
5053 .locations () \
5154 .keyRings () \
5255 .cryptoKeys () \
5356 .cryptoKeyVersions () \
5457 .asymmetricDecrypt (name = key_path ,
55- body = { 'ciphertext' : ciphertext } )
58+ body = request_body )
5659 response = request .execute ()
57- plaintext = base64 .b64decode (response ['plaintext' ]). decode ( 'utf-8' )
60+ plaintext = base64 .b64decode (response ['plaintext' ])
5861 return plaintext
5962# [END kms_decrypt_rsa]
6063
6164
6265# [START kms_encrypt_rsa]
63- def encryptRSA (message , client , key_path ):
66+ def encryptRSA (plaintext , client , key_path ):
6467 """
65- Encrypt message locally using an 'RSA_DECRYPT_OAEP_2048_SHA256' public
66- 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
6770 """
6871 public_key = getAsymmetricPublicKey (client , key_path )
6972 pad = padding .OAEP (mgf = padding .MGF1 (algorithm = hashes .SHA256 ()),
7073 algorithm = hashes .SHA256 (),
7174 label = None )
72- ciphertext = public_key .encrypt (message .encode ('ascii' ), pad )
73- ciphertext = base64 .b64encode (ciphertext ).decode ('utf-8' )
74- return ciphertext
75+ return public_key .encrypt (plaintext , pad )
7576# [END kms_encrypt_rsa]
7677
7778
@@ -82,7 +83,7 @@ def signAsymmetric(message, client, key_path):
8283 """
8384 # Note: some key algorithms will require a different hash function
8485 # For example, EC_SIGN_P384_SHA384 requires SHA384
85- digest_bytes = hashlib .sha256 (message . encode ( 'ascii' ) ).digest ()
86+ digest_bytes = hashlib .sha256 (message ).digest ()
8687 digest64 = base64 .b64encode (digest_bytes )
8788
8889 digest_JSON = {'sha256' : digest64 .decode ('utf-8' )}
@@ -94,24 +95,22 @@ def signAsymmetric(message, client, key_path):
9495 .asymmetricSign (name = key_path ,
9596 body = {'digest' : digest_JSON })
9697 response = request .execute ()
97- return response .get ('signature' , None )
98+ return base64 . b64decode ( response .get ('signature' , None ) )
9899# [END kms_sign_asymmetric]
99100
100101
101102# [START kms_verify_signature_rsa]
102103def verifySignatureRSA (signature , message , client , key_path ):
103104 """
104105 Verify the validity of an 'RSA_SIGN_PSS_2048_SHA256' signature for the
105- specified plaintext message
106+ specified message
106107 """
107108 public_key = getAsymmetricPublicKey (client , key_path )
108-
109- digest_bytes = hashlib .sha256 (message .encode ('ascii' )).digest ()
110- sig_bytes = base64 .b64decode (signature )
109+ digest_bytes = hashlib .sha256 (message ).digest ()
111110
112111 try :
113112 # Attempt verification
114- public_key .verify (sig_bytes ,
113+ public_key .verify (signature ,
115114 digest_bytes ,
116115 padding .PSS (mgf = padding .MGF1 (hashes .SHA256 ()),
117116 salt_length = 32 ),
@@ -127,16 +126,14 @@ def verifySignatureRSA(signature, message, client, key_path):
127126def verifySignatureEC (signature , message , client , key_path ):
128127 """
129128 Verify the validity of an 'EC_SIGN_P256_SHA256' signature
130- for the specified plaintext message
129+ for the specified message
131130 """
132131 public_key = getAsymmetricPublicKey (client , key_path )
133-
134- digest_bytes = hashlib .sha256 (message .encode ('ascii' )).digest ()
135- sig_bytes = base64 .b64decode (signature )
132+ digest_bytes = hashlib .sha256 (message ).digest ()
136133
137134 try :
138135 # Attempt verification
139- public_key .verify (sig_bytes ,
136+ public_key .verify (signature ,
140137 digest_bytes ,
141138 ec .ECDSA (utils .Prehashed (hashes .SHA256 ())))
142139 # No errors were thrown. Verification was successful
0 commit comments