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も参照のこと

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

[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()も参照してください

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

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

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

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

移動-新しい QMessageAuthenticationCode をother から構築する。

注意: 移動元のオブジェクトother は、部分的に形成された状態に置かれます。この状態で有効な操作は、破壊と新しいオブジェクトの割り当てだけです。

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

[noexcept] QMessageAuthenticationCode::~QMessageAuthenticationCode()

オブジェクトを破壊する。

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

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

注意: Qt 6.6 より前のバージョンでは、この関数の引数はQByteArrayView ではなくQByteArray でした。コンパイルエラーが発生する場合は、コードがQByteArrayView ではなくQByteArray に暗黙的に変換可能なオブジェクトを渡しているためです。対応する引数を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 より前のバージョンでは、この関数の引数はQByteArrayView ではなくQByteArray でした。コンパイル・エラーが発生する場合は、コードがQByteArray に暗黙的に変換可能なオブジェクトを渡しているためで、QByteArrayView には変換できません。対応する引数をQByteArray{~~~} で囲んで、キャストを明示的にしてください。これは古い Qt バージョンとの後方互換性があります。

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

[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);

std::optionalを使えば、キーがわかるまでQMessageAuthenticationCode

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)

Move-assignother をこのQMessageAuthenticationCode インスタンスに割り当てる。

注意: 移動元のオブジェクトother は部分的に形成された状態に置かれます。この状態では、有効な操作は破壊と新しいオブジェクトの割り当てだけです。

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

© 2025 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.