aboutsummaryrefslogtreecommitdiffstats
path: root/crypto
diff options
context:
space:
mode:
authorFelix Lange <fjl@twurst.com>2015-02-10 19:29:50 +0800
committerFelix Lange <fjl@twurst.com>2015-02-10 19:29:50 +0800
commit0c7df37351ab85c577fe815d6d22f4627832b0c3 (patch)
tree67957f3b8321a4a16f748e34a02f40db6d32291d /crypto
parentf1ebad2508b6941df5802d607b30b7a5e7b2c67d (diff)
downloadgo-tangerine-0c7df37351ab85c577fe815d6d22f4627832b0c3.tar
go-tangerine-0c7df37351ab85c577fe815d6d22f4627832b0c3.tar.gz
go-tangerine-0c7df37351ab85c577fe815d6d22f4627832b0c3.tar.bz2
go-tangerine-0c7df37351ab85c577fe815d6d22f4627832b0c3.tar.lz
go-tangerine-0c7df37351ab85c577fe815d6d22f4627832b0c3.tar.xz
go-tangerine-0c7df37351ab85c577fe815d6d22f4627832b0c3.tar.zst
go-tangerine-0c7df37351ab85c577fe815d6d22f4627832b0c3.zip
crypto: add key loading functions
Diffstat (limited to 'crypto')
-rw-r--r--crypto/crypto.go28
-rw-r--r--crypto/key.go3
2 files changed, 30 insertions, 1 deletions
diff --git a/crypto/crypto.go b/crypto/crypto.go
index 42e6036b5..2c8f82977 100644
--- a/crypto/crypto.go
+++ b/crypto/crypto.go
@@ -8,6 +8,8 @@ import (
"crypto/rand"
"crypto/sha256"
"fmt"
+ "io"
+ "os"
"encoding/hex"
"encoding/json"
@@ -99,6 +101,32 @@ func FromECDSAPub(pub *ecdsa.PublicKey) []byte {
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) {
return ecdsa.GenerateKey(S256(), rand.Reader)
}
diff --git a/crypto/key.go b/crypto/key.go
index b9ad34f47..ec4908c30 100644
--- a/crypto/key.go
+++ b/crypto/key.go
@@ -25,11 +25,12 @@ package crypto
import (
"bytes"
- "code.google.com/p/go-uuid/uuid"
"crypto/ecdsa"
"crypto/elliptic"
"encoding/json"
"io"
+
+ "code.google.com/p/go-uuid/uuid"
)
type Key struct {