このページでは

QMessageAuthenticationCode Class

QMessageAuthenticationCode クラスは、ハッシュに基づくメッセージ認証コードを生成する手段を提供します。詳細...

ヘッダー: #include <QMessageAuthenticationCode>
CMake: find_package(Qt6 REQUIRED COMPONENTS Core)
target_link_libraries(mytarget PRIVATE Qt6::Core)
qmake: QT += core

注:このクラスのすべての関数は再入可能である。

パブリック関数

QMessageAuthenticationCode(QCryptographicHash::Algorithm method, QByteArrayView key = {})
(since 6.6) QMessageAuthenticationCode(QMessageAuthenticationCode &&other)
~QMessageAuthenticationCode()
void addData(QByteArrayView data)
bool addData(QIODevice *device)
void addData(const char *data, qsizetype length)
void reset()
QByteArray result() const
(since 6.6) QByteArrayView resultView() const
void setKey(QByteArrayView key)
(since 6.6) void swap(QMessageAuthenticationCode &other)
(since 6.6) QMessageAuthenticationCode &operator=(QMessageAuthenticationCode &&other)

静的パブリックメンバー

QByteArray hash(QByteArrayView message, QByteArrayView key, QCryptographicHash::Algorithm method)
(since 6.8) QByteArrayView hashInto(QSpan<char> buffer, QByteArrayView message, QByteArrayView key, QCryptographicHash::Algorithm method)
(since 6.8) QByteArrayView hashInto(QSpan<char> buffer, QSpan<const QByteArrayView> messageParts, QByteArrayView key, QCryptographicHash::Algorithm method)
(since 6.8) QByteArrayView hashInto(QSpan<std::byte> buffer, QByteArrayView message, QByteArrayView key, QCryptographicHash::Algorithm method)
(since 6.8) QByteArrayView hashInto(QSpan<std::byte> buffer, QSpan<const QByteArrayView> messageParts, QByteArrayView key, QCryptographicHash::Algorithm method)
(since 6.8) QByteArrayView hashInto(QSpan<uchar> buffer, QByteArrayView message, QByteArrayView key, QCryptographicHash::Algorithm method)
(since 6.8) QByteArrayView hashInto(QSpan<uchar> buffer, QSpan<const QByteArrayView> messageParts, QByteArrayView key, QCryptographicHash::Algorithm method)

詳細な説明

QMessageAuthenticationCode クラスを使用して、ハッシュベースのメッセージ認証コード (HMAC) を生成します。このクラスは、QCryptographicHash に記載されているすべての暗号ハッシュアルゴリズムをサポートしています (QCryptographicHash::Algorithm も参照してください)。

メッセージ認証コードを生成するには、適切なハッシュアルゴリズムと秘密鍵をコンストラクタに渡します。その後、addData() を 1 回以上呼び出してメッセージデータを処理します。メッセージ全体の処理が完了したら、result() 関数を呼び出して最終的な認証コードを取得します。

    QByteArray key = "key";
    QByteArray message = "The quick brown fox jumps over the lazy dog";
    ...
    QMessageAuthenticationCode code(QCryptographicHash::Sha256, key);
    code.addData(message);
    code.result().toHex(); // returns "f7bc83f430538424b13298e6aa6fb143ef4d59a14946175997479dbc2d1a3cd8"

上記のような単純なケースでは、静的関数hash() を使用することもできます:

    QMessageAuthenticationCode::hash(message, key, QCryptographicHash::Sha256).toHex();

注: HMACの暗号強度は 、秘密鍵のサイズおよび基盤となるハッシュ関数のセキュリティに依存します。

「 QCryptographicHash 」および「QCryptographicHash::Algorithm 」も参照してください 。

メンバ関数のドキュメント

[explicit] QMessageAuthenticationCode::QMessageAuthenticationCode(QCryptographicHash::Algorithm method, QByteArrayView key = {})

method メソッドとキーkey を使用して、データから暗号ハッシュを生成するために使用できるオブジェクトを構築します。

注: Qt 6.6以前のバージョンでは 、この関数の引数は `QByteArray` として指定され、`QByteArrayView` ではありませんでした。コンパイルエラーが発生する場合は、コード内で `QByteArray` には暗黙的に変換可能だが `QByteArrayView` には変換できないオブジェクトが渡されているためです。対応する引数を `QByteArray{~~~} ` でラップして、型変換を明示的に行ってください。これにより、古いバージョンの Qt との下位互換性が保たれます。

[noexcept, since 6.6] QMessageAuthenticationCode::QMessageAuthenticationCode(QMessageAuthenticationCode &&other)

other から新しい QMessageAuthenticationCode をムーブコンストラクタで生成します。

注: 移動元のオブジェクト `other `は 、部分的に形成された状態となり、この状態では破棄と新しいオブジェクトへの代入のみが有効な操作となります。

この関数は Qt 6.6 で導入されました。

[noexcept] QMessageAuthenticationCode::~QMessageAuthenticationCode()

オブジェクトを破棄します。

[noexcept] void QMessageAuthenticationCode::addData(QByteArrayView data)

メッセージに「data 」を追加します。

注: Qt 6.6以前のバージョンでは 、この関数は引数を `QByteArray` として受け取っており、`QByteArrayView` としては受け取っていませんでした。コンパイルエラーが発生する場合は、コード内で `QByteArray` には暗黙的に変換可能だが `QByteArrayView` には変換できないオブジェクトが渡されているためです。対応する引数を `QByteArray{~~~} ` でラップして、キャストを明示的にしてください。これにより、古い Qt バージョンとの下位互換性が保たれます。

resultView() およびresult()も参照してください 。

bool QMessageAuthenticationCode::addData(QIODevice *device)

開いているQIODevice device からデータが終了するまで読み込み、それをmessageに追加します。読み込みが成功した場合はtrue を返します。

注: device は あらかじめ開かれている必要があります。

void QMessageAuthenticationCode::addData(const char *data, qsizetype length)

data の先頭からlength 文字分をメッセージに追加します。

これはオーバーロードされた関数です。

[static] QByteArray QMessageAuthenticationCode::hash(QByteArrayView message, QByteArrayView key, QCryptographicHash::Algorithm method)

キー `key ` およびメソッド `method` を使用して、メッセージ `message ` の認証コードを返します。

注: Qt 6.6以前のバージョンでは 、この関数は引数として `QByteArray` を受け取り、`QByteArrayView` を受け取ることはありませんでした。コンパイルエラーが発生する場合は、コード内で `QByteArray` には暗黙的に変換可能であるが、`QByteArrayView` には変換できないオブジェクトを渡していることが原因です。対応する引数を `QByteArray{~~~} ` でラップして、キャストを明示的にしてください。これにより、古い Qt バージョンとの下位互換性が保たれます。

hashInto()も参照してください 。

[static noexcept, since 6.8] QByteArrayView QMessageAuthenticationCode::hashInto(QSpan<char> buffer, QByteArrayView message, QByteArrayView key, QCryptographicHash::Algorithm method)

[static noexcept, since 6.8] QByteArrayView QMessageAuthenticationCode::hashInto(QSpan<char> buffer, QSpan<const QByteArrayView> messageParts, QByteArrayView key, QCryptographicHash::Algorithm method)

[static noexcept, since 6.8] QByteArrayView QMessageAuthenticationCode::hashInto(QSpan<std::byte> buffer, QByteArrayView message, QByteArrayView key, QCryptographicHash::Algorithm method)

[static noexcept, since 6.8] QByteArrayView QMessageAuthenticationCode::hashInto(QSpan<std::byte> buffer, QSpan<const QByteArrayView> messageParts, QByteArrayView key, QCryptographicHash::Algorithm method)

[static noexcept, since 6.8] QByteArrayView QMessageAuthenticationCode::hashInto(QSpan<uchar> buffer, QByteArrayView message, QByteArrayView key, QCryptographicHash::Algorithm method)

[static noexcept, since 6.8] QByteArrayView QMessageAuthenticationCode::hashInto(QSpan<uchar> buffer, QSpan<const QByteArrayView> messageParts, QByteArrayView key, QCryptographicHash::Algorithm method)

キーkey およびメソッドmethod を使用して、メッセージの認証コード(message 、またはQSpan のオーバーロードの場合はmessageParts の連結)を返します。

戻り値はbuffer のサブスパンとなります。ただし、buffer のサイズが不十分な場合は、nullQByteArrayView が返されます。

これらの関数は Qt 6.8 で導入されました。

hash()も参照してください 。

[noexcept] void QMessageAuthenticationCode::reset()

メッセージデータをリセットします。この関数を呼び出しても、キーには影響しません。

QByteArray QMessageAuthenticationCode::result() const

最終的な認証コードを返します。

resultView() およびQByteArray::toHex()も参照してください 。

[noexcept, since 6.6] QByteArrayView QMessageAuthenticationCode::resultView() const

最終的なハッシュ値を返します。

なお、返されたビューは、QMessageAuthenticationCode オブジェクトが他の手段によって変更されない限り、有効であることに注意してください。

この関数は Qt 6.6 で導入されました。

result()も参照してください 。

[noexcept] void QMessageAuthenticationCode::setKey(QByteArrayView key)

key のシークレットを設定します。この関数を呼び出すと、オブジェクトの状態が自動的にリセットされます。

パフォーマンスを最適化するには、この関数はアクティブなキーを変更する場合にのみ呼び出し、次のように初期キーを設定するために使用しないでください。

QMessageAuthenticationCode mac(method);
mac.setKey(key); // does extra work
use(mac);

初期キーは、コンストラクタの引数として渡すことを推奨します:

QMessageAuthenticationCode mac(method, key); // OK, optimal
use(mac);

キーが判明するまでQMessageAuthenticationCode の生成を遅延させるには、std::optionalを使用できます:

std::optional<QMessageAuthenticationCode> mac;
~~~
key = ~~~;
mac.emplace(method, key);
use(*mac);

注: Qt 6.6以前のバージョンでは 、この関数の引数はQByteArrayView ではなくQByteArray として扱われていました。コンパイルエラーが発生する場合は、コード内でQByteArray には暗黙的に変換可能だが、QByteArrayView には変換できないオブジェクトが渡されているためです。対応する引数をQByteArray{~~~} でラップして、キャストを明示的にしてください。これにより、古い Qt バージョンとの下位互換性が保たれます。

[noexcept, since 6.6] void QMessageAuthenticationCode::swap(QMessageAuthenticationCode &other)

このメッセージ認証コードをother と交換します。この処理は非常に高速で、失敗することはありません。

この関数は Qt 6.6 で導入されました。

[noexcept, since 6.6] QMessageAuthenticationCode &QMessageAuthenticationCode::operator=(QMessageAuthenticationCode &&other)

other をこのQMessageAuthenticationCode インスタンスに割り当てます。

注: 移動元のオブジェクト other は 、部分的に形成された状態になります。この状態では、有効な操作は破棄と新しいオブジェクトへの代入のみです。

この関数は Qt 6.6 で導入されました。

© 2026 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.