2020-04-29 08:52:05 +00:00
|
|
|
package session
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/ecdsa"
|
2020-04-29 09:46:05 +00:00
|
|
|
"crypto/elliptic"
|
2020-04-29 08:52:05 +00:00
|
|
|
"crypto/rand"
|
|
|
|
|
|
|
|
crypto "github.com/nspcc-dev/neofs-crypto"
|
|
|
|
)
|
|
|
|
|
|
|
|
type pToken struct {
|
|
|
|
// private session token
|
|
|
|
sessionKey *ecdsa.PrivateKey
|
|
|
|
}
|
|
|
|
|
2020-04-29 09:59:58 +00:00
|
|
|
// NewPrivateToken creates PrivateToken instance.
|
2020-04-29 08:52:05 +00:00
|
|
|
//
|
|
|
|
// Returns non-nil error on key generation error.
|
|
|
|
func NewPrivateToken() (PrivateToken, error) {
|
2020-04-29 09:46:05 +00:00
|
|
|
sk, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
|
2020-04-29 08:52:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &pToken{
|
|
|
|
sessionKey: sk,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sign signs data with session private key.
|
|
|
|
func (t *pToken) Sign(data []byte) ([]byte, error) {
|
|
|
|
return crypto.Sign(t.sessionKey, data)
|
|
|
|
}
|
|
|
|
|
|
|
|
// PublicKey returns a binary representation of the session public key.
|
|
|
|
func (t *pToken) PublicKey() []byte {
|
|
|
|
return crypto.MarshalPublicKey(&t.sessionKey.PublicKey)
|
|
|
|
}
|