nodejs:
const crypto = require(‘crypto’)
const fs = require(‘fs’)
var privateKey = fs.readFileSync(‘private.pem’).toString(‘utf8’)
const sign = crypto.createSign(‘RSA-SHA256’); // RSA-SHA256和SHA256结果一样
sign.update(“test”, ‘acsii’)
var signature = sign.sign(privateKey, ‘hex’)
console.log(signature)
输出:
279d0ec2405e29db7024217dd19bc939b0d149a69335f52943ec42f0b40668a38b6d7d0661b71552
d61bf6a541680e4e542fa18e0bcbb860a7f57ec0fb89c3ea8e5cdce2bf6b8c71f0c1462a9cf228e9
eb85b00360201ac0c512a1d701c4bacf2bfc6a256e2e6451a88fb0aad849cf44e0fb947c5e315c52
89ef09eebee5e05
C ++:
int main(int argc, char **argv){
ERR_load_ERR_strings();
ERR_load_RSA_strings();
ERR_load_PEM_strings();
ERR_load_BIO_strings();
string strPemFileName="private.pem";
FILE* hPriKeyFile = fopen(strPemFileName.c_str(), "rb");
if( hPriKeyFile == NULL )
{
assert(false);
return 1;
}
RSA* pRSAPriKey = RSA_new();
if(PEM_read_RSAPrivateKey(hPriKeyFile, &pRSAPriKey, 0, 0) == NULL)
{
assert(false);
return 1;
}
unsigned int sig_len = 0;
unsigned int len = 1024;
char * out = (char *)malloc(len);
memset(out, 0, len);
string context="test";
unsigned char md[SHA256_DIGEST_LENGTH] = {0};
if(1 != RSA_sign(NID_sha256,(unsigned char*)context.c_str(), context.size(), (unsigned char*)out, &sig_len,pRSAPriKey))
{
char szErrMsg[LEN_1024] = { 0 };
ERR_error_string_n(ERR_get_error(), szErrMsg, LEN_1024);
cout << szErrMsg << endl;
}
print_hex((unsigned char *)out, sig_len);
free(out);
return 0;
}
输出:
3446ed9d60bde475f6f2844c5c98fcedf08578617243ab378231db574b1685f2f82ce50da36263dc
cf6ec9152160dc66c5e0d3a7cfc5de311300cecb29c1b0135b876e024ef13a50d345c4d37bcf53e1
47a235e72cc0a40d160e37eb1abbf4e18deb132e68f5155b324b7ad95daa38c44dd607e0b1d7d1c1
370d6cceebe7ad1e