github.com/apache/arrow/go/v14@v14.0.1/parquet/internal/encryption/key_handling.go (about)

     1  // Licensed to the Apache Software Foundation (ASF) under one
     2  // or more contributor license agreements.  See the NOTICE file
     3  // distributed with this work for additional information
     4  // regarding copyright ownership.  The ASF licenses this file
     5  // to you under the Apache License, Version 2.0 (the
     6  // "License"); you may not use this file except in compliance
     7  // with the License.  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, software
    12  // distributed under the License is distributed on an "AS IS" BASIS,
    13  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14  // See the License for the specific language governing permissions and
    15  // limitations under the License.
    16  
    17  package encryption
    18  
    19  import (
    20  	"encoding/binary"
    21  	"fmt"
    22  	"unsafe"
    23  )
    24  
    25  // StringKeyIDRetriever implements the KeyRetriever interface GetKey
    26  // to allow setting in keys with a string id.
    27  type StringKeyIDRetriever map[string]string
    28  
    29  // PutKey adds a key with the given string ID that can be retrieved
    30  func (s StringKeyIDRetriever) PutKey(keyID, key string) {
    31  	s[keyID] = key
    32  }
    33  
    34  // GetKey expects the keymetadata to match one of the keys that were added
    35  // with PutKey and panics if the key cannot be found.
    36  func (s StringKeyIDRetriever) GetKey(keyMetadata []byte) string {
    37  	k, ok := s[*(*string)(unsafe.Pointer(&keyMetadata))]
    38  	if !ok {
    39  		panic(fmt.Errorf("parquet: key missing for id %s", keyMetadata))
    40  	}
    41  	return k
    42  }
    43  
    44  // IntegerKeyIDRetriever is used for using unsigned 32bit integers as key ids.
    45  type IntegerKeyIDRetriever map[uint32]string
    46  
    47  // PutKey adds keys with uint32 IDs
    48  func (i IntegerKeyIDRetriever) PutKey(keyID uint32, key string) {
    49  	i[keyID] = key
    50  }
    51  
    52  // GetKey expects the key metadata bytes to be a little endian uint32 which
    53  // is then used to retrieve the key bytes. Panics if the key id cannot be found.
    54  func (i IntegerKeyIDRetriever) GetKey(keyMetadata []byte) string {
    55  	keyID := binary.LittleEndian.Uint32(keyMetadata)
    56  	k, ok := i[keyID]
    57  	if !ok {
    58  		panic(fmt.Errorf("parquet: key missing for id %d", keyID))
    59  	}
    60  	return k
    61  }