diff options
author | obscuren <geffobscura@gmail.com> | 2015-01-20 22:04:30 +0800 |
---|---|---|
committer | obscuren <geffobscura@gmail.com> | 2015-01-20 22:04:30 +0800 |
commit | 4dd7be7ed016d09811be91acae233d1c1755ccc1 (patch) | |
tree | 42e3ac05eb0162d08ee5a64851a3b0ffc825406d /crypto/key.go | |
parent | f34838688ebd1c775a3b700824e0c6c2e98e5018 (diff) | |
parent | 12fad65991d319f5740d24b6b6a62f6c758c0e9a (diff) | |
download | dexon-4dd7be7ed016d09811be91acae233d1c1755ccc1.tar dexon-4dd7be7ed016d09811be91acae233d1c1755ccc1.tar.gz dexon-4dd7be7ed016d09811be91acae233d1c1755ccc1.tar.bz2 dexon-4dd7be7ed016d09811be91acae233d1c1755ccc1.tar.lz dexon-4dd7be7ed016d09811be91acae233d1c1755ccc1.tar.xz dexon-4dd7be7ed016d09811be91acae233d1c1755ccc1.tar.zst dexon-4dd7be7ed016d09811be91acae233d1c1755ccc1.zip |
Merge branch 'develop' of github.com-obscure:ethereum/go-ethereum into develop
Diffstat (limited to 'crypto/key.go')
-rw-r--r-- | crypto/key.go | 107 |
1 files changed, 107 insertions, 0 deletions
diff --git a/crypto/key.go b/crypto/key.go new file mode 100644 index 000000000..d371ad4dc --- /dev/null +++ b/crypto/key.go @@ -0,0 +1,107 @@ +/* + This file is part of go-ethereum + + go-ethereum is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + go-ethereum is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with go-ethereum. If not, see <http://www.gnu.org/licenses/>. +*/ +/** + * @authors + * Gustav Simonsson <gustav.simonsson@gmail.com> + * @date 2015 + * + */ + +package crypto + +import ( + "bytes" + "code.google.com/p/go-uuid/uuid" + "crypto/ecdsa" + "crypto/elliptic" + "encoding/json" + "io" +) + +type Key struct { + Id *uuid.UUID // Version 4 "random" for unique id not derived from key data + // we only store privkey as pubkey/address can be derived from it + // privkey in this struct is always in plaintext + PrivateKey *ecdsa.PrivateKey +} + +type plainKeyJSON struct { + Id []byte + PrivateKey []byte +} + +type cipherJSON struct { + Salt []byte + IV []byte + CipherText []byte +} + +type encryptedKeyJSON struct { + Id []byte + Crypto cipherJSON +} + +func (k *Key) Address() []byte { + pubBytes := FromECDSAPub(&k.PrivateKey.PublicKey) + return Sha3(pubBytes)[12:] +} + +func (k *Key) MarshalJSON() (j []byte, err error) { + jStruct := plainKeyJSON{ + *k.Id, + FromECDSA(k.PrivateKey), + } + j, err = json.Marshal(jStruct) + return j, err +} + +func (k *Key) UnmarshalJSON(j []byte) (err error) { + keyJSON := new(plainKeyJSON) + err = json.Unmarshal(j, &keyJSON) + if err != nil { + return err + } + + u := new(uuid.UUID) + *u = keyJSON.Id + k.Id = u + + k.PrivateKey = ToECDSA(keyJSON.PrivateKey) + + return err +} + +func NewKey(rand io.Reader) *Key { + randBytes := make([]byte, 32) + _, err := rand.Read(randBytes) + if err != nil { + panic("key generation: could not read from random source: " + err.Error()) + } + reader := bytes.NewReader(randBytes) + _, x, y, err := elliptic.GenerateKey(S256(), reader) + if err != nil { + panic("key generation: elliptic.GenerateKey failed: " + err.Error()) + } + privateKeyMarshalled := elliptic.Marshal(S256(), x, y) + privateKeyECDSA := ToECDSA(privateKeyMarshalled) + + key := new(Key) + id := uuid.NewRandom() + key.Id = &id + key.PrivateKey = privateKeyECDSA + return key +} |