<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Aaron</title>
    <description>The latest articles on DEV Community by Aaron (@aaronarney).</description>
    <link>https://dev.to/aaronarney</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F395767%2Fb6dae552-f88f-47b8-ab80-2e92d78db2c8.jpg</url>
      <title>DEV Community: Aaron</title>
      <link>https://dev.to/aaronarney</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/aaronarney"/>
    <language>en</language>
    <item>
      <title>Reading Serialized PHP</title>
      <dc:creator>Aaron</dc:creator>
      <pubDate>Thu, 10 Sep 2020 19:36:20 +0000</pubDate>
      <link>https://dev.to/aaronarney/reading-serialized-php-4dga</link>
      <guid>https://dev.to/aaronarney/reading-serialized-php-4dga</guid>
      <description>&lt;p&gt;Serializing data is simply converting a value into a string. Imagine you had an array in PHP that you wanted to serialize, it would look something like the following...&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nv"&gt;$arr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="s1"&gt;'one'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;'val'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s1"&gt;'two'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;'val'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;];&lt;/span&gt;

&lt;span class="nv"&gt;$serializedArray&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;serialize&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$arr&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="cd"&gt;/** 
echo $serializedArray;

output:
"a:2:{s:3:"one";s:3:"val";s:3:"two";s:3:"val";}"
**/&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's dissect it.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;a:2&lt;/code&gt; - The proceeding value is an array of length &lt;code&gt;2&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;s:3&lt;/code&gt; - This item is a string of length &lt;code&gt;3&lt;/code&gt; with a value of &lt;code&gt;val&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;s:3&lt;/code&gt; - This item is also a string of length 3 and value of &lt;code&gt;val&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You can imagine how different data types are represented in this fashion.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;string&lt;/code&gt; - &lt;code&gt;s:length:value&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;int&lt;/code&gt; - &lt;code&gt;i:value&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;bool&lt;/code&gt; - &lt;code&gt;b:value&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;array&lt;/code&gt; - &lt;code&gt;a:size:{key definition;value definition;}&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;object&lt;/code&gt; - &lt;code&gt;O:strlen(class name):object name:object size:{s:length:property name:property definition;(repeated per property)}&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Pretty easy to understand once you know the format.&lt;/p&gt;




&lt;p&gt;Resources:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.php.net/manual/en/function.serialize.php"&gt;Serialize - PHP Docs&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>php</category>
    </item>
    <item>
      <title>Critical Path Rendering</title>
      <dc:creator>Aaron</dc:creator>
      <pubDate>Fri, 14 Aug 2020 14:49:31 +0000</pubDate>
      <link>https://dev.to/aaronarney/critical-path-rendering-2fj5</link>
      <guid>https://dev.to/aaronarney/critical-path-rendering-2fj5</guid>
      <description>&lt;h1&gt;
  
  
  Critical Rendering Path
&lt;/h1&gt;

&lt;p&gt;CRP or Critical Rendering Path is an algorithm of sorts that browsers use to display a webpage. When you use your browser to navigate to a webpage, a series of steps have to occur before you will see anything populate the screen. You can think of CRP as a linear top-down process in which the browser reads HTML one line at a time and forms the webpage as it goes.&lt;/p&gt;

&lt;p&gt;The CRP process has several phases;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;DOM (Document Object Model)&lt;/li&gt;
&lt;li&gt;CSSOM (Cascading Style Sheet Object Model)&lt;/li&gt;
&lt;li&gt;Render Tree &lt;/li&gt;
&lt;li&gt;Layout&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In each of these phases a different set of steps occur. Let's go over them individually.&lt;/p&gt;

&lt;h2&gt;
  
  
  Document Object Model (DOM)
&lt;/h2&gt;

&lt;p&gt;The Document Object Model is a tree structure that represents the HTML on a webpage, by converting each HTML entity into a node object. When each HTML entity is encountered a &lt;code&gt;startTag&lt;/code&gt; is generated. The parser then moves to the next token to see if there are any children of the entity. If there is a new node and &lt;code&gt;startTag&lt;/code&gt; is generated, and so on and so forth until the end of the entity is found. At that point an &lt;code&gt;endTag&lt;/code&gt; is created and that node is closed. These series of startTags and endTags form the tree that makes up the DOM.&lt;/p&gt;

&lt;p&gt;As a simplified example, it may help to visualize it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;div&amp;gt;
    &amp;lt;div&amp;gt;
        &amp;lt;a href="#"&amp;gt;
            Some Link
        &amp;lt;/a&amp;gt;
    &amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Could convert to something like the following. Note that this is a JavaScript flavor and probably not the best way or most accurate way to represent this information.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;startTag('div')
  .startTag('div')
    .startTag('a')
      .attribute('href', '#')
      .content('Some Link')
    .endTag('a')
  .endTag('div')
.endTag('div')
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The more HTML on a page the longer this step takes. However, don't be fooled into thinking that reducing your HTML by a few or even dozens of elements will make a difference! Browsers are really fast and performance gains from such optimizations are likely to not result in anything significant.&lt;/p&gt;

&lt;h2&gt;
  
  
  CSS Object Model (CSSOM)
&lt;/h2&gt;

&lt;p&gt;The CSS object model works similar to the DOM except this time CSS is parsed and applied to nodes. When each CSS rule is encountered, the browser performs a lookup on the DOM to make a match to an element.&lt;/p&gt;

&lt;p&gt;Both HTML and CSS are render blocking resources. However, using through the use of media queries you can achieve non-render blocking CSS. This is because the CSS is ignored if the viewport size doesn't match the media query, essentially deferring the application of the CSS.&lt;/p&gt;

&lt;h2&gt;
  
  
  Render Tree
&lt;/h2&gt;

&lt;p&gt;The Render Tree is the phase in which the DOM and CSSOM are combined. Each node in the DOM has it's position and size computed. This phase is when the layout is created. Therefore, any time you modify the DOM such as adding or removing nodes, this phase gets re-triggered.&lt;/p&gt;

&lt;p&gt;Here are some &lt;a href="https://docs.google.com/spreadsheets/u/0/d/1Hvi0nu2wG3oQ51XRHtMv-A_ZlidnwUYwgQsPQUg1R2s/pub?single=true&amp;amp;gid=0&amp;amp;output=html"&gt;common properties that trigger layout&lt;/a&gt;.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;width&lt;/td&gt;
&lt;td&gt;height&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;padding&lt;/td&gt;
&lt;td&gt;margin&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;display&lt;/td&gt;
&lt;td&gt;border-width&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;border&lt;/td&gt;
&lt;td&gt;top&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;position&lt;/td&gt;
&lt;td&gt;font-size&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;float&lt;/td&gt;
&lt;td&gt;text-align&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;overflow-y&lt;/td&gt;
&lt;td&gt;font-weight&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;overflow&lt;/td&gt;
&lt;td&gt;left&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;font-family&lt;/td&gt;
&lt;td&gt;line-height&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;vertical-align&lt;/td&gt;
&lt;td&gt;right&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;clear&lt;/td&gt;
&lt;td&gt;white-space&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;bottom&lt;/td&gt;
&lt;td&gt;min-height&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  Painting
&lt;/h2&gt;

&lt;p&gt;Finally at the end of the pipeline, the painting phase is when the render tree elements are converted into pixels on the screen.&lt;/p&gt;

&lt;p&gt;The complete layout is generated and painted. Then for each node, lays out the elements on the page with their given dimensions and positions. Lastly it applies the remaining CSS to the element.&lt;/p&gt;

&lt;p&gt;Just like with the layout there are CSS properties that can re-trigger paint.&lt;/p&gt;

&lt;p&gt;The &lt;a href="https://docs.google.com/spreadsheets/u/0/d/1Hvi0nu2wG3oQ51XRHtMv-A_ZlidnwUYwgQsPQUg1R2s/pub?single=true&amp;amp;gid=0&amp;amp;output=html"&gt;common properties that trigger a re-paint&lt;/a&gt;.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;color&lt;/td&gt;
&lt;td&gt;border-style&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;visibility&lt;/td&gt;
&lt;td&gt;background&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;text-decoration&lt;/td&gt;
&lt;td&gt;background-image&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;background-position&lt;/td&gt;
&lt;td&gt;background-repeat&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;outline-color&lt;/td&gt;
&lt;td&gt;outline&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;outline-style&lt;/td&gt;
&lt;td&gt;border-radius&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;outline-width&lt;/td&gt;
&lt;td&gt;box-shadow&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;background-size&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;As a sidenote, this is why with CSS animations it's fairly important you stick to the rules of only animating four things; position, scale, rotation and opacity. These operations allow the animation to run on the GPU instead of the browsers software rasterizer. For a more in-depth explanation of how this works, read the excellent post &lt;a href="https://www.html5rocks.com/en/tutorials/speed/high-performance-animations/"&gt;"High Performance Animations."&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;This is a nutshell version of the Critical Rendering Path. It is worth diving into how to optimize the CRP but I will save that for another post.&lt;/p&gt;

</description>
      <category>css</category>
    </item>
    <item>
      <title>How you decide what to focus on when learning something new?</title>
      <dc:creator>Aaron</dc:creator>
      <pubDate>Fri, 14 Aug 2020 12:19:28 +0000</pubDate>
      <link>https://dev.to/aaronarney/how-you-decide-what-to-focus-on-when-learning-something-new-3joc</link>
      <guid>https://dev.to/aaronarney/how-you-decide-what-to-focus-on-when-learning-something-new-3joc</guid>
      <description>&lt;p&gt;I struggle constantly with this. I want to learn everything. One day I'll wake up and decide I really want to dive into C but as I begin to prep, I talk myself out of it. The thoughts usually go something like this...&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;C would be really cool to learn&lt;/li&gt;
&lt;li&gt;I should find a YouTube video that builds a simple project&lt;/li&gt;
&lt;li&gt;(searches for a good video for 30 minutes)&lt;/li&gt;
&lt;li&gt;(finds video)&lt;/li&gt;
&lt;li&gt;(opens IDE/vim)&lt;/li&gt;
&lt;li&gt;Wait, I probably should learn something more relevant to my day job if I'm going to be spending time on this today&lt;/li&gt;
&lt;li&gt;(closes video, closes IDE)&lt;/li&gt;
&lt;li&gt;goto 1, choose different topic&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Some weekends I'll spend hours doing this with no progress. Most of the time I can convince myself that learning anything is always relevant, but it's hard some days. &lt;/p&gt;

&lt;p&gt;I think my problem boils down to something quite simple. If I don't have a clear goal or project, it's hard for me to focus.&lt;/p&gt;

&lt;p&gt;Does anyone else suffer from this problem and if so, how do you navigate it?&lt;/p&gt;

</description>
      <category>discuss</category>
      <category>help</category>
      <category>career</category>
    </item>
    <item>
      <title>TypeScript Generics Simply Put</title>
      <dc:creator>Aaron</dc:creator>
      <pubDate>Thu, 13 Aug 2020 19:32:43 +0000</pubDate>
      <link>https://dev.to/aaronarney/typescript-generics-simply-put-3i9o</link>
      <guid>https://dev.to/aaronarney/typescript-generics-simply-put-3i9o</guid>
      <description>&lt;p&gt;Generics are a really cool feature of any language that supports them. They allow you to write more abstracted code while maintaining the type safety/hinting that you CRAVE.&lt;/p&gt;

&lt;p&gt;If you've used TypeScript at all, you've likely already encountered Generics through Arrays. When you create a variable that is of type array, it looks like this.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Array&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[];&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;However, this isn't valid on its own, as TypeScript is expecting to know what type will populate this array. We denote this type using angle brackets &lt;code&gt;&amp;lt;&amp;gt;&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Array&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kr"&gt;any&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[];&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Of course, using &lt;code&gt;any&lt;/code&gt; just tells TypeScript to expect all types of data.&lt;/p&gt;

&lt;p&gt;But now let's say you are expecting to fill this array with strings so that you can call the &lt;code&gt;indexOf&lt;/code&gt; method on any element. You can change &lt;code&gt;any&lt;/code&gt; to &lt;code&gt;string&lt;/code&gt; and TypeScript will then know what methods will be available.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Array&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;some&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;strings&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nx"&gt;indexOf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;s&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Using the same angle bracket syntax, you add the Generic type to a function signature, class or interface. The convention is to use a capital T, that simply abbreviates "type." Then you typically pass this type as an argument type in a constructor, method or function.&lt;/p&gt;

&lt;p&gt;The Array interface in its simplest form could be written this way...&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kr"&gt;interface&lt;/span&gt; &lt;span class="nb"&gt;Array&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="na"&gt;arg&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;T&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nx"&gt;T&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This allows any type to be associated with an Array type. We could store Objects, Maps, Atomic, Proxy, Numbers, anything!&lt;/p&gt;

&lt;p&gt;Generics are a really powerful way of abstracting code so that it's not super specific to one type of data.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.typescriptlang.org/docs/handbook/generics.html"&gt;Read more about Generics in the TypeScript docs&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>typescript</category>
      <category>javascript</category>
    </item>
    <item>
      <title>How Cryptographic Randomness with Entropy Vaguely Works</title>
      <dc:creator>Aaron</dc:creator>
      <pubDate>Thu, 16 Jul 2020 18:06:33 +0000</pubDate>
      <link>https://dev.to/aaronarney/how-cryptographic-randomness-with-entropy-vaguely-works-2ocg</link>
      <guid>https://dev.to/aaronarney/how-cryptographic-randomness-with-entropy-vaguely-works-2ocg</guid>
      <description>&lt;p&gt;Random number generators are everywhere in computing. From hardware, to operating systems, kernels, games, you name it. You often hear how pseudo-random number generators (PRNG) like those typically used in programming languages (like JavaScript's Math.random for example) are not cryptographically secure (CSPRNG). But what does that mean?&lt;/p&gt;

&lt;p&gt;First you need to understand how PRNG's work. As &lt;a href="https://v8.dev/blog/math-random"&gt;Yang Guo elegantly puts it&lt;/a&gt;...&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"As with all PRNGs, the random number is derived from an internal state, which is altered by a fixed algorithm for every new random number. So for a given initial state, the sequence of random numbers is deterministic. Since the bit size n of the internal state is limited, the numbers that a PRNG generates will eventually repeat themselves."&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In his quote, deterministic means that given some specific input you can always expect the same output. You can almost think of it as a pure function.&lt;/p&gt;

&lt;p&gt;In the V8 implementation of the &lt;code&gt;xorshift128+&lt;/code&gt; algorithm you can see the underlying logic of &lt;code&gt;Math.random()&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="kt"&gt;uint64_t&lt;/span&gt; &lt;span class="n"&gt;state0&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kt"&gt;uint64_t&lt;/span&gt; &lt;span class="n"&gt;state1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kt"&gt;uint64_t&lt;/span&gt; &lt;span class="nf"&gt;xorshift128plus&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kt"&gt;uint64_t&lt;/span&gt; &lt;span class="n"&gt;s1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;state0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kt"&gt;uint64_t&lt;/span&gt; &lt;span class="n"&gt;s0&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;state1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;state0&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;s0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;s1&lt;/span&gt; &lt;span class="o"&gt;^=&lt;/span&gt; &lt;span class="n"&gt;s1&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;23&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;s1&lt;/span&gt; &lt;span class="o"&gt;^=&lt;/span&gt; &lt;span class="n"&gt;s1&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;17&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;s1&lt;/span&gt; &lt;span class="o"&gt;^=&lt;/span&gt; &lt;span class="n"&gt;s0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;s1&lt;/span&gt; &lt;span class="o"&gt;^=&lt;/span&gt; &lt;span class="n"&gt;s0&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;26&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;state1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;s1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;state0&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;state1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Even if you don't know C this example should be pretty clear about two things.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The state is global.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;n&lt;/code&gt; can never be larger than the largest bit size the platform supports. For C &lt;code&gt;uint64_t&lt;/code&gt; that would be &lt;code&gt;9223372036854775807&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Cryptographically Secure
&lt;/h2&gt;

&lt;p&gt;Now it's very easy to see why a PRNG would be a non-starter for cryptographic applications. Crytographically secure means that a random number you receive has been generated using a non-deterministic algorithm/function. If the previous example won't work, then how do we create these CSPRNG's?&lt;/p&gt;

&lt;p&gt;Enter entropy. Entropy has somewhat ambiguous meaning in our field; it can describe how software projects begin to rot as more complexity is introduced over time. In the context of this article however, it essentially is the environmental noise in device drivers.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--DgG4vv9j--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/wv55gwn1ckc8or4j4xsp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--DgG4vv9j--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/wv55gwn1ckc8or4j4xsp.png" alt="Entropy Flow" width="433" height="481"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In Linux for example, the noise is captured into two files &lt;code&gt;/dev/random&lt;/code&gt; and &lt;code&gt;/dev/urandom&lt;/code&gt;. These files can then be accessed and consumed by the CSPRNG. When this data is consumed it is removed from the pool, that way the next number generated isn't using the same seed. These files naturally replenish themselves but it is important to note that since these are pools after all, they can be depleted if there has been no activity to replenish them.&lt;/p&gt;

&lt;p&gt;I don't want to divert into a whole deep dive into these pools but an interesting side note is that on Linux servers this can be a problem, since there are usually no mice/keyboards directly hooked up to the machine, thus reducing the amount of noise it can generate.&lt;/p&gt;

&lt;p&gt;Now you may be wondering, if &lt;code&gt;Math.random&lt;/code&gt; is insecure why don't we just use the &lt;code&gt;Cryptography API&lt;/code&gt; for everything instead? The answer is, like everything, there are trade-offs and in most cases you don't need a CSPRNG.&lt;/p&gt;

&lt;p&gt;The primary trade-off is that this method is slower than using a PRNG, and in most cases that's usually what we need anyway. Here is a &lt;a href="https://jsperf.com/math-random-vs-crypto-getrandomvalues/5"&gt;benchmark showing the stark differences in the speed of these functions&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--MWFBpkmp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/c8fap30158xw6i94c9ta.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--MWFBpkmp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/c8fap30158xw6i94c9ta.png" alt="Random Number Generation Benchmarks" width="741" height="313"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/Web_Crypto_API"&gt;MDN docs also make it perfectly clear&lt;/a&gt; that unless you know exactly what you are needing and how to do it, you probably shouldn't be using the Cryptography API as it is easy to get wrong. That shouldn't stop you from experimenting and learning about it though, just be careful!&lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;Really there is no conclusion here, I had randomly (pun intended) stumbled across entropy and found this whole dive into it really interesting. I'm sure there is something I missed or maybe got some details wrong so your feedback is welcome.&lt;/p&gt;

&lt;p&gt;Special shout-out to my co-worker David Pagan for the edits/feedback.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>cryptography</category>
    </item>
  </channel>
</rss>
