Files
GopherGate/target/doc/ring/hmac/index.html
2026-02-26 12:00:21 -05:00

71 lines
10 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta name="generator" content="rustdoc"><meta name="description" content="HMAC is specified in RFC 2104."><title>ring::hmac - Rust</title><script>if(window.location.protocol!=="file:")document.head.insertAdjacentHTML("beforeend","SourceSerif4-Regular-6b053e98.ttf.woff2,FiraSans-Italic-81dc35de.woff2,FiraSans-Regular-0fe48ade.woff2,FiraSans-MediumItalic-ccf7e434.woff2,FiraSans-Medium-e1aa3f0a.woff2,SourceCodePro-Regular-8badfe75.ttf.woff2,SourceCodePro-Semibold-aa29a496.ttf.woff2".split(",").map(f=>`<link rel="preload" as="font" type="font/woff2"href="../../static.files/${f}">`).join(""))</script><link rel="stylesheet" href="../../static.files/normalize-9960930a.css"><link rel="stylesheet" href="../../static.files/rustdoc-ca0dd0c4.css"><meta name="rustdoc-vars" data-root-path="../../" data-static-root-path="../../static.files/" data-current-crate="ring" data-themes="" data-resource-suffix="" data-rustdoc-version="1.93.1 (01f6ddf75 2026-02-11) (Arch Linux rust 1:1.93.1-1)" data-channel="1.93.1" data-search-js="search-9e2438ea.js" data-stringdex-js="stringdex-a3946164.js" data-settings-js="settings-c38705f0.js" ><script src="../../static.files/storage-e2aeef58.js"></script><script defer src="../sidebar-items.js"></script><script defer src="../../static.files/main-a410ff4d.js"></script><noscript><link rel="stylesheet" href="../../static.files/noscript-263c88ec.css"></noscript><link rel="alternate icon" type="image/png" href="../../static.files/favicon-32x32-eab170b8.png"><link rel="icon" type="image/svg+xml" href="../../static.files/favicon-044be391.svg"></head><body class="rustdoc mod"><!--[if lte IE 11]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><rustdoc-topbar><h2><a href="#">Module hmac</a></h2></rustdoc-topbar><nav class="sidebar"><div class="sidebar-crate"><h2><a href="../../ring/index.html">ring</a><span class="version">0.17.14</span></h2></div><div class="sidebar-elems"><section id="rustdoc-toc"><h2 class="location"><a href="#">Module hmac</a></h2><h3><a href="#">Sections</a></h3><ul class="block top-toc"><li><a href="#examples" title="Examples:">Examples:</a><ul><li><a href="#signing-a-value-and-verifying-it-wasnt-tampered-with" title="Signing a value and verifying it wasnt tampered with">Signing a value and verifying it wasnt tampered with</a></li><li><a href="#using-the-one-shot-api" title="Using the one-shot API:">Using the one-shot API:</a></li><li><a href="#using-the-multi-part-api" title="Using the multi-part API:">Using the multi-part API:</a></li></ul></li></ul><h3><a href="#structs">Module Items</a></h3><ul class="block"><li><a href="#structs" title="Structs">Structs</a></li><li><a href="#statics" title="Statics">Statics</a></li><li><a href="#functions" title="Functions">Functions</a></li></ul></section><div id="rustdoc-modnav"><h2 class="in-crate"><a href="../index.html">In crate ring</a></h2></div></div></nav><div class="sidebar-resizer" title="Drag to resize sidebar"></div><main><div class="width-limiter"><section id="main-content" class="content"><div class="main-heading"><div class="rustdoc-breadcrumbs"><a href="../index.html">ring</a></div><h1>Module <span>hmac</span>&nbsp;<button id="copy-path" title="Copy item path to clipboard">Copy item path</button></h1><rustdoc-toolbar></rustdoc-toolbar><span class="sub-heading"><a class="src" href="../../src/ring/hmac.rs.html#15-440">Source</a> </span></div><details class="toggle top-doc" open><summary class="hideme"><span>Expand description</span></summary><div class="docblock"><p>HMAC is specified in <a href="https://tools.ietf.org/html/rfc2104">RFC 2104</a>.</p>
<p>After a <code>Key</code> is constructed, it can be used for multiple signing or
verification operations. Separating the construction of the key from the
rest of the HMAC operation allows the per-key precomputation to be done
only once, instead of it being done in every HMAC operation.</p>
<p>Frequently all the data to be signed in a message is available in a single
contiguous piece. In that case, the module-level <code>sign</code> function can be
used. Otherwise, if the input is in multiple parts, <code>Context</code> should be
used.</p>
<h2 id="examples"><a class="doc-anchor" href="#examples">§</a>Examples:</h2><h3 id="signing-a-value-and-verifying-it-wasnt-tampered-with"><a class="doc-anchor" href="#signing-a-value-and-verifying-it-wasnt-tampered-with">§</a>Signing a value and verifying it wasnt tampered with</h3>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>ring::{hmac, rand};
<span class="kw">let </span>rng = rand::SystemRandom::new();
<span class="kw">let </span>key = hmac::Key::generate(hmac::HMAC_SHA256, <span class="kw-2">&amp;</span>rng)<span class="question-mark">?</span>;
<span class="kw">let </span>msg = <span class="string">"hello, world"</span>;
<span class="kw">let </span>tag = hmac::sign(<span class="kw-2">&amp;</span>key, msg.as_bytes());
<span class="comment">// [We give access to the message to an untrusted party, and they give it
// back to us. We need to verify they didn't tamper with it.]
</span>hmac::verify(<span class="kw-2">&amp;</span>key, msg.as_bytes(), tag.as_ref())<span class="question-mark">?</span>;
</code></pre></div><h3 id="using-the-one-shot-api"><a class="doc-anchor" href="#using-the-one-shot-api">§</a>Using the one-shot API:</h3>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>ring::{digest, hmac, rand};
<span class="kw">use </span>ring::rand::SecureRandom;
<span class="kw">let </span>msg = <span class="string">"hello, world"</span>;
<span class="comment">// The sender generates a secure key value and signs the message with it.
// Note that in a real protocol, a key agreement protocol would be used to
// derive `key_value`.
</span><span class="kw">let </span>rng = rand::SystemRandom::new();
<span class="kw">let </span>key_value: [u8; digest::SHA256_OUTPUT_LEN] = rand::generate(<span class="kw-2">&amp;</span>rng)<span class="question-mark">?</span>.expose();
<span class="kw">let </span>s_key = hmac::Key::new(hmac::HMAC_SHA256, key_value.as_ref());
<span class="kw">let </span>tag = hmac::sign(<span class="kw-2">&amp;</span>s_key, msg.as_bytes());
<span class="comment">// The receiver (somehow!) knows the key value, and uses it to verify the
// integrity of the message.
</span><span class="kw">let </span>v_key = hmac::Key::new(hmac::HMAC_SHA256, key_value.as_ref());
hmac::verify(<span class="kw-2">&amp;</span>v_key, msg.as_bytes(), tag.as_ref())<span class="question-mark">?</span>;
</code></pre></div><h3 id="using-the-multi-part-api"><a class="doc-anchor" href="#using-the-multi-part-api">§</a>Using the multi-part API:</h3>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>ring::{digest, hmac, rand};
<span class="kw">use </span>ring::rand::SecureRandom;
<span class="kw">let </span>parts = [<span class="string">"hello"</span>, <span class="string">", "</span>, <span class="string">"world"</span>];
<span class="comment">// The sender generates a secure key value and signs the message with it.
// Note that in a real protocol, a key agreement protocol would be used to
// derive `key_value`.
</span><span class="kw">let </span>rng = rand::SystemRandom::new();
<span class="kw">let </span><span class="kw-2">mut </span>key_value: [u8; digest::SHA384_OUTPUT_LEN] = rand::generate(<span class="kw-2">&amp;</span>rng)<span class="question-mark">?</span>.expose();
<span class="kw">let </span>s_key = hmac::Key::new(hmac::HMAC_SHA384, key_value.as_ref());
<span class="kw">let </span><span class="kw-2">mut </span>s_ctx = hmac::Context::with_key(<span class="kw-2">&amp;</span>s_key);
<span class="kw">for </span>part <span class="kw">in </span><span class="kw-2">&amp;</span>parts {
s_ctx.update(part.as_bytes());
}
<span class="kw">let </span>tag = s_ctx.sign();
<span class="comment">// The receiver (somehow!) knows the key value, and uses it to verify the
// integrity of the message.
</span><span class="kw">let </span>v_key = hmac::Key::new(hmac::HMAC_SHA384, key_value.as_ref());
<span class="kw">let </span><span class="kw-2">mut </span>msg = Vec::&lt;u8&gt;::new();
<span class="kw">for </span>part <span class="kw">in </span><span class="kw-2">&amp;</span>parts {
msg.extend(part.as_bytes());
}
hmac::verify(<span class="kw-2">&amp;</span>v_key, <span class="kw-2">&amp;</span>msg.as_ref(), tag.as_ref())<span class="question-mark">?</span>;
</code></pre></div></div></details><h2 id="structs" class="section-header">Structs<a href="#structs" class="anchor">§</a></h2><dl class="item-table"><dt><a class="struct" href="struct.Algorithm.html" title="struct ring::hmac::Algorithm">Algorithm</a></dt><dd>An HMAC algorithm.</dd><dt><a class="struct" href="struct.Context.html" title="struct ring::hmac::Context">Context</a></dt><dd>A context for multi-step (Init-Update-Finish) HMAC signing.</dd><dt><a class="struct" href="struct.Key.html" title="struct ring::hmac::Key">Key</a></dt><dd>A key to use for HMAC signing.</dd><dt><a class="struct" href="struct.Tag.html" title="struct ring::hmac::Tag">Tag</a></dt><dd>An HMAC tag.</dd></dl><h2 id="statics" class="section-header">Statics<a href="#statics" class="anchor">§</a></h2><dl class="item-table"><dt><a class="static" href="static.HMAC_SHA1_FOR_LEGACY_USE_ONLY.html" title="static ring::hmac::HMAC_SHA1_FOR_LEGACY_USE_ONLY">HMAC_<wbr>SHA1_<wbr>FOR_<wbr>LEGACY_<wbr>USE_<wbr>ONLY</a></dt><dd>HMAC using SHA-1. Obsolete.</dd><dt><a class="static" href="static.HMAC_SHA256.html" title="static ring::hmac::HMAC_SHA256">HMAC_<wbr>SHA256</a></dt><dd>HMAC using SHA-256.</dd><dt><a class="static" href="static.HMAC_SHA384.html" title="static ring::hmac::HMAC_SHA384">HMAC_<wbr>SHA384</a></dt><dd>HMAC using SHA-384.</dd><dt><a class="static" href="static.HMAC_SHA512.html" title="static ring::hmac::HMAC_SHA512">HMAC_<wbr>SHA512</a></dt><dd>HMAC using SHA-512.</dd></dl><h2 id="functions" class="section-header">Functions<a href="#functions" class="anchor">§</a></h2><dl class="item-table"><dt><a class="fn" href="fn.sign.html" title="fn ring::hmac::sign">sign</a></dt><dd>Calculates the HMAC of <code>data</code> using the key <code>key</code> in one step.</dd><dt><a class="fn" href="fn.verify.html" title="fn ring::hmac::verify">verify</a></dt><dd>Calculates the HMAC of <code>data</code> using the signing key <code>key</code>, and verifies
whether the resultant value equals <code>tag</code>, in one step.</dd></dl></section></div></main></body></html>