diff options
author | Paulo Gomes <pjbgf@linux.com> | 2024-02-21 10:48:14 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-02-21 10:48:14 +0000 |
commit | 686a0f7a492894fc3efd67e8be99a4240b9b65ec (patch) | |
tree | aa4dbdbe05982b274b4eca55f3f8e991889b9d28 /signer_test.go | |
parent | 6f46f8c0bd6397a0c827235c8106ce0799b24923 (diff) | |
parent | 9fa13d83c6e473d0aca7b97a620b3f4a003993f6 (diff) | |
download | go-git-686a0f7a492894fc3efd67e8be99a4240b9b65ec.tar.gz |
Merge pull request #1029 from wlynch/signer-fix
Signer: fix usage of crypto.Signer interface
Diffstat (limited to 'signer_test.go')
-rw-r--r-- | signer_test.go | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/signer_test.go b/signer_test.go new file mode 100644 index 0000000..eba0922 --- /dev/null +++ b/signer_test.go @@ -0,0 +1,56 @@ +package git + +import ( + "encoding/base64" + "fmt" + "io" + "time" + + "github.com/go-git/go-billy/v5/memfs" + "github.com/go-git/go-git/v5/plumbing/object" + "github.com/go-git/go-git/v5/storage/memory" +) + +type b64signer struct{} + +// This is not secure, and is only used as an example for testing purposes. +// Please don't do this. +func (b64signer) Sign(message io.Reader) ([]byte, error) { + b, err := io.ReadAll(message) + if err != nil { + return nil, err + } + out := make([]byte, base64.StdEncoding.EncodedLen(len(b))) + base64.StdEncoding.Encode(out, b) + return out, nil +} + +func ExampleSigner() { + repo, err := Init(memory.NewStorage(), memfs.New()) + if err != nil { + panic(err) + } + w, err := repo.Worktree() + if err != nil { + panic(err) + } + commit, err := w.Commit("example commit", &CommitOptions{ + Author: &object.Signature{ + Name: "John Doe", + Email: "john@example.com", + When: time.UnixMicro(1234567890).UTC(), + }, + Signer: b64signer{}, + AllowEmptyCommits: true, + }) + if err != nil { + panic(err) + } + + obj, err := repo.CommitObject(commit) + if err != nil { + panic(err) + } + fmt.Println(obj.PGPSignature) + // Output: dHJlZSA0YjgyNWRjNjQyY2I2ZWI5YTA2MGU1NGJmOGQ2OTI4OGZiZWU0OTA0CmF1dGhvciBKb2huIERvZSA8am9obkBleGFtcGxlLmNvbT4gMTIzNCArMDAwMApjb21taXR0ZXIgSm9obiBEb2UgPGpvaG5AZXhhbXBsZS5jb20+IDEyMzQgKzAwMDAKCmV4YW1wbGUgY29tbWl0 +} |