概述
//MasterPrivateKey:
//466EE30723D2441F2F51F949794258FBEB768D0B9A3A4614AA01A889A5F0AFD9
//1. MasterPrivateKey >> WIFDecode:
QString Check(QString hexdata)
{
QString SHA2561 = SHA256(hexdata);
QString SHA2562 = SHA256(SHA2561);
return SHA2562.left(8);
}
QString WIFEncode(QString MasterPrivateKey)
{
QString WIFDecode0 = "80" + MasterPrivateKey ;
QString WIFDecode = WIFDecode0 + Check(WIFDecode0);
return WIFDecode;
}
//WIFDecode:
//80466EE30723D2441F2F51F949794258FBEB768D0B9A3A4614AA01A889A5F0AFD93850BB01
//2. WIFDecode >> WIF
QString EncodeBase58(QString data, int reverse = 0)
{
return QString::fromStdString(EncodeBase58(QString2vuchar(data)));
}
//WIF
//5JMJhfEYzfZcfSSyqhCHDGkq5nyeTkoP1cDmirHDpMLBbkzWG2t
//3.MasterPrivateKey >> MasterPublicKey
QString MainWindow::ComputePublickey(QString priv, size_t publen, uint flags)
{
secp256k1_pubkey pubkey;
uchar* seckey = QString2ucharx(priv);
int rc = secp256k1_ec_pubkey_create(ctx, &pubkey, seckey);
unsigned char pub[publen];
secp256k1_ec_pubkey_serialize(ctx, pub, &publen, &pubkey, flags);
QString MasterPublicKey = ucharx2QString(pub, publen);
return MasterPublicKey.toUpper();
}
//MasterPublicKey:
//04FA29F28C125BEB0DB774E3D81A17F761D19E655653665FB0DE04B55AA53118B986C3B8EEE009E123190D58D54CEAE8FEEE8FF785042B14695E23B10A05E984E8
//4.MasterPublicKey >> PubkeyCompress
QString MainWindow::PubkeyCompress(QString pubkeyUncompress)
{
secp256k1_pubkey pubkey;
int rc = secp256k1_ec_pubkey_parse(ctx, &pubkey, QString2ucharx(pubkeyUncompress),
pubkeyUncompress.size()/2);
size_t publen = 65;
unsigned char pub[publen];
secp256k1_ec_pubkey_serialize(ctx, pub, &publen, &pubkey, SECP256K1_EC_COMPRESSED);
QString MasterPublicKey = ucharx2QString(pub, publen);
return MasterPublicKey.toUpper();
}
//PubkeyCompress:
//02FA29F28C125BEB0DB774E3D81A17F761D19E655653665FB0DE04B55AA53118B9
//5.MasterPublicKey >> Address
QString PubKeyToAddress(QString data)
{
return QString::fromStdString(PubKeyToAddress(QString2vuchar(data)));
}
//Address:
//13rQo12gEpncFdmHgp4ZvVrzq1ixLPtrrr
//6.masterPrivateKey Sign
//msg:123
QString MainWindow::Sign(QString msg, QString masterPrivateKey)
{
unsigned char* msg32 = QString2ucharx(SHA256(msg));
secp256k1_ecdsa_signature sig;
uchar* seckey = QString2ucharx(masterPrivateKey);
int ret = secp256k1_ecdsa_sign(ctx, &sig, msg32, seckey, NULL, NULL);
#ifdef der
unsigned char der[200];
size_t derlen = 200;
ret = secp256k1_ecdsa_signature_serialize_der(ctx, der, &derlen, &sig);
#endif
unsigned char* sig64 = (uchar*)malloc(64*sizeof(uchar));
secp256k1_ecdsa_signature_serialize_compact(ctx, sig64, &sig);
return ucharx2QString(sig64, 64);
}
//Sig:
//032533f9258cabf98a3760c0584cffb0717593f87be136846e267ec2598b0a176b98903ee79af6e5673212a92fe11c08c2354f69229148eebfb9626b92e342b1
int MainWindow::Verify(QString signature, QString pubkeyUncompress, QString msg)
{
unsigned char *sig64 = QString2ucharx(signature);
secp256k1_ecdsa_signature sig;
secp256k1_ecdsa_signature_parse_compact(ctx, &sig, sig64);
unsigned char* msg32 = QString2ucharx(SHA256(msg));
secp256k1_pubkey pubkey;
int rc = secp256k1_ec_pubkey_parse(ctx, &pubkey, QString2ucharx(pubkeyUncompress),
pubkeyUncompress.size()/2);
int ret = secp256k1_ecdsa_verify(ctx, &sig, msg32, &pubkey);
return ret;
}
//true
QString SHA256qt(QString hexdata)
{
QCryptographicHash SHA256(QCryptographicHash::Sha256);
QByteArray Data = QByteArray::fromHex(hexdata.toLatin1());
SHA256.addData(Data);
return SHA256.result().toHex().toUpper();
}
QString SHA256(QString hexdata)
{
#ifdef opensslsha256
vector<unsigned char> vch(QString2vuchar(hexdata));
uint256 hash;
SHA256(&vch[0], vch.size(), (unsigned char*)&hash); //openssl
return ucharx2QString((unsigned char*)&hash, 32).toUpper();
#else
return SHA256qt(hexdata);
#endif
}
QString SHA256(QString hexdata, int rehash)
{
for (int i = 0; i < rehash; ++i)
{
hexdata = SHA256(hexdata);
}
return hexdata;
}
vector<unsigned char> QString2vuchar(QString str)
{
int num = str.size()/2;
vector<unsigned char> v;
for(int i = 0; i < num; i++)
{
v.push_back(str.midRef(i * 2, 2).toInt(nullptr, 16));
}
return v;
}
uchar* QString2ucharx(QString str)
{
int num = str.size()/2;
uchar* v = (uchar*)malloc(num*sizeof(uchar));
for(int i = 0; i<num; i++)
{
v[i] = str.midRef(i*2,2).toInt(nullptr,16);
}
return v;
}
INCLUDEPATH +=
/usr/local/openssl_0_9_8/include
LIBS +=
/usr/local/openssl_0_9_8/lib/libssl.a
/usr/local/openssl_0_9_8/lib/libcrypto.a
/usr/local/lib/libsecp256k1.a
#include "QCryptographicHash"
#include <secp256k1.h>
最后
以上就是温婉山水为你收集整理的secp256k1随机数计算私钥公钥和地址、ECDSA签名、验证的全部内容,希望文章能够帮你解决secp256k1随机数计算私钥公钥和地址、ECDSA签名、验证所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复