github.com/rkt/rkt@v1.30.1-0.20200224141603-171c416fac02/pkg/keystore/keystoretest/opengpg.go (about)

     1  // Copyright 2014 The rkt Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  // Package keystoretest provides utilities for ACI keystore testing.
    16  //go:generate go run ./keygen/keygen.go
    17  package keystoretest
    18  
    19  import (
    20  	"bytes"
    21  	"errors"
    22  	"io"
    23  
    24  	"golang.org/x/crypto/openpgp"
    25  )
    26  
    27  // A KeyDetails represents an openpgp.Entity and its key details.
    28  type KeyDetails struct {
    29  	Fingerprint       string
    30  	ArmoredPublicKey  string
    31  	ArmoredPrivateKey string
    32  }
    33  
    34  // NewMessageAndSignature generates a new random message signed by the given entity.
    35  // NewMessageAndSignature returns message, signature and an error if any.
    36  func NewMessageAndSignature(armoredPrivateKey string) (io.ReadSeeker, io.ReadSeeker, error) {
    37  	entityList, err := openpgp.ReadArmoredKeyRing(bytes.NewBufferString(armoredPrivateKey))
    38  	if err != nil {
    39  		return nil, nil, err
    40  	}
    41  	if len(entityList) < 1 {
    42  		return nil, nil, errors.New("empty entity list")
    43  	}
    44  	signature := &bytes.Buffer{}
    45  	message := []byte("data")
    46  	if err := openpgp.ArmoredDetachSign(signature, entityList[0], bytes.NewReader(message), nil); err != nil {
    47  		return nil, nil, err
    48  	}
    49  	return bytes.NewReader(message), bytes.NewReader(signature.Bytes()), nil
    50  }