Movatterモバイル変換


[0]ホーム

URL:


D Logo
Menu
Search

Library Reference

version 2.111.0

overview

Report a bug
If you spot a problem with this page, click here to create a Bugzilla issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page.Requires a signed-in GitHub account. This works well for small changes.If you'd like to make larger changes you may want to consider usinga local clone.

std.digest.hmac

This package implements the hash-based message authentication code (HMAC)algorithm as defined inRFC2104. See alsothe correspondingWikipedia article.
License:
Boost License 1.0.

Sourcestd/digest/hmac.d

Examples:
Template API HMAC implementation.
This implements an HMAC over the digest H. If H doesn't provide information about the block size, it can be supplied explicitly using the second overload.
This type conforms tostd.digest.isDigest.
Compute HMAC over an input string
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");
structHMAC(H, size_t hashBlockSize) if (hashBlockSize % 8 == 0);

templatehmac(H) if (isDigest!H && hasBlockSize!H)

autohmac(H, size_t blockSize)(scope const(ubyte)[]secret)
if (isDigest!H);
Overload of HMAC to be used if H doesn't provide information about its block size.
Examples:
import std.digest.sha : SHA1;import std.string : representation;string data1 ="Hello, world", data2 ="Hola mundo";autohmac =HMAC!SHA1("My s3cR3T keY".representation);auto digest =hmac.put(data1.representation)                  .put(data2.representation)                  .finish();staticimmutable 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);
Constructs the HMAC digest using the specified secret.
Examples:
import std.digest.sha : SHA1;import std.string : representation;auto hmac = HMAC!SHA1("My s3cR3T keY".representation);hmac.put("Hello, world".representation);staticimmutable 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;
Reinitializes the digest, making it ready for reuse.

NoteThe constructor leaves the digest in an initialized state, so that this method only needs to be called if an unfinished digest is to be reused.

Returns:
A reference to the digest for convenient chaining.
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();// reset digesthmac.put(data2.representation);// start overstaticimmutable 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;
Feeds a piece of data into the hash computation. This method allows the type to be used as anstd.range.OutputRange.
Returns:
A reference to the digest for convenient chaining.
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);staticimmutable 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!Hfinish();
Resets the digest and returns the finished hash.
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();staticimmutable expected = [    197, 57, 52, 3, 13, 194, 13,    36, 117, 228, 8, 11, 111, 51,    165, 3, 123, 31, 251, 113];writeln(testDigest);// expected
Copyright © 1999-2025 by theD Language Foundation | Page generated byDdoc on Fri Oct 10 22:10:31 2025

[8]ページ先頭

©2009-2025 Movatter.jp