github.com/qri-io/qri@v0.10.1-0.20220104210721-c771715036cb/auth/key/key.go (about)

     1  package key
     2  
     3  import (
     4  	"encoding/base64"
     5  	"fmt"
     6  
     7  	logger "github.com/ipfs/go-log"
     8  	"github.com/libp2p/go-libp2p-core/crypto"
     9  	peer "github.com/libp2p/go-libp2p-core/peer"
    10  )
    11  
    12  var log = logger.Logger("key")
    13  
    14  // ID is a key identifier
    15  // note that while this implementation is an alias for a peer.ID, this is not
    16  // strictly a peerID.
    17  type ID = peer.ID
    18  
    19  // DecodeID parses an ID string
    20  func DecodeID(s string) (ID, error) {
    21  	pid, err := peer.Decode(s)
    22  	if err != nil {
    23  		return "", err
    24  	}
    25  	return ID(pid), nil
    26  }
    27  
    28  // IDFromPrivKey is a wrapper for calling IDFromPubKey on a private key
    29  func IDFromPrivKey(pk crypto.PrivKey) (string, error) {
    30  	return IDFromPubKey(pk.GetPublic())
    31  }
    32  
    33  // IDFromPubKey returns an ID string is a that is unique identifier for a
    34  // keypair. For RSA keys this is the base58btc-encoded multihash string of
    35  // the public key. hashes are 32-byte sha2-256 sums of public key bytes
    36  func IDFromPubKey(pubKey crypto.PubKey) (string, error) {
    37  	if pubKey == nil {
    38  		return "", fmt.Errorf("public key is required")
    39  	}
    40  
    41  	id, err := peer.IDFromPublicKey(pubKey)
    42  	if err != nil {
    43  		return "", err
    44  	}
    45  	return id.Pretty(), err
    46  }
    47  
    48  // EncodePubKeyB64 serializes a public key to a base64-encoded string
    49  func EncodePubKeyB64(pub crypto.PubKey) (string, error) {
    50  	if pub == nil {
    51  		return "", fmt.Errorf("cannot encode nil public key")
    52  	}
    53  	pubBytes, err := pub.Bytes()
    54  	if err != nil {
    55  		return "", err
    56  	}
    57  	return base64.StdEncoding.EncodeToString(pubBytes), nil
    58  }
    59  
    60  // DecodeB64PubKey deserializes a base-64 encoded key string into a public key
    61  func DecodeB64PubKey(keystr string) (pk crypto.PubKey, err error) {
    62  	d, err := base64.StdEncoding.DecodeString(keystr)
    63  	if err != nil {
    64  		return nil, fmt.Errorf("decoding base64-encoded public key: %w", err)
    65  	}
    66  	pubKey, err := crypto.UnmarshalPublicKey(d)
    67  	if err != nil {
    68  		return nil, fmt.Errorf("public key %q is invalid: %w", keystr, err)
    69  	}
    70  	return pubKey, nil
    71  }
    72  
    73  // EncodePrivKeyB64 serializes a private key to a base64-encoded string
    74  func EncodePrivKeyB64(pk crypto.PrivKey) (string, error) {
    75  	if pk == nil {
    76  		return "", fmt.Errorf("cannot encode nil private key")
    77  	}
    78  	pdata, err := pk.Bytes()
    79  	return base64.StdEncoding.EncodeToString(pdata), err
    80  }
    81  
    82  // DecodeB64PrivKey deserializes a base-64 encoded key string into a private
    83  // key
    84  func DecodeB64PrivKey(keystr string) (pk crypto.PrivKey, err error) {
    85  	data, err := base64.StdEncoding.DecodeString(keystr)
    86  	if err != nil {
    87  		return nil, fmt.Errorf("decoding base64-encoded private key: %w", err)
    88  	}
    89  
    90  	pk, err = crypto.UnmarshalPrivateKey(data)
    91  	if err != nil {
    92  		return nil, fmt.Errorf("invalid private key: %w", err)
    93  	}
    94  
    95  	return pk, nil
    96  }