|
| 1 | +# Copyright 2026 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 | +# |
| 15 | +# DO NOT MODIFY THIS FILE DIRECTLY. |
| 16 | +# This file was generated from: scripts/data/sql-functions/aead.yaml |
| 17 | +# by the script: scripts/generate_bigframes_bigquery.py |
| 18 | + |
| 19 | +from __future__ import annotations |
| 20 | + |
| 21 | +import datetime |
| 22 | +from typing import Any, Literal, Optional, TypeVar, Union |
| 23 | + |
| 24 | +import bigframes.bigquery._googlesql |
| 25 | +import bigframes.core.col |
| 26 | +import bigframes.core.expression as ex |
| 27 | +import bigframes.core.sentinels as sentinels |
| 28 | +import bigframes.operations as ops |
| 29 | +import bigframes.series as series |
| 30 | +from bigframes import dtypes |
| 31 | +from bigframes.operations import googlesql |
| 32 | + |
| 33 | +T = TypeVar("T", series.Series, bigframes.core.col.Expression) |
| 34 | + |
| 35 | +_DECRYPT_BYTES_OP = googlesql.GoogleSqlScalarOp( |
| 36 | + "AEAD.DECRYPT_BYTES", |
| 37 | + args=(googlesql.ArgSpec(), googlesql.ArgSpec(), googlesql.ArgSpec()), |
| 38 | + signature=lambda *args: dtypes.BYTES_DTYPE, |
| 39 | +) |
| 40 | +_DECRYPT_STRING_OP = googlesql.GoogleSqlScalarOp( |
| 41 | + "AEAD.DECRYPT_STRING", |
| 42 | + args=(googlesql.ArgSpec(), googlesql.ArgSpec(), googlesql.ArgSpec()), |
| 43 | + signature=lambda *args: dtypes.STRING_DTYPE, |
| 44 | +) |
| 45 | +_ENCRYPT_OP = googlesql.GoogleSqlScalarOp( |
| 46 | + "AEAD.ENCRYPT", |
| 47 | + args=(googlesql.ArgSpec(), googlesql.ArgSpec(), googlesql.ArgSpec()), |
| 48 | + signature=lambda *args: dtypes.BYTES_DTYPE, |
| 49 | +) |
| 50 | + |
| 51 | + |
| 52 | +def decrypt_bytes( |
| 53 | + keyset: Union[ |
| 54 | + T, |
| 55 | + bigframes.core.col.Expression, |
| 56 | + Union[Literal[sentinels.Sentinel.ARGUMENT_DEFAULT], bytes, dict], |
| 57 | + ], |
| 58 | + ciphertext: Union[ |
| 59 | + T, |
| 60 | + bigframes.core.col.Expression, |
| 61 | + Union[Literal[sentinels.Sentinel.ARGUMENT_DEFAULT], bytes], |
| 62 | + ], |
| 63 | + additional_data: Union[ |
| 64 | + T, |
| 65 | + bigframes.core.col.Expression, |
| 66 | + Union[Literal[sentinels.Sentinel.ARGUMENT_DEFAULT], bytes], |
| 67 | + ], |
| 68 | +) -> T: |
| 69 | + """Uses the matching key from keyset to decrypt ciphertext and verifies the integrity of the data using additional_data. Returns an error if decryption or verification fails.""" |
| 70 | + return bigframes.bigquery._googlesql.apply_googlesql_scalar_op( |
| 71 | + _DECRYPT_BYTES_OP, |
| 72 | + keyset, |
| 73 | + ciphertext, |
| 74 | + additional_data, |
| 75 | + ) # type: ignore |
| 76 | + |
| 77 | + |
| 78 | +def decrypt_string( |
| 79 | + keyset: Union[ |
| 80 | + T, |
| 81 | + bigframes.core.col.Expression, |
| 82 | + Union[Literal[sentinels.Sentinel.ARGUMENT_DEFAULT], bytes, dict], |
| 83 | + ], |
| 84 | + ciphertext: Union[ |
| 85 | + T, |
| 86 | + bigframes.core.col.Expression, |
| 87 | + Union[Literal[sentinels.Sentinel.ARGUMENT_DEFAULT], bytes], |
| 88 | + ], |
| 89 | + additional_data: Union[ |
| 90 | + T, |
| 91 | + bigframes.core.col.Expression, |
| 92 | + Union[Literal[sentinels.Sentinel.ARGUMENT_DEFAULT], str], |
| 93 | + ], |
| 94 | +) -> T: |
| 95 | + """Like AEAD.DECRYPT_BYTES, but where additional_data is of type STRING.""" |
| 96 | + return bigframes.bigquery._googlesql.apply_googlesql_scalar_op( |
| 97 | + _DECRYPT_STRING_OP, |
| 98 | + keyset, |
| 99 | + ciphertext, |
| 100 | + additional_data, |
| 101 | + ) # type: ignore |
| 102 | + |
| 103 | + |
| 104 | +def encrypt( |
| 105 | + keyset: Union[ |
| 106 | + T, |
| 107 | + bigframes.core.col.Expression, |
| 108 | + Union[Literal[sentinels.Sentinel.ARGUMENT_DEFAULT], bytes, dict], |
| 109 | + ], |
| 110 | + plaintext: Union[ |
| 111 | + T, |
| 112 | + bigframes.core.col.Expression, |
| 113 | + Union[Literal[sentinels.Sentinel.ARGUMENT_DEFAULT], bytes, str], |
| 114 | + ], |
| 115 | + additional_data: Union[ |
| 116 | + T, |
| 117 | + bigframes.core.col.Expression, |
| 118 | + Union[Literal[sentinels.Sentinel.ARGUMENT_DEFAULT], bytes, str], |
| 119 | + ], |
| 120 | +) -> T: |
| 121 | + """Encrypts plaintext using the primary cryptographic key in keyset. The algorithm of the primary key must be AEAD_AES_GCM_256. Binds the ciphertext to the context defined by additional_data. Returns NULL if any input is NULL.""" |
| 122 | + return bigframes.bigquery._googlesql.apply_googlesql_scalar_op( |
| 123 | + _ENCRYPT_OP, |
| 124 | + keyset, |
| 125 | + plaintext, |
| 126 | + additional_data, |
| 127 | + ) # type: ignore |
0 commit comments