Beispiel #1
0
func TestSigning(t *testing.T) {
	seckey, err := crypto.HexToECDSA("2934473d31f55a8a7c031bdef35b9587b40249969211aca5c29925cb04f84ccc")
	checkErr(t, err)

	t.Logf("Private key length: %v", len(crypto.FromECDSA(seckey)))
	t.Logf("private key: %x", crypto.FromECDSA(seckey))
	t.Logf("public key: %x", crypto.FromECDSAPub(&seckey.PublicKey))

	hdr, clm := jwsCompactParams(t)

	// Signed compact representation
	sm, err := Sign(seckey, hdr, clm, "foo")
	checkErr(t, err)

	_, err = sm.EncodeCompactJWS() // smc
	checkErr(t, err)

	r1, err := Base64Decode(sm.Signature)
	checkErr(t, err)

	t.Logf("Signature length == %v", len(r1))

	if len(r1) != 65 {
		t.Fatal("Invalid signature length")
	}

	res, err := sm.Verify(&seckey.PublicKey, "foo")
	checkErr(t, err)

	if err != nil || res == false {
		t.Fatal("Failed to recover a public key from signature")
	}
}
Beispiel #2
0
func (k *Key) MarshalJSON() (j []byte, err error) {
	jStruct := plainKeyJSON{
		hex.EncodeToString(k.Address[:]),
		hex.EncodeToString(crypto.FromECDSA(k.PrivateKey)),
		k.Id.String(),
		version,
	}
	j, err = json.Marshal(jStruct)
	return j, err
}
Beispiel #3
0
// tries to match the address with the string provided by the user, exits if successful
//
func addrMatch(addrStr string, toMatch string, key *ecdsa.PrivateKey) {
	toMatch = strings.ToLower(toMatch)
	addrStrMatch := strings.TrimPrefix(addrStr, toMatch)
	found := addrStrMatch != addrStr
	if found {
		// fmt.Println("pub:", hex.EncodeToString(crypto.FromECDSAPub(&key.PublicKey))) // uncomment if you want the public key
		keyStr := hex.EncodeToString(crypto.FromECDSA(key))
		addrFound(addrStr, keyStr)
		os.Exit(0) // here the program exits when it found a match
	}
}
Beispiel #4
0
func writeKey(target string) {
	key, err := crypto.GenerateKey()
	if err != nil {
		log.Fatal("could not generate key: %v", err)
	}
	b := crypto.FromECDSA(key)
	if target == "-" {
		fmt.Println(hex.EncodeToString(b))
	} else {
		if err := ioutil.WriteFile(target, b, 0600); err != nil {
			log.Fatal("write error: ", err)
		}
	}
}
Beispiel #5
0
func TestV1_2(t *testing.T) {
	t.Parallel()
	ks := &keyStorePassphrase{"testdata/v1", LightScryptN, LightScryptP}
	addr := common.HexToAddress("cb61d5a9c4896fb9658090b597ef0e7be6f7b67e")
	file := "testdata/v1/cb61d5a9c4896fb9658090b597ef0e7be6f7b67e/cb61d5a9c4896fb9658090b597ef0e7be6f7b67e"
	k, err := ks.GetKey(addr, file, "g")
	if err != nil {
		t.Fatal(err)
	}
	privHex := hex.EncodeToString(crypto.FromECDSA(k.PrivateKey))
	expectedHex := "d1b1178d3529626a1a93e073f65028370d14c7eb0936eb42abef05db6f37ad7d"
	if privHex != expectedHex {
		t.Fatal(fmt.Errorf("Unexpected privkey: %v, expected %v", privHex, expectedHex))
	}
}
// EncryptKey encrypts a key using the specified scrypt parameters into a json
// blob that can be decrypted later on.
func EncryptKey(key *Key, auth string, scryptN, scryptP int) ([]byte, error) {
	authArray := []byte(auth)
	salt := randentropy.GetEntropyCSPRNG(32)
	derivedKey, err := scrypt.Key(authArray, salt, scryptN, scryptR, scryptP, scryptDKLen)
	if err != nil {
		return nil, err
	}
	encryptKey := derivedKey[:16]
	keyBytes := crypto.FromECDSA(key.PrivateKey)

	iv := randentropy.GetEntropyCSPRNG(aes.BlockSize) // 16
	cipherText, err := aesCTRXOR(encryptKey, keyBytes, iv)
	if err != nil {
		return nil, err
	}
	mac := crypto.Keccak256(derivedKey[16:32], cipherText)

	scryptParamsJSON := make(map[string]interface{}, 5)
	scryptParamsJSON["n"] = scryptN
	scryptParamsJSON["r"] = scryptR
	scryptParamsJSON["p"] = scryptP
	scryptParamsJSON["dklen"] = scryptDKLen
	scryptParamsJSON["salt"] = hex.EncodeToString(salt)

	cipherParamsJSON := cipherparamsJSON{
		IV: hex.EncodeToString(iv),
	}

	cryptoStruct := cryptoJSON{
		Cipher:       "aes-128-ctr",
		CipherText:   hex.EncodeToString(cipherText),
		CipherParams: cipherParamsJSON,
		KDF:          "scrypt",
		KDFParams:    scryptParamsJSON,
		MAC:          hex.EncodeToString(mac),
	}
	encryptedKeyJSONV3 := encryptedKeyJSONV3{
		hex.EncodeToString(key.Address[:]),
		cryptoStruct,
		key.Id.String(),
		version,
	}
	return json.Marshal(encryptedKeyJSONV3)
}