github.com/apache/arrow/go/v7@v7.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  	"unsafe"
    22  
    23  	"golang.org/x/xerrors"
    24  )
    25  
    26  // StringKeyIDRetriever implements the KeyRetriever interface GetKey
    27  // to allow setting in keys with a string id.
    28  type StringKeyIDRetriever map[string]string
    29  
    30  // PutKey adds a key with the given string ID that can be retrieved
    31  func (s StringKeyIDRetriever) PutKey(keyID, key string) {
    32  	s[keyID] = key
    33  }
    34  
    35  // GetKey expects the keymetadata to match one of the keys that were added
    36  // with PutKey and panics if the key cannot be found.
    37  func (s StringKeyIDRetriever) GetKey(keyMetadata []byte) string {
    38  	k, ok := s[*(*string)(unsafe.Pointer(&keyMetadata))]
    39  	if !ok {
    40  		panic(xerrors.Errorf("parquet: key missing for id %s", keyMetadata))
    41  	}
    42  	return k
    43  }
    44  
    45  // IntegerKeyIDRetriever is used for using unsigned 32bit integers as key ids.
    46  type IntegerKeyIDRetriever map[uint32]string
    47  
    48  // PutKey adds keys with uint32 IDs
    49  func (i IntegerKeyIDRetriever) PutKey(keyID uint32, key string) {
    50  	i[keyID] = key
    51  }
    52  
    53  // GetKey expects the key metadata bytes to be a little endian uint32 which
    54  // is then used to retrieve the key bytes. Panics if the key id cannot be found.
    55  func (i IntegerKeyIDRetriever) GetKey(keyMetadata []byte) string {
    56  	keyID := binary.LittleEndian.Uint32(keyMetadata)
    57  	k, ok := i[keyID]
    58  	if !ok {
    59  		panic(xerrors.Errorf("parquet: key missing for id %d", keyID))
    60  	}
    61  	return k
    62  }