<aid="md:symmetric-encryption"class="tsd-anchor"></a><h2><ahref="#md:symmetric-encryption">Symmetric Encryption</a></h2><p>Symmetric encryption uses a unique key to encrypt and decrypt messages.</p>
<pre><codeclass="language-typescript"><spanclass="hl-6">import</span><spanclass="hl-1"> {</span><br/><spanclass="hl-1"></span><spanclass="hl-8">createDecoder</span><spanclass="hl-1">,</span><br/><spanclass="hl-1"></span><spanclass="hl-8">createEncoder</span><spanclass="hl-1">,</span><br/><spanclass="hl-1"></span><spanclass="hl-8">generateSymmetricKey</span><br/><spanclass="hl-1">} </span><spanclass="hl-6">from</span><spanclass="hl-1"></span><spanclass="hl-2">"@waku/message-encryption/symmetric"</span><spanclass="hl-1">;</span><br/><br/><spanclass="hl-9">// Generate a random key</span><br/><spanclass="hl-3">const</span><spanclass="hl-1"></span><spanclass="hl-7">key</span><spanclass="hl-1"> = </span><spanclass="hl-0">generateSymmetricKey</span><spanclass="hl-1">();</span><br/><br/><spanclass="hl-9">// To send messages, create an encoder</span><br/><spanclass="hl-3">const</span><spanclass="hl-1"></span><spanclass="hl-7">encoder</span><spanclass="hl-1"> = </span><spanclass="hl-0">createEncoder</span><spanclass="hl-1">(</span><spanclass="hl-8">contentTopic</span><spanclass="hl-1">, </span><spanclass="hl-8">key</span><spanclass="hl-1">);</span><br/><br/><spanclass="hl-9">// For example</span><br/><spanclass="hl-8">waku</span><spanclass="hl-1">.</span><spanclass="hl-8">lightPush</span><spanclass="hl-1">.</span><spanclass="hl-0">push</span><spanclass="hl-1">(</span><spanclass="hl-8">encoder</span><spanclass="hl-1">, { </span><spanclass="hl-8">payload</span><spanclass="hl-1"> });</span><br/><br/><spanclass="hl-9">// To receive messages, create a decoder</span><br/><spanclass="hl-3">const</span><spanclass="hl-1"></span><spanclass="hl-7">decoder</span><spanclass="hl-1"> = </span><spanclass="hl-0">createDecoder</span><spanclass="hl-1">(</span><spanclass="hl-8">contentTopic</span><spanclass="hl-1">, </span><spanclass="hl-8">key</span><spanclass="hl-1">);</span><br/><br/><spanclass="hl-9">// For example</span><br/><spanclass="hl-6">await</span><spanclass="hl-1"></span><spanclass="hl-8">waku</span><spanclass="hl-1">.</span><spanclass="hl-8">store</span><spanclass="hl-1">.</span><spanclass="hl-0">queryOrderedCallback</span><spanclass="hl-1">([</span><spanclass="hl-8">decoder</span><spanclass="hl-1">], (</span><spanclass="hl-8">msg</span><spanclass="hl-1">) </span><spanclass="hl-3">=></span><spanclass="hl-1"> {</span><br/><spanclass="hl-1"></span><spanclass="hl-9">// ...</span><br/><spanclass="hl-1">});</span>
<aid="md:ecies-encryption"class="tsd-anchor"></a><h2><ahref="#md:ecies-encryption">ECIES Encryption</a></h2><p>ECIES encryption enables encryption for a public key and decryption using a private key.</p>
<pre><codeclass="language-typescript"><spanclass="hl-6">import</span><spanclass="hl-1"> {</span><br/><spanclass="hl-1"></span><spanclass="hl-8">createDecoder</span><spanclass="hl-1">,</span><br/><spanclass="hl-1"></span><spanclass="hl-8">createEncoder</span><spanclass="hl-1">,</span><br/><spanclass="hl-1"></span><spanclass="hl-8">generatePrivateKey</span><spanclass="hl-1">,</span><br/><spanclass="hl-1"></span><spanclass="hl-8">getPublicKey</span><br/><spanclass="hl-1">} </span><spanclass="hl-6">from</span><spanclass="hl-1"></span><spanclass="hl-2">"@waku/message-encryption/ecies"</span><spanclass="hl-1">;</span><br/><br/><spanclass="hl-9">// Generate a random private key</span><br/><spanclass="hl-3">const</span><spanclass="hl-1"></span><spanclass="hl-7">privateKey</span><spanclass="hl-1"> = </span><spanclass="hl-0">generatePrivateKey</span><spanclass="hl-1">();</span><br/><br/><spanclass="hl-9">// Keep the private key secure, provide the public key to the sender</span><br/><spanclass="hl-3">const</span><spanclass="hl-1"></span><spanclass="hl-7">publicKey</span><spanclass="hl-1"> = </span><spanclass="hl-0">getPublicKey</span><spanclass="hl-1">(</span><spanclass="hl-8">privateKey</span><spanclass="hl-1">);</span><br/><br/><spanclass="hl-9">// To send messages, create an encoder</span><br/><spanclass="hl-3">const</span><spanclass="hl-1"></span><spanclass="hl-7">encoder</span><spanclass="hl-1"> = </span><spanclass="hl-0">createEncoder</span><spanclass="hl-1">(</span><spanclass="hl-8">contentTopic</span><spanclass="hl-1">, </span><spanclass="hl-8">publicKey</span><spanclass="hl-1">);</span><br/><br/><spanclass="hl-9">// For example</span><br/><spanclass="hl-8">waku</span><spanclass="hl-1">.</span><spanclass="hl-8">lightPush</span><spanclass="hl-1">.</span><spanclass="hl-0">push</span><spanclass="hl-1">(</span><spanclass="hl-8">encoder</span><spanclass="hl-1">, { </span><spanclass="hl-8">payload</span><spanclass="hl-1"> });</span><br/><br/><spanclass="hl-9">// To receive messages, create a decoder</span><br/><spanclass="hl-3">const</span><spanclass="hl-1"></span><spanclass="hl-7">decoder</span><spanclass="hl-1"> = </span><spanclass="hl-0">createDecoder</span><spanclass="hl-1">(</span><spanclass="hl-8">contentTopic</span><spanclass="hl-1">, </span><spanclass="hl-8">privateKey</span><spanclass="hl-1">);</span><br/><br/><spanclass="hl-9">// For example</span><br/><spanclass="hl-6">await</span><spanclass="hl-1"></span><spanclass="hl-8">waku</span><spanclass="hl-1">.</span><spanclass="hl-8">store</span><spanclass="hl-1">.</span><spanclass="hl-0">queryOrderedCallback</span><spanclass="hl-1">([</span><spanclass="hl-8">decoder</span><spanclass="hl-1">], (</span><spanclass="hl-8">msg</span><spanclass="hl-1">) </span><spanclass="hl-3">=></span><spanclass="hl-1"> {</span><br/><spanclass="hl-1"></span><spanclass="hl-9">// ...</span><br/><spanclass="hl-1">});</span>