aboutsummaryrefslogtreecommitdiffstats
path: root/crypto/key.go
diff options
context:
space:
mode:
authorGustav Simonsson <gustav.simonsson@gmail.com>2015-10-07 00:39:42 +0800
committerGustav Simonsson <gustav.simonsson@gmail.com>2015-10-13 23:44:14 +0800
commit2db97986460c57ba74a563d97a704a45a270df7d (patch)
treeca25e74a5436945c20bb0b95281235d43a2d2167 /crypto/key.go
parent44fd3951410fb5923d7e578ac97942a7ea791e96 (diff)
downloaddexon-2db97986460c57ba74a563d97a704a45a270df7d.tar
dexon-2db97986460c57ba74a563d97a704a45a270df7d.tar.gz
dexon-2db97986460c57ba74a563d97a704a45a270df7d.tar.bz2
dexon-2db97986460c57ba74a563d97a704a45a270df7d.tar.lz
dexon-2db97986460c57ba74a563d97a704a45a270df7d.tar.xz
dexon-2db97986460c57ba74a563d97a704a45a270df7d.tar.zst
dexon-2db97986460c57ba74a563d97a704a45a270df7d.zip
common, crypto: add ICAP functions
Diffstat (limited to 'crypto/key.go')
-rw-r--r--crypto/key.go22
1 files changed, 22 insertions, 0 deletions
diff --git a/crypto/key.go b/crypto/key.go
index 35139b67f..4ec43dfd7 100644
--- a/crypto/key.go
+++ b/crypto/key.go
@@ -22,6 +22,7 @@ import (
"encoding/hex"
"encoding/json"
"io"
+ "strings"
"github.com/ethereum/go-ethereum/common"
"github.com/pborman/uuid"
@@ -143,3 +144,24 @@ func NewKey(rand io.Reader) *Key {
return NewKeyFromECDSA(privateKeyECDSA)
}
+
+// generate key whose address fits into < 155 bits so it can fit into
+// the Direct ICAP spec. for simplicity and easier compatibility with
+// other libs, we retry until the first byte is 0.
+func NewKeyForDirectICAP(rand io.Reader) *Key {
+ randBytes := make([]byte, 64)
+ _, err := rand.Read(randBytes)
+ if err != nil {
+ panic("key generation: could not read from random source: " + err.Error())
+ }
+ reader := bytes.NewReader(randBytes)
+ privateKeyECDSA, err := ecdsa.GenerateKey(S256(), reader)
+ if err != nil {
+ panic("key generation: ecdsa.GenerateKey failed: " + err.Error())
+ }
+ key := NewKeyFromECDSA(privateKeyECDSA)
+ if !strings.HasPrefix(key.Address.Hex(), "0x00") {
+ return NewKeyForDirectICAP(rand)
+ }
+ return key
+}