Skip to content

Commit 147c42b

Browse files
committed
Initial "dgst" implementation for freebsd and alpine packages.
Signed-off-by: Igor Ippolitov <iippolitov@gmail.com>
1 parent bdc9acb commit 147c42b

2 files changed

Lines changed: 93 additions & 0 deletions

File tree

main_client.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ import (
4747
_ "github.com/sassoftware/relic/v8/signers/vsix"
4848
_ "github.com/sassoftware/relic/v8/signers/xap"
4949
_ "github.com/sassoftware/relic/v8/signers/xar"
50+
_ "github.com/sassoftware/relic/v8/signers/blob"
5051
)
5152

5253
var (

signers/blob/signer.go

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package blob
2+
3+
import (
4+
5+
"io"
6+
"os"
7+
"fmt"
8+
"errors"
9+
"crypto"
10+
"crypto/rand"
11+
12+
"github.com/rs/zerolog"
13+
"github.com/sassoftware/relic/v8/lib/audit"
14+
"github.com/sassoftware/relic/v8/lib/certloader"
15+
"github.com/sassoftware/relic/v8/signers"
16+
"github.com/sassoftware/relic/v8/lib/atomicfile"
17+
"github.com/sassoftware/relic/v8/lib/binpatch"
18+
19+
)
20+
21+
22+
var BlobSigner = &signers.Signer{
23+
Name: "blob",
24+
CertTypes: signers.CertTypePgp,
25+
AllowStdin: true,
26+
Transform: transform,
27+
Sign: sign,
28+
Verify: verify,
29+
}
30+
31+
func init() {
32+
signers.Register(BlobSigner)
33+
}
34+
35+
func formatLog(attrs *audit.Info) *zerolog.Event {
36+
return attrs.AttrsForLog("blob.")
37+
}
38+
39+
func sign(r io.Reader, cert *certloader.Certificate, opts signers.SignOpts) ([]byte, error) {
40+
privKey := cert.PrivateKey.(crypto.Signer)
41+
hash := opts.Hash.New()
42+
if _, err := io.Copy(hash, r); err != nil {
43+
return nil, err
44+
}
45+
digest := hash.Sum(nil)
46+
return privKey.Sign(rand.Reader, digest, opts.Hash)
47+
}
48+
49+
func verify(f *os.File, opts signers.VerifyOpts) ([]*signers.Signature, error) {
50+
return nil, errors.New("Not implemented")
51+
}
52+
53+
type blobTransformer struct {
54+
input *os.File
55+
}
56+
57+
func transform(f *os.File, opts signers.SignOpts) (signers.Transformer, error) {
58+
//save input filename for later use
59+
return &blobTransformer{f}, nil
60+
}
61+
62+
func (t blobTransformer) GetReader() (io.Reader, error) {
63+
if _, err := t.input.Seek(0, io.SeekStart); err != nil {
64+
return nil, fmt.Errorf("seeking input file: %w", err)
65+
}
66+
return t.input, nil
67+
}
68+
69+
func (t *blobTransformer) Apply(dest, mimeType string, result io.Reader) error {
70+
var output string
71+
// avoid mangling input file
72+
if t.input.Name() == dest {
73+
output = "-"
74+
} else {
75+
output = dest
76+
}
77+
78+
// copy from default implementation in signers/transform
79+
if mimeType == binpatch.MimeType {
80+
return signers.ApplyBinPatch(t.input, dest, result)
81+
}
82+
f, err := atomicfile.WriteAny(output)
83+
if err != nil {
84+
return err
85+
}
86+
if _, err := io.Copy(f, result); err != nil {
87+
return err
88+
}
89+
t.input.Close()
90+
return f.Commit()
91+
}
92+

0 commit comments

Comments
 (0)