forked from GoogleCloudPlatform/python-runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcolumnencryption
More file actions
22 lines (16 loc) · 764 Bytes
/
Copy pathcolumnencryption
File metadata and controls
22 lines (16 loc) · 764 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
from google.cloud import kms_v1
from google.cloud.kms_v1 import enums
from google.protobuf import field_mask_pb2
import base64
# Initialize the KMS client
client = kms_v1.KeyManagementServiceClient()
# Set your KMS key name
kms_key_name = "projects/model-height-439805-h4/locations/global/keyRings/my-key-ring/cryptoKeys/my-crypto-key"
# The data you want to encrypt (SSN in this case)
data_to_encrypt = "123-45-6789"
# Encrypt the data
response = client.encrypt(name=kms_key_name, plaintext=data_to_encrypt.encode("utf-8"))
# The encrypted data will be in the 'ciphertext' field
encrypted_data = base64.b64encode(response.ciphertext).decode('utf-8')
# Print the encrypted data (you can now store this in BigQuery)
print(f"Encrypted Data: {encrypted_data}")