英語版
このページの英語版を見る

std.digest.hmac

本パッケージは、RFC2104で定義されているハッシュベースのメッセージ認証 コード(HMAC)アルゴリズムを実装する。 アルゴリズムを実装する。対応する 対応するWikipedia の記事も参照のこと。
Examples:
テンプレートAPIのHMAC実装。
これはダイジェストHに対するHMACを実装する。 がブロック・サイズに関する情報を提供しない場合は、2番目のオーバーロードを使用して明示的に提供することができる。 を使って明示的に与えることができる。
この型は以下に準拠する。 std.digest.isDigest.
入力文字列に対してHMACを計算する
import std.ascii : LetterCase;
import std.digest : toHexString;
import std.digest.sha : SHA1;
import std.string : representation;

auto secret = "secret".representation;
assert("The quick brown fox jumps over the lazy dog"
        .representation
        .hmac!SHA1(secret)
        .toHexString!(LetterCase.lower) == "198ea1ea04c435c1246b586a06d5cf11c3ffcda6");
struct HMAC(H, size_t hashBlockSize) if (hashBlockSize % 8 == 0);

template hmac(H) if (isDigest!H && hasBlockSize!H)

auto hmac(H, size_t blockSize)(scope const(ubyte)[] secret)
if (isDigest!H);
Hがブロックサイズに関する情報を提供しない場合に使用されるHMACのオーバーロード。 のオーバーロードである。
Examples:
import std.digest.sha : SHA1;
import std.string : representation;
string data1 = "Hello, world", data2 = "Hola mundo";
auto hmac = HMAC!SHA1("My s3cR3T keY".representation);
auto digest = hmac.put(data1.representation)
                  .put(data2.representation)
                  .finish();
static immutable expected = [
    197, 57, 52, 3, 13, 194, 13,
    36, 117, 228, 8, 11, 111, 51,
    165, 3, 123, 31, 251, 113];
writeln(digest); // expected
this(scope const(ubyte)[] secret);
指定された秘密を使用してHMACダイジェストを構築する。
Examples:
import std.digest.sha : SHA1;
import std.string : representation;
auto hmac = HMAC!SHA1("My s3cR3T keY".representation);
hmac.put("Hello, world".representation);
static immutable expected = [
    130, 32, 235, 44, 208, 141,
    150, 232, 211, 214, 162, 195,
    188, 127, 52, 89, 100, 68, 90, 216];
writeln(hmac.finish()); // expected
ref HMAC!(H, blockSize) start() return;
ダイジェストを再初期化し、再利用できるようにする。

注釈 コンストラクタはダイジェストを初期化した状態のままにしておく。 メソッドをコールする必要があるのは、 未完成のダイジェストを再利用する場合だけである。

Returns:
連結に便利なダイジェストへの参照。
Examples:
import std.digest.sha : SHA1;
import std.string : representation;
string data1 = "Hello, world", data2 = "Hola mundo";
auto hmac = HMAC!SHA1("My s3cR3T keY".representation);
hmac.put(data1.representation);
hmac.start();                   // ダイジェストをリセット
hmac.put(data2.representation); // やり直す
static immutable expected = [
    122, 151, 232, 240, 249, 80,
    19, 178, 186, 77, 110, 23, 208,
    52, 11, 88, 34, 151, 192, 255];
writeln(hmac.finish()); // expected
ref HMAC!(H, blockSize) put(in ubyte[] data...) return;
データの一部をハッシュ計算に投入する。このメソッドは 型として使用できる。 std.range.OutputRange.
Returns:
ダイジェストへの参照として使用できる。
Examples:
import std.digest.hmac, std.digest.sha;
import std.string : representation;
string data1 = "Hello, world", data2 = "Hola mundo";
auto hmac = HMAC!SHA1("My s3cR3T keY".representation);
hmac.put(data1.representation)
    .put(data2.representation);
static immutable expected = [
    197, 57, 52, 3, 13, 194, 13,
    36, 117, 228, 8, 11, 111, 51,
    165, 3, 123, 31, 251, 113];
writeln(hmac.finish()); // expected
DigestType!H finish();
ダイジェストをリセットし、終了したハッシュを返す。
Examples:
import std.digest.sha : SHA1;
import std.string : representation;
string data1 = "Hello, world", data2 = "Hola mundo";
auto hmac = HMAC!SHA1("My s3cR3T keY".representation);
auto testDigest = hmac.put(data1.representation)
                  .put(data2.representation)
                  .finish();
static immutable expected = [
    197, 57, 52, 3, 13, 194, 13,
    36, 117, 228, 8, 11, 111, 51,
    165, 3, 123, 31, 251, 113];
writeln(testDigest); // expected