34 lines
865 B
Go
34 lines
865 B
Go
package worktree
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"fmt"
|
|
"io"
|
|
|
|
"github.com/go-git/go-git/v6"
|
|
"github.com/hiddeco/sshsig"
|
|
"golang.org/x/crypto/ssh"
|
|
)
|
|
|
|
// sshSigner signs commits the way `git -c gpg.format=ssh` does, so forges
|
|
// verify them against the account's SSH signing key.
|
|
type sshSigner struct{ key ssh.Signer }
|
|
|
|
// NewSSHSigner parses an OpenSSH private key for commit signing.
|
|
func NewSSHSigner(privateKey []byte) (git.Signer, error) {
|
|
key, err := ssh.ParsePrivateKey(privateKey)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("parse signing key: %w", err)
|
|
}
|
|
return sshSigner{key: key}, nil
|
|
}
|
|
|
|
func (s sshSigner) Sign(_ context.Context, message io.Reader) ([]byte, error) {
|
|
sig, err := sshsig.Sign(message, s.key, sshsig.HashSHA512, "git")
|
|
if err != nil {
|
|
return nil, fmt.Errorf("ssh sign: %w", err)
|
|
}
|
|
return bytes.TrimSpace(sshsig.Armor(sig)), nil
|
|
}
|