github.com/zntrio/harp/v2@v2.0.9/pkg/sdk/value/encryption/context.go (about)

     1  // Licensed to Elasticsearch B.V. under one or more contributor
     2  // license agreements. See the NOTICE file distributed with
     3  // this work for additional information regarding copyright
     4  // ownership. Elasticsearch B.V. licenses this file to you under
     5  // the Apache License, Version 2.0 (the "License"); you may
     6  // not use this file except in compliance with the License.
     7  // You may obtain a copy of the License at
     8  //
     9  //     http://www.apache.org/licenses/LICENSE-2.0
    10  //
    11  // Unless required by applicable law or agreed to in writing,
    12  // software distributed under the License is distributed on an
    13  // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    14  // KIND, either express or implied.  See the License for the
    15  // specific language governing permissions and limitations
    16  // under the License.
    17  
    18  package encryption
    19  
    20  import "context"
    21  
    22  type contextKey string
    23  
    24  func (c contextKey) String() string {
    25  	return "github.com/zntrio/harp/v2/pkg/sdk/value/encryption#" + string(c)
    26  }
    27  
    28  var (
    29  	contextKeyNonce = contextKey("nonce")
    30  	contextKeyAAD   = contextKey("aad")
    31  )
    32  
    33  func WithNonce(ctx context.Context, value []byte) context.Context {
    34  	return context.WithValue(ctx, contextKeyNonce, value)
    35  }
    36  
    37  // Nonce gets the nonce value from the context.
    38  func Nonce(ctx context.Context) ([]byte, bool) {
    39  	nonce, ok := ctx.Value(contextKeyNonce).([]byte)
    40  	return nonce, ok
    41  }
    42  
    43  func WithAdditionalData(ctx context.Context, value []byte) context.Context {
    44  	return context.WithValue(ctx, contextKeyAAD, value)
    45  }
    46  
    47  // AdditionalData gets the aad value from the context.
    48  func AdditionalData(ctx context.Context) ([]byte, bool) {
    49  	aad, ok := ctx.Value(contextKeyAAD).([]byte)
    50  	return aad, ok
    51  }