<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-5">import</span><spanclass="hl-1"> {</span><br/><spanclass="hl-1"></span><spanclass="hl-3">createDecoder</span><spanclass="hl-1">,</span><br/><spanclass="hl-1"></span><spanclass="hl-3">createEncoder</span><spanclass="hl-1">,</span><br/><spanclass="hl-1"></span><spanclass="hl-3">generateSymmetricKey</span><br/><spanclass="hl-1">} </span><spanclass="hl-5">from</span><spanclass="hl-1"></span><spanclass="hl-2">"@waku/message-encryption/symmetric"</span><spanclass="hl-1">;</span><br/><br/><spanclass="hl-4">// Generate a random key</span><br/><spanclass="hl-6">const</span><spanclass="hl-1"></span><spanclass="hl-9">key</span><spanclass="hl-1"> = </span><spanclass="hl-0">generateSymmetricKey</span><spanclass="hl-1">();</span><br/><br/><spanclass="hl-4">// To send messages, create an encoder</span><br/><spanclass="hl-6">const</span><spanclass="hl-1"></span><spanclass="hl-9">encoder</span><spanclass="hl-1"> = </span><spanclass="hl-0">createEncoder</span><spanclass="hl-1">(</span><spanclass="hl-3">contentTopic</span><spanclass="hl-1">, </span><spanclass="hl-3">key</span><spanclass="hl-1">);</span><br/><br/><spanclass="hl-4">// For example</span><br/><spanclass="hl-3">waku</span><spanclass="hl-1">.</span><spanclass="hl-3">lightPush</span><spanclass="hl-1">.</span><spanclass="hl-0">push</span><spanclass="hl-1">(</span><spanclass="hl-3">encoder</span><spanclass="hl-1">, { </span><spanclass="hl-3">payload</span><spanclass="hl-1"> });</span><br/><br/><spanclass="hl-4">// To receive messages, create a decoder</span><br/><spanclass="hl-6">const</span><spanclass="hl-1"></span><spanclass="hl-9">decoder</span><spanclass="hl-1"> = </span><spanclass="hl-0">createDecoder</span><spanclass="hl-1">(</span><spanclass="hl-3">contentTopic</span><spanclass="hl-1">, </span><spanclass="hl-3">key</span><spanclass="hl-1">);</span><br/><br/><spanclass="hl-4">// For example</span><br/><spanclass="hl-5">await</span><spanclass="hl-1"></span><spanclass="hl-3">waku</span><spanclass="hl-1">.</span><spanclass="hl-3">store</span><spanclass="hl-1">.</span><spanclass="hl-0">queryOrderedCallback</span><spanclass="hl-1">([</span><spanclass="hl-3">decoder</span><spanclass="hl-1">], (</span><spanclass="hl-3">msg</span><spanclass="hl-1">) </span><spanclass="hl-6">=></span><spanclass="hl-1"> {</span><br/><spanclass="hl-1"></span><spanclass="hl-4">// ...</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-5">import</span><spanclass="hl-1"> {</span><br/><spanclass="hl-1"></span><spanclass="hl-3">createDecoder</span><spanclass="hl-1">,</span><br/><spanclass="hl-1"></span><spanclass="hl-3">createEncoder</span><spanclass="hl-1">,</span><br/><spanclass="hl-1"></span><spanclass="hl-3">generatePrivateKey</span><spanclass="hl-1">,</span><br/><spanclass="hl-1"></span><spanclass="hl-3">getPublicKey</span><br/><spanclass="hl-1">} </span><spanclass="hl-5">from</span><spanclass="hl-1"></span><spanclass="hl-2">"@waku/message-encryption/ecies"</span><spanclass="hl-1">;</span><br/><br/><spanclass="hl-4">// Generate a random private key</span><br/><spanclass="hl-6">const</span><spanclass="hl-1"></span><spanclass="hl-9">privateKey</span><spanclass="hl-1"> = </span><spanclass="hl-0">generatePrivateKey</span><spanclass="hl-1">();</span><br/><br/><spanclass="hl-4">// Keep the private key secure, provide the public key to the sender</span><br/><spanclass="hl-6">const</span><spanclass="hl-1"></span><spanclass="hl-9">publicKey</span><spanclass="hl-1"> = </span><spanclass="hl-0">getPublicKey</span><spanclass="hl-1">(</span><spanclass="hl-3">privateKey</span><spanclass="hl-1">);</span><br/><br/><spanclass="hl-4">// To send messages, create an encoder</span><br/><spanclass="hl-6">const</span><spanclass="hl-1"></span><spanclass="hl-9">encoder</span><spanclass="hl-1"> = </span><spanclass="hl-0">createEncoder</span><spanclass="hl-1">(</span><spanclass="hl-3">contentTopic</span><spanclass="hl-1">, </span><spanclass="hl-3">publicKey</span><spanclass="hl-1">);</span><br/><br/><spanclass="hl-4">// For example</span><br/><spanclass="hl-3">waku</span><spanclass="hl-1">.</span><spanclass="hl-3">lightPush</span><spanclass="hl-1">.</span><spanclass="hl-0">push</span><spanclass="hl-1">(</span><spanclass="hl-3">encoder</span><spanclass="hl-1">, { </span><spanclass="hl-3">payload</span><spanclass="hl-1"> });</span><br/><br/><spanclass="hl-4">// To receive messages, create a decoder</span><br/><spanclass="hl-6">const</span><spanclass="hl-1"></span><spanclass="hl-9">decoder</span><spanclass="hl-1"> = </span><spanclass="hl-0">createDecoder</span><spanclass="hl-1">(</span><spanclass="hl-3">contentTopic</span><spanclass="hl-1">, </span><spanclass="hl-3">privateKey</span><spanclass="hl-1">);</span><br/><br/><spanclass="hl-4">// For example</span><br/><spanclass="hl-5">await</span><spanclass="hl-1"></span><spanclass="hl-3">waku</span><spanclass="hl-1">.</span><spanclass="hl-3">store</span><spanclass="hl-1">.</span><spanclass="hl-0">queryOrderedCallback</span><spanclass="hl-1">([</span><spanclass="hl-3">decoder</span><spanclass="hl-1">], (</span><spanclass="hl-3">msg</span><spanclass="hl-1">) </span><spanclass="hl-6">=></span><spanclass="hl-1"> {</span><br/><spanclass="hl-1"></span><spanclass="hl-4">// ...</span><br/><spanclass="hl-1">});</span>