13,009
回編集
編集の要約なし |
|||
184行目: | 184行目: | ||
<br> | <br> | ||
ただし、セキュリティ上重要な用途では、専門家によって監査・検証されたライブラリや実装を使用することが推奨される。<br> | ただし、セキュリティ上重要な用途では、専門家によって監査・検証されたライブラリや実装を使用することが推奨される。<br> | ||
<br><br> | |||
== QMessageAuthenticationCodeクラス == | |||
==== ハッシュ化 ==== | |||
以下の例では、<code>QMessageAuthenticationCode</code>クラスを使用して、MD4、MD5、SHA-2、SHA-3でハッシュ化している。<br> | |||
<br> | |||
各ハッシュアルゴリズムに対して、<code>QMessageAuthenticationCode</code>オブジェクトを作成して、コンストラクタでアルゴリズムを指定する。<br> | |||
<code>addData</code>メソッドを使用して、ハッシュ化する文字列をUTF-8エンコードで追加する。<br> | |||
<code>result</code>メソッドを使用してハッシュ値を取得し、<code>toHex</code>メソッドで16進数表記の文字列に変換する。<br> | |||
<br> | |||
出力結果は、<code>QCryptographicHash</code>クラスを使用した場合と同値となる。<br> | |||
<br> | |||
<code>QMessageAuthenticationCode</code>クラスは、主にメッセージ認証コード (MAC) の計算に使用されるが、このようにハッシュ化にも使用することができる。<br> | |||
<u>ただし、一般的なハッシュ化の用途では、<code>QCryptographicHash</code>クラスを使用する方が簡潔で便利である。</u><br> | |||
<syntaxhighlight lang="c++"> | |||
#include <QCoreApplication> | |||
#include <QMessageAuthenticationCode> | |||
#include <QDebug> | |||
int main(int argc, char *argv[]) | |||
{ | |||
QCoreApplication a(argc, argv); | |||
// ハッシュ化する文字列 | |||
QString input = "Hello, world!"; | |||
// MD4 | |||
QMessageAuthenticationCode md4(QCryptographicHash::Md4); | |||
md4.addData(input.toUtf8()); | |||
QByteArray md4Hash = md4.result().toHex(); | |||
qDebug() << "MD4 Hash:" << md4Hash; | |||
// MD5 | |||
QMessageAuthenticationCode md5(QCryptographicHash::Md5); | |||
md5.addData(input.toUtf8()); | |||
QByteArray md5Hash = md5.result().toHex(); | |||
qDebug() << "MD5 Hash:" << md5Hash; | |||
// SHA-2 (SHA-224) | |||
QMessageAuthenticationCode sha224(QCryptographicHash::Sha224); | |||
sha224.addData(input.toUtf8()); | |||
QByteArray sha224Hash = sha224.result().toHex(); | |||
qDebug() << "SHA-2 (SHA-224) Hash:" << sha224Hash; | |||
// SHA-2 (SHA-256) | |||
QMessageAuthenticationCode sha256(QCryptographicHash::Sha256); | |||
sha256.addData(input.toUtf8()); | |||
QByteArray sha256Hash = sha256.result().toHex(); | |||
qDebug() << "SHA-2 (SHA-256) Hash:" << sha256Hash; | |||
// SHA-2 (SHA-384) | |||
QMessageAuthenticationCode sha384(QCryptographicHash::Sha384); | |||
sha384.addData(input.toUtf8()); | |||
QByteArray sha384Hash = sha384.result().toHex(); | |||
qDebug() << "SHA-2 (SHA-384) Hash:" << sha384Hash; | |||
// SHA-2 (SHA-512) | |||
QMessageAuthenticationCode sha512(QCryptographicHash::Sha512); | |||
sha512.addData(input.toUtf8()); | |||
QByteArray sha512Hash = sha512.result().toHex(); | |||
qDebug() << "SHA-2 (SHA-512) Hash:" << sha512Hash; | |||
// SHA-3 (SHA3-224) | |||
QMessageAuthenticationCode sha3_224(QCryptographicHash::Sha3_224); | |||
sha3_224.addData(input.toUtf8()); | |||
QByteArray sha3_224Hash = sha3_224.result().toHex(); | |||
qDebug() << "SHA-3 (SHA3-224) Hash:" << sha3_224Hash; | |||
// SHA-3 (SHA3-256) | |||
QMessageAuthenticationCode sha3_256(QCryptographicHash::Sha3_256); | |||
sha3_256.addData(input.toUtf8()); | |||
QByteArray sha3_256Hash = sha3_256.result().toHex(); | |||
qDebug() << "SHA-3 (SHA3-256) Hash:" << sha3_256Hash; | |||
// SHA-3 (SHA3-384) | |||
QMessageAuthenticationCode sha3_384(QCryptographicHash::Sha3_384); | |||
sha3_384.addData(input.toUtf8()); | |||
QByteArray sha3_384Hash = sha3_384.result().toHex(); | |||
qDebug() << "SHA-3 (SHA3-384) Hash:" << sha3_384Hash; | |||
// SHA-3 (SHA3-512) | |||
QMessageAuthenticationCode sha3_512(QCryptographicHash::Sha3_512); | |||
sha3_512.addData(input.toUtf8()); | |||
QByteArray sha3_512Hash = sha3_512.result().toHex(); | |||
qDebug() << "SHA-3 (SHA3-512) Hash:" << sha3_512Hash; | |||
return a.exec(); | |||
} | |||
</syntaxhighlight> | |||
<br><br> | <br><br> | ||