storj/pkg/auth/signing/peers.go

71 lines
2.1 KiB
Go
Raw Normal View History

// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information.
package signing
import (
"context"
"crypto"
monkit "gopkg.in/spacemonkeygo/monkit.v2"
"storj.io/storj/pkg/identity"
"storj.io/storj/pkg/pkcrypto"
"storj.io/storj/pkg/storj"
)
var mon = monkit.Package()
// PrivateKey implements a signer and signee using a crypto.PrivateKey.
type PrivateKey struct {
Self storj.NodeID
Key crypto.PrivateKey
}
// SignerFromFullIdentity returns signer based on full identity.
func SignerFromFullIdentity(identity *identity.FullIdentity) Signer {
return &PrivateKey{
Self: identity.ID,
Key: identity.Key,
}
}
// ID returns node id associated with PrivateKey.
func (private *PrivateKey) ID() storj.NodeID { return private.Self }
// HashAndSign hashes the data and signs with the used key.
func (private *PrivateKey) HashAndSign(ctx context.Context, data []byte) (_ []byte, err error) {
defer mon.Task()(&ctx)(&err)
return pkcrypto.HashAndSign(private.Key, data)
}
// HashAndVerifySignature hashes the data and verifies that the signature belongs to the PrivateKey.
func (private *PrivateKey) HashAndVerifySignature(ctx context.Context, data, signature []byte) (err error) {
defer mon.Task()(&ctx)(&err)
pub := pkcrypto.PublicKeyFromPrivate(private.Key)
return pkcrypto.HashAndVerifySignature(pub, data, signature)
}
// PublicKey implements a signee using crypto.PublicKey.
type PublicKey struct {
Self storj.NodeID
Key crypto.PublicKey
}
// SigneeFromPeerIdentity returns signee based on peer identity.
func SigneeFromPeerIdentity(identity *identity.PeerIdentity) Signee {
return &PublicKey{
Self: identity.ID,
Key: identity.Leaf.PublicKey,
}
}
// ID returns node id associated with this PublicKey.
func (public *PublicKey) ID() storj.NodeID { return public.Self }
// HashAndVerifySignature hashes the data and verifies that the signature belongs to the PublicKey.
func (public *PublicKey) HashAndVerifySignature(ctx context.Context, data, signature []byte) (err error) {
defer mon.Task()(&ctx)(&err)
return pkcrypto.HashAndVerifySignature(public.Key, data, signature)
}