xref: /aosp_15_r20/external/tink/python/examples/walkthrough/create_keyset.py (revision e7b1675dde1b92d52ec075b0a92829627f2c52a5)
1# Copyright 2022 Google LLC
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#      http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS-IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14"""Example to showcase how to create a keyset."""
15# [START tink_walkthrough_create_keyset]
16import tink
17from tink import aead
18
19
20def CreateAead128GcmKeyset() -> tink.KeysetHandle:
21  """Creates a keyset with a single AES128-GCM key and return a handle to it.
22
23  Prerequisites:
24    - Register AEAD implementations of Tink.
25
26  Returns:
27    A handle to the created keyset.
28
29  Raises:
30    tink.TinkError in case of errors.
31  """
32  # Tink provides pre-baked templates. For example, we generate a key template
33  # for AES128-GCM.
34  key_template = aead.aead_key_templates.AES128_GCM
35  # This will generate a new keyset with only *one* key and return a keyset
36  # handle to it.
37  return tink.new_keyset_handle(key_template)
38
39
40# [END tink_walkthrough_create_keyset]
41