forked from TrueCloudLab/frostfs-api-go
[#3] signature: Add buffer pool
Signed-off-by: Dmitrii Stepanov <d.stepanov@yadro.com>
This commit is contained in:
parent
73fde0e37c
commit
ec0d0274fa
5 changed files with 67 additions and 58 deletions
|
@ -13,7 +13,6 @@ import (
|
|||
type cfg struct {
|
||||
schemeFixed bool
|
||||
scheme refs.SignatureScheme
|
||||
buffer []byte
|
||||
}
|
||||
|
||||
func defaultCfg() *cfg {
|
||||
|
@ -36,9 +35,10 @@ func verify(cfg *cfg, data []byte, sig *refs.Signature) error {
|
|||
case refs.ECDSA_RFC6979_SHA256:
|
||||
return crypto.VerifyRFC6979(pub, data, sig.GetSign())
|
||||
case refs.ECDSA_RFC6979_SHA256_WALLET_CONNECT:
|
||||
buf := make([]byte, base64.StdEncoding.EncodedLen(len(data)))
|
||||
base64.StdEncoding.Encode(buf, data)
|
||||
if !walletconnect.Verify(pub, buf, sig.GetSign()) {
|
||||
buffer := newBufferFromPool(base64.StdEncoding.EncodedLen(len(data)))
|
||||
defer returnBufferToPool(buffer)
|
||||
base64.StdEncoding.Encode(buffer, data)
|
||||
if !walletconnect.Verify(pub, buffer, sig.GetSign()) {
|
||||
return crypto.ErrInvalidSignature
|
||||
}
|
||||
return nil
|
||||
|
@ -54,9 +54,10 @@ func sign(cfg *cfg, key *ecdsa.PrivateKey, data []byte) ([]byte, error) {
|
|||
case refs.ECDSA_RFC6979_SHA256:
|
||||
return crypto.SignRFC6979(key, data)
|
||||
case refs.ECDSA_RFC6979_SHA256_WALLET_CONNECT:
|
||||
buf := make([]byte, base64.StdEncoding.EncodedLen(len(data)))
|
||||
base64.StdEncoding.Encode(buf, data)
|
||||
return walletconnect.Sign(key, buf)
|
||||
buffer := newBufferFromPool(base64.StdEncoding.EncodedLen(len(data)))
|
||||
defer returnBufferToPool(buffer)
|
||||
base64.StdEncoding.Encode(buffer, data)
|
||||
return walletconnect.Sign(key, buffer)
|
||||
default:
|
||||
panic(fmt.Sprintf("unsupported scheme %s", cfg.scheme))
|
||||
}
|
||||
|
@ -69,13 +70,6 @@ func SignWithRFC6979() SignOption {
|
|||
}
|
||||
}
|
||||
|
||||
// WithBuffer allows providing pre-allocated buffer for signature verification.
|
||||
func WithBuffer(buf []byte) SignOption {
|
||||
return func(c *cfg) {
|
||||
c.buffer = buf
|
||||
}
|
||||
}
|
||||
|
||||
func SignWithWalletConnect() SignOption {
|
||||
return func(c *cfg) {
|
||||
c.scheme = refs.ECDSA_RFC6979_SHA256_WALLET_CONNECT
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue