{"id":8203,"date":"2021-09-01T21:42:00","date_gmt":"2021-09-02T02:42:00","guid":{"rendered":"https:\/\/www.rushworth.us\/lisa\/?p=8203"},"modified":"2021-09-02T15:50:55","modified_gmt":"2021-09-02T20:50:55","slug":"using-memcached-in-php","status":"publish","type":"post","link":"https:\/\/www.rushworth.us\/lisa\/?p=8203","title":{"rendered":"Using Memcached in PHP"},"content":{"rendered":"\n<p>Quick PHP code used as a proof of concept for storing credentials in memcached &#8212; cred is encrypted using libsodium before being send to memcached, and it is decrypted after being retrieved. This is done both to prevent in-memory data from being meaningful and because the PHP memcached extension doesn&#8217;t seem to support SSL communication. <\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: php; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\n&lt;?php\n\n# To generate key and nonce, use sodium_bin2hex to stash these two values\n#$sodiumKey   = random_bytes(SODIUM_CRYPTO_SECRETBOX_KEYBYTES); \/\/ 256 bit\n#$sodiumNonce = random_bytes(SODIUM_CRYPTO_SECRETBOX_NONCEBYTES); \/\/ 24 bytes\n\n# Stashed key and nonce strings\n$strSodiumKey = &#039;cdce35b57cdb25032e68eb14a33c8252507ae6ab1627c1c7fcc420894697bf3e&#039;;\n$strSodiumNonce = &#039;652e16224e38da20ea818a92feb9b927d756ade085d75dab&#039;;\n# Turn key and nonce back into binary data\n$sodiumKey = sodium_hex2bin($strSodiumKey);\n$sodiumNonce = sodium_hex2bin($strSodiumNonce);\n\n# Initiate memcached object and add sandbox server\n$memcacheD = new Memcached;\n$memcacheD-&gt;addServer(&#039;127.0.0.1&#039;,&#039;11211&#039;,1);  # add high priority weight server added to memcacheD\n\n$arrayDataToStore = array(\n    &quot;credValueGoesHere&quot;,\n        &quot;cred2&quot;,\n        &quot;cred3&quot;,\n        &quot;cred4&quot;,\n        &quot;cred5&quot;\n);\n\n# Encrypt and stash data\nfor($i = 0; $i &lt; count($arrayDataToStore); $i++){\n    usleep(100);\n    $strValue = $arrayDataToStore&#x5B;$i];\n    $strMemcachedKey = &#039;credtest&#039; . $i;\n        $strCryptedValue =  base64_encode(sodium_crypto_secretbox($strValue, $sodiumNonce, $sodiumKey));\n    $memcacheD-&gt;set($strMemcachedKey, $strCryptedValue,time()+120);\n}\n\n# Get each key and decrypt it\nfor($i = 0; $i &lt; count($arrayDataToStore); $i++){\n        $strMemcachedKey = &#039;credtest&#039;.$i;\n        $strValue = sodium_crypto_secretbox_open(base64_decode($memcacheD-&gt;get($strMemcachedKey)),$sodiumNonce, $sodiumKey);\n        echo &quot;The value on key $strMemcachedKey is: $strValue \\n&quot;;\n}\n?&gt;\n<\/pre><\/div>","protected":false},"excerpt":{"rendered":"<p>Quick PHP code used as a proof of concept for storing credentials in memcached &#8212; cred is encrypted using libsodium before being send to memcached, and it is decrypted after being retrieved. This is done both to prevent in-memory data from being meaningful and because the PHP memcached extension doesn&#8217;t seem to support SSL communication.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[33],"tags":[1452,35],"class_list":["post-8203","post","type-post","status-publish","format-standard","hentry","category-coding","tag-memcached","tag-php"],"_links":{"self":[{"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/posts\/8203","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=8203"}],"version-history":[{"count":1,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/posts\/8203\/revisions"}],"predecessor-version":[{"id":8204,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/posts\/8203\/revisions\/8204"}],"wp:attachment":[{"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=8203"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=8203"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=8203"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}