Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
M
mybee
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
vicotor
mybee
Commits
9122f964
Commit
9122f964
authored
Jan 29, 2020
by
Janos Guljas
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add comments to crypto package
parent
b7b3d087
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
15 additions
and
6 deletions
+15
-6
crypto.go
pkg/crypto/crypto.go
+15
-6
No files found.
pkg/crypto/crypto.go
View file @
9122f964
...
...
@@ -16,17 +16,28 @@ import (
"golang.org/x/crypto/sha3"
)
var
keyTypeSecp256k1
=
"
S
ecp256k1"
var
keyTypeSecp256k1
=
"
s
ecp256k1"
// GenerateSecp256k1Key generates an ECDSA private key using
// secp256k1 elliptic curve.
func
GenerateSecp256k1Key
()
(
*
ecdsa
.
PrivateKey
,
error
)
{
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
string
`json:"type"`
Key
[]
byte
`json:"key"`
}
// MarshalSecp256k1PrivateKey marshals secp256k1 ECDSA private key
// that can be unmarshaled by UnmarshalPrivateKey.
func
MarshalSecp256k1PrivateKey
(
k
*
ecdsa
.
PrivateKey
)
([]
byte
,
error
)
{
return
json
.
Marshal
(
privateKey
{
Type
:
keyTypeSecp256k1
,
...
...
@@ -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
)
{
var
pk
privateKey
if
err
:=
json
.
Unmarshal
(
data
,
&
pk
);
err
!=
nil
{
...
...
@@ -47,6 +59,7 @@ func UnmarshalPrivateKey(data []byte) (*ecdsa.PrivateKey, error) {
}
}
// decodeSecp256k1PrivateKey decodes raw ECDSA private key.
func
decodeSecp256k1PrivateKey
(
data
[]
byte
)
(
*
ecdsa
.
PrivateKey
,
error
)
{
if
l
:=
len
(
data
);
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) {
return
(
*
ecdsa
.
PrivateKey
)(
privk
),
nil
}
func
NewAddress
(
p
ecdsa
.
PublicKey
)
swarm
.
Address
{
d
:=
elliptic
.
Marshal
(
btcec
.
S256
(),
p
.
X
,
p
.
Y
)
return
swarm
.
NewAddress
(
keccak256
(
d
))
}
// keccak256 calculates a hash sum from provided data.
func
keccak256
(
data
...
[]
byte
)
[]
byte
{
d
:=
sha3
.
New256
()
for
_
,
b
:=
range
data
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment