Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
G
Geth-Modification
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
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
张蕾
Geth-Modification
Commits
0c7df373
Commit
0c7df373
authored
Feb 10, 2015
by
Felix Lange
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
crypto: add key loading functions
parent
f1ebad25
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
30 additions
and
1 deletion
+30
-1
crypto.go
crypto/crypto.go
+28
-0
key.go
crypto/key.go
+2
-1
No files found.
crypto/crypto.go
View file @
0c7df373
...
@@ -8,6 +8,8 @@ import (
...
@@ -8,6 +8,8 @@ import (
"crypto/rand"
"crypto/rand"
"crypto/sha256"
"crypto/sha256"
"fmt"
"fmt"
"io"
"os"
"encoding/hex"
"encoding/hex"
"encoding/json"
"encoding/json"
...
@@ -99,6 +101,32 @@ func FromECDSAPub(pub *ecdsa.PublicKey) []byte {
...
@@ -99,6 +101,32 @@ func FromECDSAPub(pub *ecdsa.PublicKey) []byte {
return
elliptic
.
Marshal
(
S256
(),
pub
.
X
,
pub
.
Y
)
return
elliptic
.
Marshal
(
S256
(),
pub
.
X
,
pub
.
Y
)
}
}
// HexToECDSA parses a secp256k1 private key.
func
HexToECDSA
(
hexkey
string
)
(
*
ecdsa
.
PrivateKey
,
error
)
{
b
,
err
:=
hex
.
DecodeString
(
hexkey
)
if
err
!=
nil
{
return
nil
,
errors
.
New
(
"invalid hex string"
)
}
if
len
(
b
)
!=
32
{
return
nil
,
errors
.
New
(
"invalid length, need 256 bits"
)
}
return
ToECDSA
(
b
),
nil
}
// LoadECDSA loads a secp256k1 private key from the given file.
func
LoadECDSA
(
file
string
)
(
*
ecdsa
.
PrivateKey
,
error
)
{
buf
:=
make
([]
byte
,
32
)
fd
,
err
:=
os
.
Open
(
file
)
if
err
!=
nil
{
return
nil
,
err
}
defer
fd
.
Close
()
if
_
,
err
:=
io
.
ReadFull
(
fd
,
buf
);
err
!=
nil
{
return
nil
,
err
}
return
ToECDSA
(
buf
),
nil
}
func
GenerateKey
()
(
*
ecdsa
.
PrivateKey
,
error
)
{
func
GenerateKey
()
(
*
ecdsa
.
PrivateKey
,
error
)
{
return
ecdsa
.
GenerateKey
(
S256
(),
rand
.
Reader
)
return
ecdsa
.
GenerateKey
(
S256
(),
rand
.
Reader
)
}
}
...
...
crypto/key.go
View file @
0c7df373
...
@@ -25,11 +25,12 @@ package crypto
...
@@ -25,11 +25,12 @@ package crypto
import
(
import
(
"bytes"
"bytes"
"code.google.com/p/go-uuid/uuid"
"crypto/ecdsa"
"crypto/ecdsa"
"crypto/elliptic"
"crypto/elliptic"
"encoding/json"
"encoding/json"
"io"
"io"
"code.google.com/p/go-uuid/uuid"
)
)
type
Key
struct
{
type
Key
struct
{
...
...
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