github.com/number571/tendermint@v0.34.11-gost/crypto/gost512/gost512.go (about)

     1  package gost512
     2  
     3  import (
     4  	"bufio"
     5  	"bytes"
     6  	"fmt"
     7  	"os"
     8  	"strings"
     9  
    10  	gkeys "github.com/number571/go-cryptopro/gost_r_34_10_2012"
    11  
    12  	"github.com/number571/tendermint/crypto"
    13  	"github.com/number571/tendermint/crypto/tmhash"
    14  	tmjson "github.com/number571/tendermint/libs/json"
    15  )
    16  
    17  //-------------------------------------
    18  
    19  var (
    20  	_ crypto.PrivKey = PrivKey{}
    21  )
    22  
    23  const (
    24  	PrivKeyName = "tendermint/PrivKey512"
    25  	PubKeyName  = "tendermint/PubKey512"
    26  
    27  	PubKeySize     = gkeys.PubKeySize512
    28  	PrivateKeySize = gkeys.PrivKeySize512
    29  
    30  	SignatureSize = gkeys.SignatureSize512
    31  
    32  	ProvType = "512"
    33  	KeyType  = gkeys.KeyType + " " + ProvType
    34  )
    35  
    36  func init() {
    37  	tmjson.RegisterType(PubKey{}, PubKeyName)
    38  	tmjson.RegisterType(PrivKey{}, PrivKeyName)
    39  }
    40  
    41  type PrivKey gkeys.PrivKey512
    42  
    43  func (privKey PrivKey) Bytes() []byte {
    44  	return gkeys.PrivKey512(privKey).Bytes()
    45  }
    46  
    47  func (privKey PrivKey) Sign(msg []byte) ([]byte, error) {
    48  	return gkeys.PrivKey512(privKey).Sign(msg)
    49  }
    50  
    51  func (privKey PrivKey) PubKey() crypto.PubKey {
    52  	return PubKey(gkeys.PrivKey512(privKey).PubKey().(gkeys.PubKey512))
    53  }
    54  
    55  func (privKey PrivKey) Equals(other crypto.PrivKey) bool {
    56  	return bytes.Equal(gkeys.PrivKey512(privKey).Bytes(), other.Bytes())
    57  }
    58  
    59  func (privKey PrivKey) Type() string {
    60  	return gkeys.PrivKey512(privKey).Type()
    61  }
    62  
    63  func GenPrivKey() PrivKey {
    64  	fmt.Printf("Generating private key [%s]...\n", KeyType)
    65  	return GenPrivKeyWithInput(
    66  		inputString("Subject >>> "),
    67  		inputString("Password >>> "),
    68  	)
    69  }
    70  
    71  func GenPrivKeyWithInput(subject, password string) PrivKey {
    72  	cfg := gkeys.NewConfig(gkeys.K512, subject, password)
    73  	gkeys.GenPrivKey(cfg)
    74  
    75  	priv, err := gkeys.NewPrivKey(cfg)
    76  	if err != nil {
    77  		panic(err)
    78  	}
    79  
    80  	return PrivKey(priv.(gkeys.PrivKey512))
    81  }
    82  
    83  func inputString(begin string) string {
    84  	fmt.Print(begin)
    85  	data, err := bufio.NewReader(os.Stdin).ReadString('\n')
    86  	if err != nil {
    87  		panic(err)
    88  	}
    89  	return strings.TrimSpace(data)
    90  }
    91  
    92  //-------------------------------------
    93  
    94  var _ crypto.PubKey = PubKey{}
    95  
    96  type PubKey gkeys.PubKey512
    97  
    98  func (pubKey PubKey) Address() crypto.Address {
    99  	return crypto.Address(tmhash.SumTruncated(pubKey.Bytes()))
   100  }
   101  
   102  func (pubKey PubKey) Bytes() []byte {
   103  	return gkeys.PubKey512(pubKey).Bytes()
   104  }
   105  
   106  func (pubKey PubKey) VerifySignature(msg []byte, sig []byte) bool {
   107  	return gkeys.PubKey512(pubKey).VerifySignature(msg, sig)
   108  }
   109  
   110  func (pubKey PubKey) String() string {
   111  	return gkeys.PubKey512(pubKey).String()
   112  }
   113  
   114  func (pubKey PubKey) Type() string {
   115  	return gkeys.PubKey512(pubKey).Type()
   116  }
   117  
   118  func (pubKey PubKey) Equals(other crypto.PubKey) bool {
   119  	return bytes.Equal(gkeys.PubKey512(pubKey).Bytes(), other.Bytes())
   120  }
   121  
   122  //-------------------------------------
   123  
   124  var _ crypto.BatchVerifier = &BatchVerifier{}
   125  
   126  type BatchVerifier gkeys.BatchVerifierX
   127  
   128  func NewBatchVerifier() crypto.BatchVerifier {
   129  	return &BatchVerifier{}
   130  }
   131  
   132  func (b *BatchVerifier) Add(key crypto.PubKey, msg, signature []byte) error {
   133  	return (*gkeys.BatchVerifierX)(b).Add(gkeys.PubKey512(key.(PubKey)), msg, signature)
   134  }
   135  
   136  func (b *BatchVerifier) Verify() (bool, []bool) {
   137  	return (*gkeys.BatchVerifierX)(b).Verify()
   138  }