Commit 9122f964 authored by Janos Guljas's avatar Janos Guljas

add comments to crypto package

parent b7b3d087
...@@ -16,17 +16,28 @@ import ( ...@@ -16,17 +16,28 @@ import (
"golang.org/x/crypto/sha3" "golang.org/x/crypto/sha3"
) )
var keyTypeSecp256k1 = "Secp256k1" var keyTypeSecp256k1 = "secp256k1"
// GenerateSecp256k1Key generates an ECDSA private key using
// secp256k1 elliptic curve.
func GenerateSecp256k1Key() (*ecdsa.PrivateKey, error) { func GenerateSecp256k1Key() (*ecdsa.PrivateKey, error) {
return ecdsa.GenerateKey(btcec.S256(), rand.Reader) return ecdsa.GenerateKey(btcec.S256(), rand.Reader)
} }
// NewAddress constructs a Swarm Address from ECDSA private key.
func NewAddress(p ecdsa.PublicKey) swarm.Address {
d := elliptic.Marshal(btcec.S256(), p.X, p.Y)
return swarm.NewAddress(keccak256(d))
}
// privateKey holds information about Swarm private key for mashaling.
type privateKey struct { type privateKey struct {
Type string `json:"type"` Type string `json:"type"`
Key []byte `json:"key"` Key []byte `json:"key"`
} }
// MarshalSecp256k1PrivateKey marshals secp256k1 ECDSA private key
// that can be unmarshaled by UnmarshalPrivateKey.
func MarshalSecp256k1PrivateKey(k *ecdsa.PrivateKey) ([]byte, error) { func MarshalSecp256k1PrivateKey(k *ecdsa.PrivateKey) ([]byte, error) {
return json.Marshal(privateKey{ return json.Marshal(privateKey{
Type: keyTypeSecp256k1, Type: keyTypeSecp256k1,
...@@ -34,6 +45,7 @@ func MarshalSecp256k1PrivateKey(k *ecdsa.PrivateKey) ([]byte, error) { ...@@ -34,6 +45,7 @@ func MarshalSecp256k1PrivateKey(k *ecdsa.PrivateKey) ([]byte, error) {
}) })
} }
// UnmarshalPrivateKey unmarshals ECDSA private key from encoded data.
func UnmarshalPrivateKey(data []byte) (*ecdsa.PrivateKey, error) { func UnmarshalPrivateKey(data []byte) (*ecdsa.PrivateKey, error) {
var pk privateKey var pk privateKey
if err := json.Unmarshal(data, &pk); err != nil { if err := json.Unmarshal(data, &pk); err != nil {
...@@ -47,6 +59,7 @@ func UnmarshalPrivateKey(data []byte) (*ecdsa.PrivateKey, error) { ...@@ -47,6 +59,7 @@ func UnmarshalPrivateKey(data []byte) (*ecdsa.PrivateKey, error) {
} }
} }
// decodeSecp256k1PrivateKey decodes raw ECDSA private key.
func decodeSecp256k1PrivateKey(data []byte) (*ecdsa.PrivateKey, error) { func decodeSecp256k1PrivateKey(data []byte) (*ecdsa.PrivateKey, error) {
if l := len(data); l != btcec.PrivKeyBytesLen { if l := len(data); l != btcec.PrivKeyBytesLen {
return nil, fmt.Errorf("secp256k1 data size %d expected %d", l, btcec.PrivKeyBytesLen) return nil, fmt.Errorf("secp256k1 data size %d expected %d", l, btcec.PrivKeyBytesLen)
...@@ -55,11 +68,7 @@ func decodeSecp256k1PrivateKey(data []byte) (*ecdsa.PrivateKey, error) { ...@@ -55,11 +68,7 @@ func decodeSecp256k1PrivateKey(data []byte) (*ecdsa.PrivateKey, error) {
return (*ecdsa.PrivateKey)(privk), nil return (*ecdsa.PrivateKey)(privk), nil
} }
func NewAddress(p ecdsa.PublicKey) swarm.Address { // keccak256 calculates a hash sum from provided data.
d := elliptic.Marshal(btcec.S256(), p.X, p.Y)
return swarm.NewAddress(keccak256(d))
}
func keccak256(data ...[]byte) []byte { func keccak256(data ...[]byte) []byte {
d := sha3.New256() d := sha3.New256()
for _, b := range data { for _, b := range data {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment