<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-4">createDecoder</span><spanclass="hl-1">,</span><br/><spanclass="hl-1"></span><spanclass="hl-4">createEncoder</span><spanclass="hl-1">,</span><br/><spanclass="hl-1"></span><spanclass="hl-4">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-7">// Generate a random key</span><br/><spanclass="hl-3">const</span><spanclass="hl-1"></span><spanclass="hl-5">key</span><spanclass="hl-1"> = </span><spanclass="hl-0">generateSymmetricKey</span><spanclass="hl-1">();</span><br/><br/><spanclass="hl-7">// To send messages, create an encoder</span><br/><spanclass="hl-3">const</span><spanclass="hl-1"></span><spanclass="hl-5">encoder</span><spanclass="hl-1"> = </span><spanclass="hl-0">createEncoder</span><spanclass="hl-1">(</span><spanclass="hl-4">contentTopic</span><spanclass="hl-1">, </span><spanclass="hl-4">key</span><spanclass="hl-1">);</span><br/><br/><spanclass="hl-7">// For example</span><br/><spanclass="hl-4">waku</span><spanclass="hl-1">.</span><spanclass="hl-4">lightPush</span><spanclass="hl-1">.</span><spanclass="hl-0">push</span><spanclass="hl-1">(</span><spanclass="hl-4">encoder</span><spanclass="hl-1">, { </span><spanclass="hl-4">payload</span><spanclass="hl-1"> });</span><br/><br/><spanclass="hl-7">// To receive messages, create a decoder</span><br/><spanclass="hl-3">const</span><spanclass="hl-1"></span><spanclass="hl-5">decoder</span><spanclass="hl-1"> = </span><spanclass="hl-0">createDecoder</span><spanclass="hl-1">(</span><spanclass="hl-4">contentTopic</span><spanclass="hl-1">, </span><spanclass="hl-4">key</span><spanclass="hl-1">);</span><br/><br/><spanclass="hl-7">// For example</span><br/><spanclass="hl-6">await</span><spanclass="hl-1"></span><spanclass="hl-4">waku</span><spanclass="hl-1">.</span><spanclass="hl-4">store</span><spanclass="hl-1">.</span><spanclass="hl-0">queryOrderedCallback</span><spanclass="hl-1">([</span><spanclass="hl-4">decoder</span><spanclass="hl-1">], (</span><spanclass="hl-4">msg</span><spanclass="hl-1">) </span><spanclass="hl-3">=></span><spanclass="hl-1"> {</span><br/><spanclass="hl-1"></span><spanclass="hl-7">// ...</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-4">createDecoder</span><spanclass="hl-1">,</span><br/><spanclass="hl-1"></span><spanclass="hl-4">createEncoder</span><spanclass="hl-1">,</span><br/><spanclass="hl-1"></span><spanclass="hl-4">generatePrivateKey</span><spanclass="hl-1">,</span><br/><spanclass="hl-1"></span><spanclass="hl-4">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-7">// Generate a random private key</span><br/><spanclass="hl-3">const</span><spanclass="hl-1"></span><spanclass="hl-5">privateKey</span><spanclass="hl-1"> = </span><spanclass="hl-0">generatePrivateKey</span><spanclass="hl-1">();</span><br/><br/><spanclass="hl-7">// 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-5">publicKey</span><spanclass="hl-1"> = </span><spanclass="hl-0">getPublicKey</span><spanclass="hl-1">(</span><spanclass="hl-4">privateKey</span><spanclass="hl-1">);</span><br/><br/><spanclass="hl-7">// To send messages, create an encoder</span><br/><spanclass="hl-3">const</span><spanclass="hl-1"></span><spanclass="hl-5">encoder</span><spanclass="hl-1"> = </span><spanclass="hl-0">createEncoder</span><spanclass="hl-1">(</span><spanclass="hl-4">contentTopic</span><spanclass="hl-1">, </span><spanclass="hl-4">publicKey</span><spanclass="hl-1">);</span><br/><br/><spanclass="hl-7">// For example</span><br/><spanclass="hl-4">waku</span><spanclass="hl-1">.</span><spanclass="hl-4">lightPush</span><spanclass="hl-1">.</span><spanclass="hl-0">push</span><spanclass="hl-1">(</span><spanclass="hl-4">encoder</span><spanclass="hl-1">, { </span><spanclass="hl-4">payload</span><spanclass="hl-1"> });</span><br/><br/><spanclass="hl-7">// To receive messages, create a decoder</span><br/><spanclass="hl-3">const</span><spanclass="hl-1"></span><spanclass="hl-5">decoder</span><spanclass="hl-1"> = </span><spanclass="hl-0">createDecoder</span><spanclass="hl-1">(</span><spanclass="hl-4">contentTopic</span><spanclass="hl-1">, </span><spanclass="hl-4">privateKey</span><spanclass="hl-1">);</span><br/><br/><spanclass="hl-7">// For example</span><br/><spanclass="hl-6">await</span><spanclass="hl-1"></span><spanclass="hl-4">waku</span><spanclass="hl-1">.</span><spanclass="hl-4">store</span><spanclass="hl-1">.</span><spanclass="hl-0">queryOrderedCallback</span><spanclass="hl-1">([</span><spanclass="hl-4">decoder</span><spanclass="hl-1">], (</span><spanclass="hl-4">msg</span><spanclass="hl-1">) </span><spanclass="hl-3">=></span><spanclass="hl-1"> {</span><br/><spanclass="hl-1"></span><spanclass="hl-7">// ...</span><br/><spanclass="hl-1">});</span>