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
29
util/signature/buffer.go
Normal file
29
util/signature/buffer.go
Normal file
|
@ -0,0 +1,29 @@
|
|||
package signature
|
||||
|
||||
import "sync"
|
||||
|
||||
const poolSliceMaxSize = 64 * 1024
|
||||
|
||||
var buffersPool = sync.Pool{
|
||||
New: func() any {
|
||||
return make([]byte, 0)
|
||||
},
|
||||
}
|
||||
|
||||
func newBufferFromPool(size int) []byte {
|
||||
result := buffersPool.Get().([]byte)
|
||||
if cap(result) < size {
|
||||
result = make([]byte, size)
|
||||
} else {
|
||||
result = result[:size]
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func returnBufferToPool(buf []byte) {
|
||||
if cap(buf) > poolSliceMaxSize {
|
||||
return
|
||||
}
|
||||
buf = buf[:0]
|
||||
buffersPool.Put(buf)
|
||||
}
|
|
@ -35,7 +35,10 @@ func SignDataWithHandler(key *ecdsa.PrivateKey, src DataSource, handler KeySigna
|
|||
opts[i](cfg)
|
||||
}
|
||||
|
||||
data, err := readSignedData(cfg, src)
|
||||
buffer := newBufferFromPool(src.SignedDataSize())
|
||||
defer returnBufferToPool(buffer)
|
||||
|
||||
data, err := src.ReadSignedData(buffer)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -61,7 +64,10 @@ func VerifyDataWithSource(dataSrc DataSource, sigSrc KeySignatureSource, opts ..
|
|||
opts[i](cfg)
|
||||
}
|
||||
|
||||
data, err := readSignedData(cfg, dataSrc)
|
||||
buffer := newBufferFromPool(dataSrc.SignedDataSize())
|
||||
defer returnBufferToPool(buffer)
|
||||
|
||||
data, err := dataSrc.ReadSignedData(buffer)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -76,13 +82,3 @@ func SignData(key *ecdsa.PrivateKey, v DataWithSignature, opts ...SignOption) er
|
|||
func VerifyData(src DataWithSignature, opts ...SignOption) error {
|
||||
return VerifyDataWithSource(src, src.GetSignature, opts...)
|
||||
}
|
||||
|
||||
func readSignedData(cfg *cfg, src DataSource) ([]byte, error) {
|
||||
size := src.SignedDataSize()
|
||||
if cfg.buffer == nil || cap(cfg.buffer) < size {
|
||||
cfg.buffer = make([]byte, size)
|
||||
} else {
|
||||
cfg.buffer = cfg.buffer[:size]
|
||||
}
|
||||
return src.ReadSignedData(cfg.buffer)
|
||||
}
|
||||
|
|
|
@ -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