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

88 lines
11 KiB
HTML

<!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="PBKDF2 derivation and verification."><title>ring::pbkdf2 - 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 pbkdf2</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 pbkdf2</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="#password-database-example" title="Password Database Example">Password Database Example</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>pbkdf2</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/pbkdf2.rs.html#15-344">Source</a> </span></div><details class="toggle top-doc" open><summary class="hideme"><span>Expand description</span></summary><div class="docblock"><p>PBKDF2 derivation and verification.</p>
<p>Use <code>derive</code> to derive PBKDF2 outputs. Use <code>verify</code> to verify secret
against previously-derived outputs.</p>
<p>PBKDF2 is specified in <a href="https://tools.ietf.org/html/rfc2898#section-5.2">RFC 2898 Section 5.2</a> with test vectors given in
<a href="https://tools.ietf.org/html/rfc6070">RFC 6070</a>. See also <a href="http://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-132.pdf">NIST Special Publication 800-132</a>.</p>
<h2 id="examples"><a class="doc-anchor" href="#examples">§</a>Examples</h2><h3 id="password-database-example"><a class="doc-anchor" href="#password-database-example">§</a>Password Database Example</h3>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>ring::{digest, pbkdf2};
<span class="kw">use </span>std::{collections::HashMap, num::NonZeroU32};
<span class="kw">static </span>PBKDF2_ALG: pbkdf2::Algorithm = pbkdf2::PBKDF2_HMAC_SHA256;
<span class="kw">const </span>CREDENTIAL_LEN: usize = digest::SHA256_OUTPUT_LEN;
<span class="kw">pub type </span>Credential = [u8; CREDENTIAL_LEN];
<span class="kw">enum </span>Error {
WrongUsernameOrPassword
}
<span class="kw">struct </span>PasswordDatabase {
pbkdf2_iterations: NonZeroU32,
db_salt_component: [u8; <span class="number">16</span>],
<span class="comment">// Normally this would be a persistent database.
</span>storage: HashMap&lt;String, Credential&gt;,
}
<span class="kw">impl </span>PasswordDatabase {
<span class="kw">pub fn </span>store_password(<span class="kw-2">&amp;mut </span><span class="self">self</span>, username: <span class="kw-2">&amp;</span>str, password: <span class="kw-2">&amp;</span>str) {
<span class="kw">let </span>salt = <span class="self">self</span>.salt(username);
<span class="kw">let </span><span class="kw-2">mut </span>to_store: Credential = [<span class="number">0u8</span>; CREDENTIAL_LEN];
pbkdf2::derive(PBKDF2_ALG, <span class="self">self</span>.pbkdf2_iterations, <span class="kw-2">&amp;</span>salt,
password.as_bytes(), <span class="kw-2">&amp;mut </span>to_store);
<span class="self">self</span>.storage.insert(String::from(username), to_store);
}
<span class="kw">pub fn </span>verify_password(<span class="kw-2">&amp;</span><span class="self">self</span>, username: <span class="kw-2">&amp;</span>str, attempted_password: <span class="kw-2">&amp;</span>str)
-&gt; <span class="prelude-ty">Result</span>&lt;(), Error&gt; {
<span class="kw">match </span><span class="self">self</span>.storage.get(username) {
<span class="prelude-val">Some</span>(actual_password) =&gt; {
<span class="kw">let </span>salt = <span class="self">self</span>.salt(username);
pbkdf2::verify(PBKDF2_ALG, <span class="self">self</span>.pbkdf2_iterations, <span class="kw-2">&amp;</span>salt,
attempted_password.as_bytes(),
actual_password)
.map_err(|<span class="kw">_</span>| Error::WrongUsernameOrPassword)
},
<span class="prelude-val">None </span>=&gt; <span class="prelude-val">Err</span>(Error::WrongUsernameOrPassword)
}
}
<span class="comment">// The salt should have a user-specific component so that an attacker
// cannot crack one password for multiple users in the database. It
// should have a database-unique component so that an attacker cannot
// crack the same user's password across databases in the unfortunate
// but common case that the user has used the same password for
// multiple systems.
</span><span class="kw">fn </span>salt(<span class="kw-2">&amp;</span><span class="self">self</span>, username: <span class="kw-2">&amp;</span>str) -&gt; Vec&lt;u8&gt; {
<span class="kw">let </span><span class="kw-2">mut </span>salt = Vec::with_capacity(<span class="self">self</span>.db_salt_component.len() +
username.as_bytes().len());
salt.extend(<span class="self">self</span>.db_salt_component.as_ref());
salt.extend(username.as_bytes());
salt
}
}
<span class="kw">fn </span>main() {
<span class="comment">// Normally these parameters would be loaded from a configuration file.
</span><span class="kw">let </span><span class="kw-2">mut </span>db = PasswordDatabase {
pbkdf2_iterations: NonZeroU32::new(<span class="number">100_000</span>).unwrap(),
db_salt_component: [
<span class="comment">// This value was generated from a secure PRNG.
</span><span class="number">0xd6</span>, <span class="number">0x26</span>, <span class="number">0x98</span>, <span class="number">0xda</span>, <span class="number">0xf4</span>, <span class="number">0xdc</span>, <span class="number">0x50</span>, <span class="number">0x52</span>,
<span class="number">0x24</span>, <span class="number">0xf2</span>, <span class="number">0x27</span>, <span class="number">0xd1</span>, <span class="number">0xfe</span>, <span class="number">0x39</span>, <span class="number">0x01</span>, <span class="number">0x8a
</span>],
storage: HashMap::new(),
};
db.store_password(<span class="string">"alice"</span>, <span class="string">"@74d7]404j|W}6u"</span>);
<span class="comment">// An attempt to log in with the wrong password fails.
</span><span class="macro">assert!</span>(db.verify_password(<span class="string">"alice"</span>, <span class="string">"wrong password"</span>).is_err());
<span class="comment">// Normally there should be an expoentially-increasing delay between
// attempts to further protect against online attacks.
// An attempt to log in with the right password succeeds.
</span><span class="macro">assert!</span>(db.verify_password(<span class="string">"alice"</span>, <span class="string">"@74d7]404j|W}6u"</span>).is_ok());
}</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::pbkdf2::Algorithm">Algorithm</a></dt><dd>A PBKDF2 algorithm.</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.PBKDF2_HMAC_SHA1.html" title="static ring::pbkdf2::PBKDF2_HMAC_SHA1">PBKD<wbr>F2_<wbr>HMAC_<wbr>SHA1</a></dt><dd>PBKDF2 using HMAC-SHA1.</dd><dt><a class="static" href="static.PBKDF2_HMAC_SHA256.html" title="static ring::pbkdf2::PBKDF2_HMAC_SHA256">PBKD<wbr>F2_<wbr>HMAC_<wbr>SHA256</a></dt><dd>PBKDF2 using HMAC-SHA256.</dd><dt><a class="static" href="static.PBKDF2_HMAC_SHA384.html" title="static ring::pbkdf2::PBKDF2_HMAC_SHA384">PBKD<wbr>F2_<wbr>HMAC_<wbr>SHA384</a></dt><dd>PBKDF2 using HMAC-SHA384.</dd><dt><a class="static" href="static.PBKDF2_HMAC_SHA512.html" title="static ring::pbkdf2::PBKDF2_HMAC_SHA512">PBKD<wbr>F2_<wbr>HMAC_<wbr>SHA512</a></dt><dd>PBKDF2 using HMAC-SHA512.</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.derive.html" title="fn ring::pbkdf2::derive">derive</a></dt><dd>Fills <code>out</code> with the key derived using PBKDF2 with the given inputs.</dd><dt><a class="fn" href="fn.verify.html" title="fn ring::pbkdf2::verify">verify</a></dt><dd>Verifies that a previously-derived (e.g., using <code>derive</code>) PBKDF2 value
matches the PBKDF2 value derived from the other inputs.</dd></dl></section></div></main></body></html>