· 7 years ago · Jul 10, 2018, 09:16 PM
1package encryption
2
3
4import (
5 "github.com/tyler-smith/go-bip39"
6 "github.com/tyler-smith/go-bip32"
7 "crypto/ecdsa"
8 "crypto/elliptic"
9 "golang.org/x/crypto/nacl/secretbox"
10 "crypto/rand"
11 "encoding/hex"
12 "io"
13 "log"
14 )
15
16
17type Encryption interface {
18 GenerateMnemonic() ( *bip32.Key, *bip32.Key)
19}
20
21type Assymetric struct{
22 RootPrivateKey *bip32.Key
23 RootPublicKeyKey *bip32.Key
24 RootMnemonic string
25 RootPassphrase string
26}
27
28func (c *Assymetric) GenerateMnemonic() (*bip32.Key, *bip32.Key){
29
30 entropy, _ := bip39.NewEntropy(256)
31 mnemonic, _ := bip39.NewMnemonic(entropy)
32
33 seed := bip39.NewSeed(mnemonic, c.RootPassphrase)
34
35 rootPrivateKey, _ := bip32.NewMasterKey(seed)
36 rootPublicKey := rootPrivateKey.PublicKey()
37
38 // Display mnemonic and keys
39 //c.RootMnemonic = mnemonic
40 c.RootPrivateKey = rootPrivateKey
41 c.RootPublicKeyKey = rootPublicKey
42
43
44 key, _ := rootPrivateKey.NewChildKey(0)
45 log.Printf("Private key 0 is %s", key)
46 log.Printf("Public key 0 is %s", key.PublicKey())
47
48
49 // You must use a different nonce for each message you encrypt with the
50 // same key. Since the nonce here is 192 bits long, a random value
51 // provides a sufficiently small probability of repeats.
52
53 log.Printf("This is the secret key %s", secretKey)
54
55 var nonce [24]byte
56 if _, err := io.ReadFull(rand.Reader, nonce[:]); err != nil {
57 panic(err)
58 }
59
60 log.Printf("This is the nonce %s", nonce)
61
62 //private_key_bytes, _ := x509.MarshalECPrivateKey(key)
63 //log.Println(private_key_bytes)
64
65
66 private_key, _ := ecdsa.GenerateKey(elliptic.P224(), rand.Reader)
67 private_key_bytes, _ := x509.MarshalECPrivateKey(private_key)
68 log.Println(private_key_bytes)
69
70
71 encrypted := secretbox.Seal(nonce[:], []byte("hello world"), &nonce, &secretKey)
72
73 log.Printf("This is the encrypted text %s", encrypted)
74 return rootPrivateKey, rootPublicKey