#include <QCryptographicHash>
QByteArray hash2 = QCryptographicHash::hash(password.toLocal8Bit(), QCryptographicHash::Md5);
auto pwd = hash2.toHex().toStdString();
源代码
QByteArray QCryptographicHash::hash(const QByteArray &data, Algorithm method)
{
QCryptographicHash hash(method);
hash.addData(data);
return hash.result();
}
void QCryptographicHash::addData(const QByteArray &data)
{
addData(data.constData(), data.length());
}
void QCryptographicHash::addData(const char *data, int length)
{
switch (d->method) {
case Sha1:
sha1Update(&d->sha1Context, (const unsigned char *)data, length);
break;
#ifdef QT_CRYPTOGRAPHICHASH_ONLY_SHA1
default:
Q_ASSERT_X(false, "QCryptographicHash", "Method not compiled in");
Q_UNREACHABLE();
break;
#else
case Md4:
md4_update(&d->md4Context, (const unsigned char *)data, length);
break;
case Md5:
MD5Update(&d->md5Context, (const unsigned char *)data, length);
break;
case Sha224:
SHA224Input(&d->sha224Context, reinterpret_cast<const unsigned char *>(data), length);
break;
case Sha256:
SHA256Input(&d->sha256Context, reinterpret_cast<const unsigned char *>(data), length);
break;
case Sha384:
SHA384Input(&d->sha384Context, reinterpret_cast<const unsigned char *>(data), length);
break;
case Sha512:
SHA512Input(&d->sha512Context, reinterpret_cast<const unsigned char *>(data), length);
break;
case RealSha3_224:
case Keccak_224:
sha3Update(&d->sha3Context, reinterpret_cast<const BitSequence *>(data), quint64(length) * 8);
break;
case RealSha3_256:
case Keccak_256:
sha3Update(&d->sha3Context, reinterpret_cast<const BitSequence *>(data), quint64(length) * 8);
break;
case RealSha3_384:
case Keccak_384:
sha3Update(&d->sha3Context, reinterpret_cast<const BitSequence *>(data), quint64(length) * 8);
break;
case RealSha3_512:
case Keccak_512:
sha3Update(&d->sha3Context, reinterpret_cast<const BitSequence *>(data), quint64(length) * 8);
break;
#endif
}
d->result.clear();
}
QByteArray QCryptographicHash::result() const
{
if (!d->result.isEmpty())
return d->result;
switch (d->method) {
case Sha1: {
Sha1State copy = d->sha1Context;
d->result.resize(20);
sha1FinalizeState(©);
sha1ToHash(©, (unsigned char *)d->result.data());
break;
}
#ifdef QT_CRYPTOGRAPHICHASH_ONLY_SHA1
default:
Q_ASSERT_X(false, "QCryptographicHash", "Method not compiled in");
Q_UNREACHABLE();
break;
#else
case Md4: {
md4_context copy = d->md4Context;
d->result.resize(MD4_RESULTLEN);
md4_final(©, (unsigned char *)d->result.data());
break;
}
case Md5: {
MD5Context copy = d->md5Context;
d->result.resize(16);
MD5Final(©, (unsigned char *)d->result.data());
break;
}
case Sha224: {
SHA224Context copy = d->sha224Context;
d->result.resize(SHA224HashSize);
SHA224Result(©, reinterpret_cast<unsigned char *>(d->result.data()));
break;
}
case Sha256:{
SHA256Context copy = d->sha256Context;
d->result.resize(SHA256HashSize);
SHA256Result(©, reinterpret_cast<unsigned char *>(d->result.data()));
break;
}
case Sha384:{
SHA384Context copy = d->sha384Context;
d->result.resize(SHA384HashSize);
SHA384Result(©, reinterpret_cast<unsigned char *>(d->result.data()));
break;
}
case Sha512:{
SHA512Context copy = d->sha512Context;
d->result.resize(SHA512HashSize);
SHA512Result(©, reinterpret_cast<unsigned char *>(d->result.data()));
break;
}
case RealSha3_224: {
d->sha3Finish(224, QCryptographicHashPrivate::Sha3Variant::Sha3);
break;
}
case RealSha3_256: {
d->sha3Finish(256, QCryptographicHashPrivate::Sha3Variant::Sha3);
break;
}
case RealSha3_384: {
d->sha3Finish(384, QCryptographicHashPrivate::Sha3Variant::Sha3);
break;
}
case RealSha3_512: {
d->sha3Finish(512, QCryptographicHashPrivate::Sha3Variant::Sha3);
break;
}
case Keccak_224: {
d->sha3Finish(224, QCryptographicHashPrivate::Sha3Variant::Keccak);
break;
}
case Keccak_256: {
d->sha3Finish(256, QCryptographicHashPrivate::Sha3Variant::Keccak);
break;
}
case Keccak_384: {
d->sha3Finish(384, QCryptographicHashPrivate::Sha3Variant::Keccak);
break;
}
case Keccak_512: {
d->sha3Finish(512, QCryptographicHashPrivate::Sha3Variant::Keccak);
break;
}
#endif
}
return d->result;
}
QCryptographicHash::QCryptographicHash(Algorithm method)
: d(new QCryptographicHashPrivate)
{
d->method = method;
reset();
}
void QCryptographicHash::reset()
{
switch (d->method) {
case Sha1:
sha1InitState(&d->sha1Context);
break;
#ifdef QT_CRYPTOGRAPHICHASH_ONLY_SHA1
default:
Q_ASSERT_X(false, "QCryptographicHash", "Method not compiled in");
Q_UNREACHABLE();
break;
#else
case Md4:
md4_init(&d->md4Context);
break;
case Md5:
MD5Init(&d->md5Context);
break;
case Sha224:
SHA224Reset(&d->sha224Context);
break;
case Sha256:
SHA256Reset(&d->sha256Context);
break;
case Sha384:
SHA384Reset(&d->sha384Context);
break;
case Sha512:
SHA512Reset(&d->sha512Context);
break;
case RealSha3_224:
case Keccak_224:
sha3Init(&d->sha3Context, 224);
break;
case RealSha3_256:
case Keccak_256:
sha3Init(&d->sha3Context, 256);
break;
case RealSha3_384:
case Keccak_384:
sha3Init(&d->sha3Context, 384);
break;
case RealSha3_512:
case Keccak_512:
sha3Init(&d->sha3Context, 512);
break;
#endif
}
d->result.clear();
}
标签:case,Qt,Keccak,break,result,计算,copy,data,md5
From: https://www.cnblogs.com/simp/p/17421616.html