xref: /aosp_15_r20/external/tink/python/examples/walkthrough/load_cleartext_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 load a cleartext keyset."""
15# [START tink_walkthrough_load_cleartext_keyset]
16import tink
17
18from tink import cleartext_keyset_handle
19
20
21def LoadKeyset(serialized_keyset: str) -> tink.KeysetHandle:
22  r"""Loads a JSON-serialized unencrypted keyset and returns a KeysetHandle.
23
24  Prerequisites for this example:
25    - Create an plaintext keyset in JSON, for example, using Tinkey:
26
27      tinkey create-key --key-template AES256_GCM --out-format json \
28        --out keyset.json
29
30  Args:
31    serialized_keyset: JSON serialized keyset.
32
33  Returns:
34    A handle to the loaded keyset.
35
36  Raises:
37    tink.TinkError in case of errors.
38  """
39  return cleartext_keyset_handle.read(tink.JsonKeysetReader(serialized_keyset))
40
41
42# [END tink_walkthrough_load_cleartext_keyset]
43