本文共 935 字,大约阅读时间需要 3 分钟。
QCryptographicHash类提供了生成密码散列的方法。该类可以用于生成二进制或文本数据的加密散列值。目前支持MD4、MD5、SHA-1、
SHA-224、SHA-256、SHA-384和SHA-512。
QString password_md5;
QByteArray ba_md5;
ba_md5 = QCryptographicHash::hash(password.toLocal8Bit(),QCryptographicHash::Md5);
password_md5.append(ba_md5.toHex().toUpper());
QJsonObject json;
json.insert("userId", username);
json.insert("password", password_md5);
QJsonDocument document;
document.setObject(json);
QByteArray byte_array = document.toJson(QJsonDocument::Compact);
QString json_str(byte_array);
通过静态hase()方法计算:
1
2
3
4QByteArray byteArray;
byteArray.append("password");
QByteArray hash = QCryptographicHash::hash(byteArray, QCryptographicHash::Md5);
QString strMD5 = hash.toHex();
通过result()方法计算:
QByteArray byteArray;
byteArray.append("password");
QCryptographicHash hash(QCryptographicHash::Md5);
hash.addData(byteArray); // 添加数据到加密哈希值
QByteArray result = hash.result(); // 返回最终的哈希值
QString strMD5 = result.toHex();
转载地址:http://lqexl.baihongyu.com/