在实际工作中,要对某些数据进行SHA256的加密,下面,将通过python和Qt两种方式实现,特别要主要入参的顺序,不能改变,否则加密的数据不对
python:
from hashlib import sha256
import hmac
def get_sign(secretkey, data):
_key = secretkey.encode('utf-8')
_data = data.encode('utf-8')
#这里需要注意,_key和_data两个入参顺序不能改变,否则加密出来的数据不对
#hexdigest表示加密字符串转化成16进制
sign = hmac.new(_key,_data, digestmod=sha256).hexdigest()
return sign
Qt(C++):
QString MakeSign(const QString& strSecretkey,const QString& strData)
{
QByteArray strKey = strSecretkey.toUtf8();
QByteArray strData = strData.toUtf8();
//QByteArray QMessageAuthenticationCode::hash(const QByteArray &message, const QByteArray &key, QCryptographicHash::Algorithm method)
return QMessageAuthenticationCode::hash(strData, strKey, QCryptographicHash::Sha256).toHex();
}