def test_write_encrypted_read_encrypted(self): encrypted_keyset = example_encrypted_keyset() stream = io.StringIO() writer = core.JsonKeysetWriter(stream) writer.write_encrypted(encrypted_keyset) reader = core.JsonKeysetReader(stream.getvalue()) self.assertEqual(encrypted_keyset, reader.read_encrypted())
def test_write_read_with_unicode_chars(self): keyset = tink_pb2.Keyset() key = keyset.key.add() key.key_data.type_url = ( u'\xe3\x82\xb3\xe3\x83\xb3\xe3\x83\x8b\xe3\x83\x81\xe3\x83\x8f') stream = io.StringIO() writer = core.JsonKeysetWriter(stream) writer.write(keyset) reader = core.JsonKeysetReader(stream.getvalue()) self.assertEqual(keyset, reader.read())
def test_read(self): json_keyset = """ { "primaryKeyId": 42, "key": [ { "keyData": { "typeUrl": "type.googleapis.com/google.crypto.tink.AesGcmKey", "keyMaterialType": "SYMMETRIC", "value": "GhCS/1+ejWpx68NfGt6ziYHd" }, "outputPrefixType": "TINK", "keyId": 42, "status": "ENABLED" } ] }""" reader = core.JsonKeysetReader(json_keyset) keyset = reader.read() self.assertEqual(keyset.primary_key_id, 42) self.assertLen(keyset.key, 1)
def test_read_encrypted(self): # encryptedKeyset is a base64-encoding of 'some ciphertext with keyset' json_encrypted_keyset = """ { "encryptedKeyset": "c29tZSBjaXBoZXJ0ZXh0IHdpdGgga2V5c2V0", "keysetInfo": { "primaryKeyId": 42, "keyInfo": [ { "typeUrl": "type.googleapis.com/google.crypto.tink.AesGcmKey", "outputPrefixType": "TINK", "keyId": 42, "status": "ENABLED" } ] } }""" reader = core.JsonKeysetReader(json_encrypted_keyset) enc_keyset = reader.read_encrypted() self.assertEqual(enc_keyset.encrypted_keyset, b'some ciphertext with keyset') self.assertLen(enc_keyset.keyset_info.key_info, 1) self.assertEqual(enc_keyset.keyset_info.key_info[0].type_url, 'type.googleapis.com/google.crypto.tink.AesGcmKey')
def test_read_encrypted_invalid(self): with self.assertRaisesRegex(core.TinkError, 'Failed to load JSON'): reader = core.JsonKeysetReader('not json') reader.read_encrypted()