// Validates that public keys generated to random default are different. func TestPubKey(t *testing.T) { b1 := cipher.RandByte(33) pubKeyRandom1 := cipher.NewPubKey(b1) fmt.Fprintf(os.Stdout, "Public Key Random 1: %v \n", pubKeyRandom1) assert.True(t, bytes.Equal(pubKeyRandom1[:], b1)) b2 := cipher.RandByte(33) pubKeyRandom2 := cipher.NewPubKey(b2) fmt.Fprintf(os.Stdout, "Public Key Random 2: %v \n", pubKeyRandom2) assert.True(t, bytes.Equal(pubKeyRandom2[:], b2)) assert.False(t, bytes.Equal(pubKeyRandom1[:], pubKeyRandom2[:])) }
//check for collisions and retry if failure func NewWalletFilename() string { timestamp := time.Now().Format(WalletTimestampFormat) //should read in wallet files and make sure does not exist padding := hex.EncodeToString((cipher.RandByte(2))) fmt.Printf("padding= %s", padding) return fmt.Sprintf("%s_%s.%s", timestamp, padding, WalletExt) }
func Encrypt(r interface{}, pubkey string, seckey string) (data []byte, nonce []byte, err error) { defer func() { if r := recover(); r != nil { err = errors.New("encrypt faild") } }() d, err := json.Marshal(r) if err != nil { return } p := cipher.MustPubKeyFromHex(pubkey) s := cipher.MustSecKeyFromHex(seckey) nonce = cipher.RandByte(chacha20.NonceSize) key := cipher.ECDH(p, s) data, err = cipher.Chacha20Encrypt([]byte(d), key, nonce) return }
func main() { registerFlags() parseFlags() w := Wallet{ Meta: make(map[string]string), //map[string]string Entries: make([]KeyEntry, genCount), } if BitcoinAddress == false { w.Meta = map[string]string{"coin": "skycoin"} } else { w.Meta = map[string]string{"coin": "bitcoin"} } if seed == "" { //generate a new seed, as hex string seed = cipher.SumSHA256(cipher.RandByte(1024)).Hex() } w.Meta["seed"] = seed seckeys := cipher.GenerateDeterministicKeyPairs([]byte(seed), genCount) for i, sec := range seckeys { pub := cipher.PubKeyFromSecKey(sec) w.Entries[i] = getKeyEntry(pub, sec) } output, err := json.MarshalIndent(w, "", " ") if err != nil { fmt.Printf("Error formating wallet to JSON. Error : %s\n", err.Error()) return } fmt.Printf("%s\n", string(output)) }