github.com/grailbio/base@v0.0.11/cmd/grail-access/remote/encoding.go (about)

     1  // Copyright 2022 GRAIL, Inc. All rights reserved.
     2  // Use of this source code is governed by the Apache-2.0
     3  // license that can be found in the LICENSE file.
     4  
     5  package remote
     6  
     7  import (
     8  	"encoding/base64"
     9  	"fmt"
    10  
    11  	"v.io/v23/security"
    12  	"v.io/v23/vom"
    13  )
    14  
    15  var b64 = base64.StdEncoding
    16  
    17  func encodeBlessings(b security.Blessings) (string, error) {
    18  	bs, err := vom.Encode(b)
    19  	if err != nil {
    20  		return "", fmt.Errorf("vom-encoding blessings: %v", err)
    21  	}
    22  	return b64.EncodeToString(bs), nil
    23  }
    24  
    25  func decodeBlessings(s string) (security.Blessings, error) {
    26  	b, err := b64.DecodeString(s)
    27  	if err != nil {
    28  		return security.Blessings{}, fmt.Errorf("base64 decoding blessings string: %v", err)
    29  	}
    30  	var blessings security.Blessings
    31  	if err := vom.Decode(b, &blessings); err != nil {
    32  		return security.Blessings{}, fmt.Errorf("vom-decoding: %v", err)
    33  	}
    34  	return blessings, nil
    35  }
    36  
    37  func encodePublicKey(k security.PublicKey) (string, error) {
    38  	der, err := k.MarshalBinary()
    39  	if err != nil {
    40  		return "", fmt.Errorf("corrupted public key: %v", err)
    41  	}
    42  	return b64.EncodeToString(der), nil
    43  }
    44  
    45  func decodePublicKey(s string) (security.PublicKey, error) {
    46  	bs, err := b64.DecodeString(s)
    47  	if err != nil {
    48  		return nil, fmt.Errorf("base64-decoding public key: %v", err)
    49  	}
    50  	key, err := security.UnmarshalPublicKey(bs)
    51  	if err != nil {
    52  		return nil, fmt.Errorf("unmarshalling public key: %v", err)
    53  	}
    54  	return key, nil
    55  }