1
0
Fork 0
forked from mCaptcha/website
mcaptcha-website/api-docs/browser/0.1.0/src/mCaptcha_browser/lib.rs.html
2021-03-11 05:08:07 +00:00

114 lines
No EOL
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="Source of the Rust file `src/lib.rs`."><meta name="keywords" content="rust, rustlang, rust-lang"><title>lib.rs - source</title><link rel="stylesheet" type="text/css" href="../../normalize.css"><link rel="stylesheet" type="text/css" href="../../rustdoc.css" id="mainThemeStyle"><link rel="stylesheet" type="text/css" href="../../light.css" id="themeStyle"><link rel="stylesheet" type="text/css" href="../../dark.css" disabled ><link rel="stylesheet" type="text/css" href="../../ayu.css" disabled ><script id="default-settings"></script><script src="../../storage.js"></script><noscript><link rel="stylesheet" href="../../noscript.css"></noscript><link rel="icon" type="image/svg+xml" href="../../favicon.svg">
<link rel="alternate icon" type="image/png" href="../../favicon-16x16.png">
<link rel="alternate icon" type="image/png" href="../../favicon-32x32.png"><style type="text/css">#crate-search{background-image:url("../../down-arrow.svg");}</style></head><body class="rustdoc source"><!--[if lte IE 8]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><nav class="sidebar"><div class="sidebar-menu">&#9776;</div><a href='../../mCaptcha_browser/index.html'><div class='logo-container rust-logo'><img src='../../rust-logo.png' alt='logo'></div></a></nav><div class="theme-picker"><button id="theme-picker" aria-label="Pick another theme!" aria-haspopup="menu"><img src="../../brush.svg" width="18" alt="Pick another theme!"></button><div id="theme-choices" role="menu"></div></div><script src="../../theme.js"></script><nav class="sub"><form class="search-form"><div class="search-container"><div><select id="crate-search"><option value="All crates">All crates</option></select><input class="search-input" name="search" disabled autocomplete="off" spellcheck="false" placeholder="Click or press S to search, ? for more options…" type="search"></div><button type="button" class="help-button">?</button>
<a id="settings-menu" href="../../settings.html"><img src="../../wheel.svg" width="18" alt="Change settings"></a></div></form></nav><section id="main" class="content"><pre class="line-numbers"><span id="1"> 1</span>
<span id="2"> 2</span>
<span id="3"> 3</span>
<span id="4"> 4</span>
<span id="5"> 5</span>
<span id="6"> 6</span>
<span id="7"> 7</span>
<span id="8"> 8</span>
<span id="9"> 9</span>
<span id="10">10</span>
<span id="11">11</span>
<span id="12">12</span>
<span id="13">13</span>
<span id="14">14</span>
<span id="15">15</span>
<span id="16">16</span>
<span id="17">17</span>
<span id="18">18</span>
<span id="19">19</span>
<span id="20">20</span>
<span id="21">21</span>
<span id="22">22</span>
<span id="23">23</span>
<span id="24">24</span>
<span id="25">25</span>
<span id="26">26</span>
<span id="27">27</span>
<span id="28">28</span>
<span id="29">29</span>
<span id="30">30</span>
<span id="31">31</span>
<span id="32">32</span>
<span id="33">33</span>
<span id="34">34</span>
<span id="35">35</span>
<span id="36">36</span>
<span id="37">37</span>
<span id="38">38</span>
<span id="39">39</span>
<span id="40">40</span>
<span id="41">41</span>
<span id="42">42</span>
<span id="43">43</span>
<span id="44">44</span>
<span id="45">45</span>
<span id="46">46</span>
<span id="47">47</span>
<span id="48">48</span>
<span id="49">49</span>
<span id="50">50</span>
<span id="51">51</span>
<span id="52">52</span>
<span id="53">53</span>
<span id="54">54</span>
</pre><div class="example-wrap"><pre class="rust ">
<span class="doccomment">//! mCaptcha is a proof of work based Denaial-of-Service attack protection system.</span>
<span class="doccomment">//! This is is a WASM library that you can embed in your frontend code to protect your</span>
<span class="doccomment">//! service.</span>
<span class="doccomment">//!</span>
<span class="doccomment">//! A commercial managed solution is in the works but I&#39;d much rather prefer</span>
<span class="doccomment">//! folks host their own instances as it will make the more decentralized and free.</span>
<span class="doccomment">//!</span>
<span class="doccomment">//! ## Workflow:</span>
<span class="doccomment">//! mCaptcha workflow in the frontend is simple.</span>
<span class="doccomment">//! 1. Call service to get a proof of work(PoW) configuration</span>
<span class="doccomment">//! 2. Call into mCaptcha to get PoW</span>
<span class="doccomment">//! 3. Send PoW to your service</span>
<span class="kw">use</span> <span class="ident">wasm_bindgen</span>::<span class="ident">prelude</span>::<span class="kw-2">*</span>;
<span class="comment">// When the `wee_alloc` feature is enabled, use `wee_alloc` as the global</span>
<span class="comment">// allocator.</span>
<span class="attribute">#[<span class="ident">cfg</span>(<span class="ident">feature</span> <span class="op">=</span> <span class="string">&quot;wee_alloc&quot;</span>)]</span>
<span class="attribute">#[<span class="ident">global_allocator</span>]</span>
<span class="kw">static</span> <span class="ident">ALLOC</span>: <span class="ident">wee_alloc</span>::<span class="ident">WeeAlloc</span> <span class="op">=</span> <span class="ident">wee_alloc</span>::<span class="ident">WeeAlloc</span>::<span class="ident">INIT</span>;
<span class="kw">use</span> <span class="ident">pow_sha256</span>::<span class="ident">PoW</span>;
<span class="attribute">#[<span class="ident">wasm_bindgen</span>]</span>
<span class="kw">pub</span> <span class="kw">fn</span> <span class="ident">gen_pow</span>(<span class="ident">difficulty_factor</span>: <span class="ident">u32</span>, <span class="ident">secret</span>: <span class="ident">String</span>) <span class="op">-</span><span class="op">&gt;</span> <span class="ident">String</span> {
<span class="kw">let</span> <span class="ident">difficulty</span> <span class="op">=</span> <span class="ident">u128</span>::<span class="ident">max_value</span>() <span class="op">-</span> <span class="ident">u128</span>::<span class="ident">max_value</span>() <span class="op">/</span> <span class="ident">difficulty_factor</span> <span class="kw">as</span> <span class="ident">u128</span>;
<span class="kw">let</span> <span class="ident">a</span> <span class="op">=</span> <span class="ident">PoW</span>::<span class="ident">prove_work</span>(<span class="kw-2">&amp;</span><span class="ident">secret</span>.<span class="ident">as_bytes</span>().<span class="ident">to_vec</span>(), <span class="ident">difficulty</span>).<span class="ident">unwrap</span>();
<span class="kw">let</span> <span class="ident">payload</span> <span class="op">=</span> <span class="ident">serde_json</span>::<span class="ident">to_string</span>(<span class="kw-2">&amp;</span><span class="ident">a</span>).<span class="ident">unwrap</span>();
<span class="ident">payload</span>
}
<span class="attribute">#[<span class="ident">cfg</span>(<span class="ident">test</span>)]</span>
<span class="kw">mod</span> <span class="ident">tests</span> {
<span class="kw">use</span> <span class="kw">super</span>::<span class="kw-2">*</span>;
<span class="attribute">#[<span class="ident">test</span>]</span>
<span class="kw">fn</span> <span class="ident">it_works</span>() {
<span class="kw">let</span> <span class="ident">payload</span> <span class="op">=</span> <span class="ident">gen_pow</span>(<span class="number">500</span>, <span class="string">&quot;MFsqLMZId629Dh2hrtux2Qdn3gBzCaSt&quot;</span>.<span class="ident">into</span>());
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="string">&quot;{\&quot;nonce\&quot;:312,\&quot;result\&quot;:\&quot;340175381422106372296624206295814425082\&quot;,\&quot;_spook\&quot;:null}&quot;</span>, <span class="kw-2">&amp;</span><span class="ident">payload</span> );
<span class="kw">let</span> <span class="ident">payload</span> <span class="op">=</span> <span class="ident">gen_pow</span>(<span class="number">1_000</span>, <span class="string">&quot;MFsqLMZId629Dh2hrtux2Qdn3gBzCaSt&quot;</span>.<span class="ident">into</span>());
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="string">&quot;{\&quot;nonce\&quot;:312,\&quot;result\&quot;:\&quot;340175381422106372296624206295814425082\&quot;,\&quot;_spook\&quot;:null}&quot;</span>, <span class="kw-2">&amp;</span><span class="ident">payload</span>);
<span class="kw">let</span> <span class="ident">payload</span> <span class="op">=</span> <span class="ident">gen_pow</span>(<span class="number">2_000</span>, <span class="string">&quot;MFsqLMZId629Dh2hrtux2Qdn3gBzCaSt&quot;</span>.<span class="ident">into</span>());
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="kw-2">&amp;</span><span class="ident">payload</span>, <span class="string">&quot;{\&quot;nonce\&quot;:312,\&quot;result\&quot;:\&quot;340175381422106372296624206295814425082\&quot;,\&quot;_spook\&quot;:null}&quot;</span>);
<span class="kw">let</span> <span class="ident">payload</span> <span class="op">=</span> <span class="ident">gen_pow</span>(<span class="number">100_000</span>, <span class="string">&quot;MFsqLMZId629Dh2hrtux2Qdn3gBzCaSt&quot;</span>.<span class="ident">into</span>());
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="kw-2">&amp;</span><span class="ident">payload</span>, <span class="string">&quot;{\&quot;nonce\&quot;:59930,\&quot;result\&quot;:\&quot;340281433562218714678373578791487113813\&quot;,\&quot;_spook\&quot;:null}&quot;</span>);
<span class="kw">let</span> <span class="ident">payload</span> <span class="op">=</span> <span class="ident">gen_pow</span>(<span class="number">1_000_000</span>, <span class="string">&quot;MFsqLMZId629Dh2hrtux2Qdn3gBzCaSt&quot;</span>.<span class="ident">into</span>());
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="kw-2">&amp;</span><span class="ident">payload</span>,<span class="string">&quot;{\&quot;nonce\&quot;:1902451,\&quot;result\&quot;:\&quot;340282308726676882310449308394036800665\&quot;,\&quot;_spook\&quot;:null}&quot;</span>);
}
}
</pre></div>
</section><section id="search" class="content hidden"></section><section class="footer"></section><script>window.rootPath = "../../";window.currentCrate = "mCaptcha_browser";</script><script src="../../main.js"></script><script src="../../source-script.js"></script><script src="../../source-files.js"></script><script defer src="../../search-index.js"></script></body></html>