bitbucket.org/number571/tendermint@v0.8.14/crypto/gost256/gost256.go (about)

     1  package gost256
     2  
     3  import (
     4  	"bufio"
     5  	"bytes"
     6  	"fmt"
     7  	"os"
     8  	"strings"
     9  
    10  	"bitbucket.org/number571/tendermint/crypto"
    11  	"bitbucket.org/number571/tendermint/crypto/tmhash"
    12  	tmjson "bitbucket.org/number571/tendermint/libs/json"
    13  
    14  	gkeys "bitbucket.org/number571/go-cryptopro/gost_r_34_10_2012"
    15  )
    16  
    17  //-------------------------------------
    18  const (
    19  	PrivKeyName = "tendermint/PrivKey256"
    20  	PubKeyName  = "tendermint/PubKey256"
    21  
    22  	ProvType = "256"
    23  	KeyType  = gkeys.KeyType + " " + ProvType
    24  
    25  	PubKeySize  = gkeys.PubKeySize256
    26  	PrivKeySize = gkeys.PrivKeySize256
    27  )
    28  
    29  func init() {
    30  	tmjson.RegisterType(PubKey{}, PubKeyName)
    31  	tmjson.RegisterType(PrivKey{}, PrivKeyName)
    32  }
    33  
    34  var _ crypto.PrivKey = PrivKey{}
    35  
    36  type PrivKey gkeys.PrivKey256
    37  
    38  func (privKey PrivKey) Bytes() []byte {
    39  	return gkeys.PrivKey256(privKey).Bytes()
    40  }
    41  
    42  func (privKey PrivKey) PubKey() crypto.PubKey {
    43  	return PubKey(gkeys.PrivKey256(privKey).PubKey().(gkeys.PubKey256))
    44  }
    45  
    46  func (privKey PrivKey) Equals(other crypto.PrivKey) bool {
    47  	return bytes.Equal(gkeys.PrivKey256(privKey).Bytes(), other.Bytes())
    48  }
    49  
    50  func (privKey PrivKey) Type() string {
    51  	return gkeys.PrivKey256(privKey).Type()
    52  }
    53  
    54  func GenPrivKey() PrivKey {
    55  	fmt.Printf("Generating private key [%s]...\n", KeyType)
    56  	return GenPrivKeyWithInput(
    57  		inputString("Subject >>> "),
    58  		inputString("Password >>> "),
    59  	)
    60  }
    61  
    62  func GenPrivKeyWithInput(subject, password string) PrivKey {
    63  	cfg := gkeys.NewConfig(gkeys.K256, subject, password)
    64  	gkeys.GenPrivKey(cfg)
    65  
    66  	priv, err := gkeys.NewPrivKey(cfg)
    67  	if err != nil {
    68  		panic(err)
    69  	}
    70  
    71  	return PrivKey(priv.(gkeys.PrivKey256))
    72  }
    73  
    74  func inputString(begin string) string {
    75  	fmt.Print(begin)
    76  	data, err := bufio.NewReader(os.Stdin).ReadString('\n')
    77  	if err != nil {
    78  		panic(err)
    79  	}
    80  	return strings.TrimSpace(data)
    81  }
    82  
    83  //-------------------------------------
    84  
    85  var _ crypto.PubKey = PubKey{}
    86  
    87  type PubKey gkeys.PubKey256
    88  
    89  func (pubKey PubKey) Address() crypto.Address {
    90  	return crypto.Address(tmhash.SumTruncated(pubKey.Bytes()))
    91  }
    92  
    93  func (pubKey PubKey) Bytes() []byte {
    94  	return gkeys.PubKey256(pubKey).Bytes()
    95  }
    96  
    97  func (pubKey PubKey) String() string {
    98  	return gkeys.PubKey256(pubKey).String()
    99  }
   100  
   101  func (pubKey PubKey) Equals(other crypto.PubKey) bool {
   102  	return bytes.Equal(gkeys.PubKey256(pubKey).Bytes(), other.Bytes())
   103  }
   104  
   105  func (pubKey PubKey) Type() string {
   106  	return gkeys.PubKey256(pubKey).Type()
   107  }