github.com/arieschain/arieschain@v0.0.0-20191023063405-37c074544356/internal/build/pgp.go (about)

     1  // signFile reads the contents of an input file and signs it (in armored format)
     2  // with the key provided, placing the signature into the output file.
     3  
     4  package build
     5  
     6  import (
     7  	"bytes"
     8  	"fmt"
     9  	"os"
    10  
    11  	"golang.org/x/crypto/openpgp"
    12  )
    13  
    14  // PGPSignFile parses a PGP private key from the specified string and creates a
    15  // signature file into the output parameter of the input file.
    16  //
    17  // Note, this method assumes a single key will be container in the pgpkey arg,
    18  // furthermore that it is in armored format.
    19  func PGPSignFile(input string, output string, pgpkey string) error {
    20  	// Parse the keyring and make sure we only have a single private key in it
    21  	keys, err := openpgp.ReadArmoredKeyRing(bytes.NewBufferString(pgpkey))
    22  	if err != nil {
    23  		return err
    24  	}
    25  	if len(keys) != 1 {
    26  		return fmt.Errorf("key count mismatch: have %d, want %d", len(keys), 1)
    27  	}
    28  	// Create the input and output streams for signing
    29  	in, err := os.Open(input)
    30  	if err != nil {
    31  		return err
    32  	}
    33  	defer in.Close()
    34  
    35  	out, err := os.Create(output)
    36  	if err != nil {
    37  		return err
    38  	}
    39  	defer out.Close()
    40  
    41  	// Generate the signature and return
    42  	return openpgp.ArmoredDetachSign(out, keys[0], in, nil)
    43  }