Skip to content

Commit

Permalink
Avoid creating a new signer on every Sign call in signer_factory.go
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 696547312
Change-Id: I260aab4acfb88b25aa1dea133d39c5fdff31c86d
  • Loading branch information
morambro authored and copybara-github committed Nov 14, 2024
1 parent 560d705 commit 89f8f51
Showing 1 changed file with 11 additions and 11 deletions.
22 changes: 11 additions & 11 deletions signature/signer_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@ func NewSigner(handle *keyset.Handle) (tink.Signer, error) {

// wrappedSigner is an Signer implementation that uses the underlying primitive set for signing.
type wrappedSigner struct {
ps *primitiveset.PrimitiveSet
logger monitoring.Logger
signer tink.Signer
signerKeyID uint32
logger monitoring.Logger
}

// Asserts that wrappedSigner implements the Signer interface.
Expand Down Expand Up @@ -90,9 +91,11 @@ func extractFullSigner(entry *primitiveset.Entry) (tink.Signer, error) {
}

func newWrappedSigner(ps *primitiveset.PrimitiveSet) (*wrappedSigner, error) {
if _, err := extractFullSigner(ps.Primary); err != nil {
signer, err := extractFullSigner(ps.Primary)
if err != nil {
return nil, err
}
// Validate that all entries are tink.Signer.
for _, entries := range ps.Entries {
for _, entry := range entries {
if _, err := extractFullSigner(entry); err != nil {
Expand All @@ -105,8 +108,9 @@ func newWrappedSigner(ps *primitiveset.PrimitiveSet) (*wrappedSigner, error) {
return nil, err
}
return &wrappedSigner{
ps: ps,
logger: logger,
signer: signer,
signerKeyID: ps.Primary.KeyID,
logger: logger,
}, nil
}

Expand All @@ -128,15 +132,11 @@ func createSignerLogger(ps *primitiveset.PrimitiveSet) (monitoring.Logger, error

// Sign signs the given data using the primary key.
func (s *wrappedSigner) Sign(data []byte) ([]byte, error) {
signer, err := extractFullSigner(s.ps.Primary)
if err != nil {
return nil, err
}
signature, err := signer.Sign(data)
signature, err := s.signer.Sign(data)
if err != nil {
s.logger.LogFailure()
return nil, err
}
s.logger.Log(s.ps.Primary.KeyID, len(data))
s.logger.Log(s.signerKeyID, len(data))
return signature, nil
}

0 comments on commit 89f8f51

Please sign in to comment.