以太坊中使用的椭圆曲线是S256,私钥和HASH都为32位,公钥为65位其中第一位是压缩字节0x04,压缩公钥为33字节,地址是是公钥的后64位hash后取后20个字节作为地址。签名数据为65位,R,S各32位,65位为0和1.
生成私钥
//生成私钥
key, err := crypto.GenerateKey()
if err != nil {
t.Fatalf("failed GenerateKey with %s.", err)
}
//不含0x的私钥
fmt.Println("private key no 0x \n", hex.EncodeToString(crypto.FromECDSA(key)))
private key no 0x
b1fb9a42d8478cf19bbc1cb4e75625ced1728c8de8691845f546b2ad84a7d379
私钥存储
//本地生成privatekey文件,保存私钥
if err := crypto.SaveECDSA("privatekey", key); err != nil {
log.Error(fmt.Sprintf("Failed to persist node key: %v", err))
}
b1fb9a42d8478cf19bbc1cb4e75625ced1728c8de8691845f546b2ad84a7d379
公钥16进制打印
fmt.Println("public key no 0x \n", hex.EncodeToString(crypto.FromECDSAPub(&key.PublicKey)))
public key no 0x
0425b775a01b5df335cd71170f6a16d8b43704e68b8eb87a8e6ebfd3deafbfc1151d76bbe078002ffb7caaca06441b1c3976c3ca3b1e1fda9cf0f4591d799758e4
字符串转私钥和地址
//由私钥字符串转换私钥和地址
//由私钥字符串转换私钥
acc1Key, _ := crypto.HexToE