<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
 <title> web</title>
 <link href="http://reversemicrowave.me/tag/web.xml" rel="self"/>
 <link href="http://reversemicrowave.me/tag/web.html"/>
 <updated>2016-03-04T02:06:58+02:00</updated>
 <id>http://reversemicrowave.me/tag/web.html</id>
 <author>
   <name>Sergey Yavnyi</name>
 </author>
 
 <entry>
   <title>Cryptographically secure random numbers in Elm</title>
   <link href="http://reversemicrowave.me/blog/2016/03/04/elm-secure-random"/>
   <updated>2016-03-04T02:06:58+02:00</updated>
   <id>http://reversemicrowave.me/blog/2016/03/04/elm-secure-random</id>
   <content type="html">&lt;p&gt;Elm comes with a &lt;a href=&quot;http://package.elm-lang.org/packages/elm-lang/core/3.0.0/Random&quot;&gt;nice purely-functional random number generator&lt;/a&gt; in the standard library. In fact, it&amp;rsquo;s a port of the Haskell&amp;rsquo;s &lt;a href=&quot;http://hackage.haskell.org/package/random-1.0.1.1/docs/System-Random.html&quot;&gt;&lt;code&gt;System.Random&lt;/code&gt;&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Not only this random number generator implementation is deterministic (that is, given the same initial seed it will produce the same random sequence), it is also strictly &lt;a href=&quot;https://en.wikipedia.org/wiki/Pure_function&quot;&gt;purely-functional&lt;/a&gt; (just like everything in Elm, actually).&lt;/p&gt;

&lt;!-- more --&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-text&quot; data-lang=&quot;text&quot;&gt;generate : Generator a -&amp;gt; Seed -&amp;gt; (a, Seed)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Above is the signature of the &lt;a href=&quot;http://package.elm-lang.org/packages/elm-lang/core/3.0.0/Random#generate&quot;&gt;&lt;code&gt;generate&lt;/code&gt;&lt;/a&gt; function that you use to produce random values. It takes a generator (&lt;code&gt;Generator a&lt;/code&gt;) and a seed (&lt;code&gt;Seed&lt;/code&gt;), and returns a randomly generated value (&lt;code&gt;a&lt;/code&gt;) and the new &lt;code&gt;Seed&lt;/code&gt;. And if you want to generate another random value, you need to use this new &lt;code&gt;Seed&lt;/code&gt; in the next call to &lt;code&gt;generate&lt;/code&gt; – if not, you&amp;rsquo;ll get back the same number that you got the first time.&lt;/p&gt;

&lt;p&gt;So &lt;code&gt;generate&lt;/code&gt;&amp;rsquo;s type signature explicitly tells us that the random value generation is completely defined by the seed that we supply it. This means that, if we have the initial &lt;code&gt;Seed&lt;/code&gt;, we can get the whole sequence of random values.&lt;/p&gt;

&lt;p&gt;Can this kind of generator be suitable for security-sensitive applications?&lt;/p&gt;

&lt;h2&gt;Secure random numbers&lt;/h2&gt;

&lt;p&gt;Probably not.&lt;/p&gt;

&lt;p&gt;Deterministic random generation is great when you want to reproduce the application&amp;rsquo;s behavior consistently, for example in automated tests. Such generators can, of course, be useful in many other scenarios. For example, in deterministic models in game development, procedural content generation, etc.&lt;/p&gt;

&lt;p&gt;But for security-related applications it is &lt;em&gt;completely opposite&lt;/em&gt;: you definitely &lt;em&gt;don&amp;rsquo;t&lt;/em&gt; want anyone to be able to figure out your randomly generated private key. If we wanted to generate random values for cryptographic purpose, we definitely wouldn&amp;rsquo;t want the random generator to be so explicitly predictable.&lt;/p&gt;

&lt;h3&gt;The perfect generator&lt;/h3&gt;

&lt;p&gt;Surprisingly, the core Elm library doesn&amp;rsquo;t come with such a random value generator.&lt;/p&gt;

&lt;p&gt;So we&amp;rsquo;re going to try to bring it to Elm. To start simple, let&amp;rsquo;s limit to generating &lt;code&gt;Int&lt;/code&gt;s – the rest we then can easily derive later.&lt;/p&gt;

&lt;p&gt;Suppose our ideal random value generating function would have the following signature:&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-text&quot; data-lang=&quot;text&quot;&gt;getRandomInt : () -&amp;gt; Int
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;According to this type signature, we don&amp;rsquo;t want to give the generation function any context at all, and we want a random &lt;code&gt;Int&lt;/code&gt; in return. As the function doesn&amp;rsquo;t take a seed or other random generation algorithm parameters as an argument means that the function caller is not in control of
the random value generation anymore – perfect.&lt;/p&gt;

&lt;p&gt;So how does one implement such a function? Well, turns out it is impossible to do in a purely-functional way. Think about it, how would you make a random number every time if all you are given is a &lt;code&gt;()&lt;/code&gt;? Even returning just different number on each call is impossible:&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-text&quot; data-lang=&quot;text&quot;&gt;getRandomInt : () -&amp;gt; Int
getRandomInt () = ... -- what?
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Mathematically speaking, the function&amp;rsquo;s &lt;a href=&quot;https://en.wikipedia.org/wiki/Image_(mathematics)&quot;&gt;image&lt;/a&gt; can not have more elements than it&amp;rsquo;s &lt;a href=&quot;https://en.wikipedia.org/wiki/Domain_of_a_function&quot;&gt;domain&lt;/a&gt;, by definition. Domain, in our case, is the &lt;code&gt;()&lt;/code&gt; type, which is inhabited by exactly one value (or element). This means that such kind of function can only return the same value every time.&lt;/p&gt;

&lt;h3&gt;Breaking the math&lt;/h3&gt;

&lt;p&gt;So mathematically, we can not make a perfect random number generator. So what? This is not a maths class, &lt;em&gt;this is hacking&lt;/em&gt;!&lt;/p&gt;

&lt;p&gt;Given that modern browsers already provide a cryptographically secure random number generator implementation, the &lt;a href=&quot;https://developer.mozilla.org/en-US/docs/Web/API/RandomSource/getRandomValues&quot;&gt;window.crypto.getRandomValues&lt;/a&gt; function, we will only need to provide Elm bindings for it!&lt;/p&gt;

&lt;p&gt;Writing Elm bindings for raw JavaScript code &lt;a href=&quot;https://github.com/NoRedInk/take-home/wiki/Writing-your-first-Elm-Native-module&quot;&gt;is not complicated&lt;/a&gt;. In our case, it would look like this:&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span class=&quot;c1&quot;&gt;// boilerplate&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;Elm&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;Native&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;SecureRandom&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{};&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;Elm&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;Native&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;SecureRandom&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;make&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;localRuntime&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;nx&quot;&gt;localRuntime&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;Native&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;localRuntime&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;Native&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;||&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{};&lt;/span&gt;
  &lt;span class=&quot;nx&quot;&gt;localRuntime&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;Native&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;SecureRandom&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;localRuntime&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;Native&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;SecureRandom&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;||&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{};&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;localRuntime&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;Native&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;SecureRandom&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;values&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;localRuntime&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;Native&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;SecureRandom&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;values&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;// actual code&lt;/span&gt;
  &lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;crypto&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;window&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;crypto&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;localRuntime&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;Native&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;SecureRandom&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;values&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;nx&quot;&gt;getRandomInt&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;c1&quot;&gt;// generate a singleton array of unsigned 32-bit ints and return&lt;/span&gt;
      &lt;span class=&quot;c1&quot;&gt;// that single value.&lt;/span&gt;
      &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;crypto&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;getRandomValues&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;Uint32Array&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;And the Elm module:&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-text&quot; data-lang=&quot;text&quot;&gt;module SecureRandom where

import Native.SecureRandom

getRandomInt : () -&amp;gt; Int
getRandomInt = Native.SecureRandom.getRandomInt
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h2&gt;Side effects&lt;/h2&gt;

&lt;p&gt;Okay, this should work, right?&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=p8-HDUBbtgQ&quot;&gt;Wrong&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;It will &lt;em&gt;run&lt;/em&gt;, but will work largely not the way one might expect. The problem is that this isn&amp;rsquo;t a pure function anymore: the math is broken, and it will not return the same value given the same input (hello, random number generation?), by causing a side effect of changing the random generator&amp;rsquo;s internal state.&lt;/p&gt;

&lt;p&gt;Now if you consider this type signature again, you might see that it doesn&amp;rsquo;t really make any sense in a purely-functional programming language: it will have to either return the same value every time (because how can a function&amp;rsquo;s value vary if it can&amp;rsquo;t get any variation in it&amp;rsquo;s input?), thus not being random at all, &lt;em&gt;or&lt;/em&gt; completely abandon &lt;a href=&quot;https://en.wikipedia.org/wiki/Pure_function&quot;&gt;purity&lt;/a&gt; (that&amp;rsquo;s what we did with our JavaScript bindings).&lt;/p&gt;

&lt;p&gt;Being impure might not seem like a big deal, but there are practical implications to this. As &lt;a href=&quot;https://en.wikipedia.org/wiki/Referential_transparency&quot;&gt;referential transparency&lt;/a&gt; is now broken for your impure (or effectful) function, you can not make a lot of assumptions about your code by just looking at it. The state that the function depends on (to provide the side effects) is now removed from the function&amp;rsquo;s type signature. You now will need to mentally manipulate this state every time the function is called in order to determine the call&amp;rsquo;s result. And as this state may change on every call, you now can not tell the result of your program &lt;a href=&quot;https://en.wikipedia.org/wiki/Halting_problem&quot;&gt;until you actually execute it&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The compiler is now misguided too: since it assumes that all functions are pure, it may decide to reorder, inline or substitute function calls for optimizations. As an effectful function&amp;rsquo;s result now really depends on some hidden state (of which the compiler is not aware!), these optimizations may result in an absolutely unpredictable and most certainly incorrect program behavior.&lt;/p&gt;

&lt;h3&gt;Taming side effects in Elm&lt;/h3&gt;

&lt;p&gt;As we established that our &lt;code&gt;getRandomInt&lt;/code&gt; function will have side effects, we now should use the power of the language&amp;rsquo;s type system and encode this fact in the function&amp;rsquo;s type. We will also need to use a proper implementation.&lt;/p&gt;

&lt;p&gt;One way to accomplish this in Elm is using the &lt;a href=&quot;http://package.elm-lang.org/packages/elm-lang/core/3.0.0/Task&quot;&gt;&lt;code&gt;Task x a&lt;/code&gt;&lt;/a&gt; type. It is widely used to do things like HTTP requests, local storage access, reading cookies, etc. All of these operations have side effects, as they either depend on or modify some global environment.&lt;/p&gt;

&lt;p&gt;A &lt;code&gt;Task x a&lt;/code&gt; value means that some, possibly effectful, action (task) will be performed at some point in time, and a successful result of its execution will be an &lt;code&gt;a&lt;/code&gt; value while a failure result will be encoded with an &lt;code&gt;x&lt;/code&gt; value. It is not that dissimilar from the .NET&amp;rsquo;s &lt;a href=&quot;https://msdn.microsoft.com/en-us/library/system.threading.tasks.task(v=vs.110).aspx&quot;&gt;&lt;code&gt;Task&amp;lt;T&amp;gt;&lt;/code&gt;&lt;/a&gt; class and Haskell&amp;rsquo;s &lt;a href=&quot;https://hackage.haskell.org/package/base-4.8.2.0/docs/System-IO.html#t:IO&quot;&gt;&lt;code&gt;IO a&lt;/code&gt;&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;So our updated function signature should now look like this:&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-text&quot; data-lang=&quot;text&quot;&gt;getRandomInt : () -&amp;gt; Task never Int
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;We know that we want to do an effectful computation – generate a random value of type &lt;code&gt;Int&lt;/code&gt;. We&amp;rsquo;re keeping it simple for now, by saying that our computation can never fail – that&amp;rsquo;s what the &lt;code&gt;never&lt;/code&gt; type variable means in our case.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;(The name &lt;code&gt;never&lt;/code&gt; doesn&amp;rsquo;t actually matter, it&amp;rsquo;s only important that it is a free type variable and our function&amp;rsquo;s return value of type &lt;code&gt;Task x Int&lt;/code&gt; can be used with any &lt;code&gt;x&lt;/code&gt;, depending on the context.)&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;But how do we know that this kind of function will not have any (unwanted) side effects? We know this by inspecting the possibilities that Elm gives us to operate on &lt;code&gt;Task x a&lt;/code&gt; values:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  You can not get the resulting value out of a &lt;code&gt;Task&lt;/code&gt; immediately, thus causing the side effect to happen arbitrarily&lt;/li&gt;
&lt;li&gt;  You do not control when the &lt;code&gt;Task&lt;/code&gt; is actually executed – the Elm runtime does this for you, and you should trust it&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You are fundamentally limited to just two things you can do to &lt;code&gt;Task&lt;/code&gt;s:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;You only can wait for the &lt;code&gt;Task&lt;/code&gt; to complete, &lt;a href=&quot;http://package.elm-lang.org/packages/elm-lang/core/3.0.0/Task#andThen&quot;&gt;and then&lt;/a&gt; perform another &lt;code&gt;Task&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-text&quot; data-lang=&quot;text&quot;&gt;andThen : Task x a -&amp;gt; (a -&amp;gt; Task x b) -&amp;gt; Task x b
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;And you also can create a &lt;code&gt;Task&lt;/code&gt; that will bear no side effects, and will just instantly resolve as completed and either &lt;a href=&quot;http://package.elm-lang.org/packages/elm-lang/core/3.0.0/Task#succeed&quot;&gt;return&lt;/a&gt; a particular value or &lt;a href=&quot;http://package.elm-lang.org/packages/elm-lang/core/3.0.0/Task#fail&quot;&gt;fail&lt;/a&gt; with an error value:&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-text&quot; data-lang=&quot;text&quot;&gt;succeed : a -&amp;gt; Task x a
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-text&quot; data-lang=&quot;text&quot;&gt;fail : x -&amp;gt; Task x a
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It turns out that this is enough to do all kinds of things when constructing simple or complicated task workflows. In fact, the &lt;code&gt;Task x a&lt;/code&gt; type forms a &lt;a href=&quot;http://hackage.haskell.org/package/base-4.8.2.0/docs/Prelude.html#t:Monad&quot;&gt;monad&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;But there is no way to explicitly run a task in Elm – you &lt;a href=&quot;http://elm-lang.org/guide/reactivity#tasks&quot;&gt;run tasks by connecting them to a port&lt;/a&gt;. Here, the Elm runtime protects us from using tasks in an incorrect way by completely monopolizing the way they are executed.&lt;/p&gt;

&lt;h3&gt;Native tasks&lt;/h3&gt;

&lt;p&gt;Okay, let&amp;rsquo;s now fix our code to generate random numbers inside a &lt;code&gt;Task&lt;/code&gt;. Here&amp;rsquo;s how our JavaScript code should look like:&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span class=&quot;c1&quot;&gt;// boilerplate&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;Elm&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;Native&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;SecureRandom&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{};&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;Elm&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;Native&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;SecureRandom&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;make&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;localRuntime&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;nx&quot;&gt;localRuntime&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;Native&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;localRuntime&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;Native&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;||&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{};&lt;/span&gt;
  &lt;span class=&quot;nx&quot;&gt;localRuntime&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;Native&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;SecureRandom&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;localRuntime&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;Native&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;SecureRandom&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;||&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{};&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;localRuntime&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;Native&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;SecureRandom&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;values&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;localRuntime&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;Native&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;SecureRandom&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;values&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

  &lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;crypto&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;window&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;crypto&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;// the Task module&lt;/span&gt;
  &lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;Task&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;Elm&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;Native&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;Task&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;make&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;localRuntime&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;localRuntime&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;Native&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;SecureRandom&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;values&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;nx&quot;&gt;getRandomInt&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;c1&quot;&gt;// create the Task&lt;/span&gt;
      &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;Task&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;asyncFunction&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;callback&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;c1&quot;&gt;// notify that our Task has completed with some result&lt;/span&gt;
        &lt;span class=&quot;nx&quot;&gt;callback&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
          &lt;span class=&quot;c1&quot;&gt;// wrap the random int into a succeeding Task&lt;/span&gt;
          &lt;span class=&quot;nx&quot;&gt;Task&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;succeed&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
            &lt;span class=&quot;c1&quot;&gt;// generate a random int&lt;/span&gt;
            &lt;span class=&quot;nx&quot;&gt;crypto&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;getRandomValues&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;Uint32Array&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]));&lt;/span&gt;
      &lt;span class=&quot;p&quot;&gt;});&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;We create the &lt;code&gt;Task&lt;/code&gt; by calling the &lt;code&gt;Task.asyncFunction&lt;/code&gt; function and giving it our worker function that will be called when it&amp;rsquo;s time to execute the task. Our function will be given a callback, which we should call when the &lt;code&gt;Task&lt;/code&gt; is complete, and we are ready to return the resulting value.&lt;/p&gt;

&lt;p&gt;Elm code:&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-text&quot; data-lang=&quot;text&quot;&gt;module SecureRandom where

import Native.SecureRandom

getRandomInt : () -&amp;gt; Task never Int
getRandomInt = Native.SecureRandom.getRandomInt
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;As you can see, only the type signature has changed in the Elm module.&lt;/p&gt;

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

&lt;p&gt;I&amp;rsquo;ve used this approach to accomplish &lt;a href=&quot;https://github.com/blacktaxi/pwdgen-elm&quot;&gt;secure random password generation in Elm&lt;/a&gt;. (I know, I already &lt;a href=&quot;http://reversemicrowave.me/blog/2013/12/19/a-password-generation-tool/&quot;&gt;did this&lt;/a&gt; &lt;a href=&quot;http://reversemicrowave.me/blog/2015/04/20/js_of_ocaml-pwdgen/&quot;&gt;twice&lt;/a&gt;!)&lt;/p&gt;

&lt;p&gt;I&amp;rsquo;ve put these Elm bindings together into a library, which you can find &lt;a href=&quot;https://github.com/blacktaxi/elm-random-secure&quot;&gt;here on GitHub&lt;/a&gt;. There is a simple example of how you can use it in practice, but you can also check out the &lt;a href=&quot;https://github.com/blacktaxi/pwdgen-elm/blob/30b96b8e49d685e3bf3842f0353c399e26fe2dc4/src/Generator.elm#L46&quot;&gt;source code&lt;/a&gt; of my password generation app. Hope you will find it helpful.&lt;/p&gt;

&lt;p&gt;As for side effect management in purely-functional languages, it may look a bit daunting in the beginning, but it definitely pays off in the long run.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Hello, js_of_ocaml</title>
   <link href="http://reversemicrowave.me/blog/2015/04/20/js_of_ocaml-pwdgen"/>
   <updated>2015-04-20T03:06:58+03:00</updated>
   <id>http://reversemicrowave.me/blog/2015/04/20/js_of_ocaml-pwdgen</id>
   <content type="html">&lt;p&gt;Recently I finally got myself to try writing some
OCaml. Of course, I never make it easy for myself, so I tried out js_of_ocaml
at the same time.&lt;/p&gt;

&lt;p&gt;I wanted to improve my &lt;a href=&quot;/blog/2013/12/19/a-password-generation-tool/&quot;&gt;password generation tool&lt;/a&gt; to
generate passwords on the client side, as server-side password generation
with security improvement pretense is just clown-worthy.&lt;/p&gt;

&lt;!-- more --&gt;

&lt;h4&gt;TL;DR&lt;/h4&gt;

&lt;p&gt;It all worked out &amp;ndash; js_of_ocaml is pretty cool, but I wouldn&amp;rsquo;t say I&amp;rsquo;d rush
to use it in a production system right away. Of course I have little to no
OCaml and js_of_ocaml experience, so perhaps with some more time invested it
gets a lot better.&lt;/p&gt;

&lt;p&gt;The end result can be seen here: &lt;a href=&quot;http://reversemicrowave.me/pwdgen&quot;&gt;password generation app (CLICK)&lt;/a&gt;.
The source code is on &lt;a href=&quot;https://github.com/blacktaxi/pwdgen/tree/8e72c559992c3b452dec99ee53c5555fff911029&quot;&gt;GitHub&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;OCaml, js_of_ocaml&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://ocaml.org/&quot;&gt;OCaml&lt;/a&gt; is a general purpose programming language with strong bias towards
statically-typed functional programming. It descends from the
ML family of languages and has been around for a rather long time now. Some of
the cool features of this language are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;first-class parametrizable modules, which are often compared to Haskell&amp;rsquo;s
typeclasses, but are in fact more powerful in a way that they can be parametrized
not only by type, but by value. (see &lt;a href=&quot;https://realworldocaml.org/v1/en/html/functors.html&quot;&gt;OCaml Functors&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;sophisticated &lt;a href=&quot;https://realworldocaml.org/v1/en/html/objects.html&quot;&gt;object system&lt;/a&gt;
with support for structural typing&lt;/li&gt;
&lt;li&gt;powerful type system with support for &lt;a href=&quot;https://blogs.janestreet.com/why-gadts-matter-for-performance/&quot;&gt;GADTs&lt;/a&gt;
and &lt;a href=&quot;http://okmij.org/ftp/Computation/lightweight-dependent-typing.html&quot;&gt;dependent types to some extent&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href=&quot;http://ocsigen.org/js_of_ocaml/&quot;&gt;js_of_ocaml&lt;/a&gt; is an &lt;a href=&quot;https://realworldocaml.org/v1/en/html/the-compiler-backend-byte-code-and-native-code.html&quot;&gt;OCaml bytecode&lt;/a&gt; compiler that outputs JavaScript. It&amp;rsquo;s been developed since at least 2010 and
the project seems to be getting a &lt;a href=&quot;https://github.com/ocsigen/js_of_ocaml&quot;&gt;fair amount of activity&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Since js_of_ocaml compiles OCaml bytecode, it therefore does not limit the
language to some subset &amp;ndash; it is a full-on OCaml compiler to JavaScript.&lt;/p&gt;

&lt;h2&gt;Performance&lt;/h2&gt;

&lt;p&gt;It&amp;rsquo;s hard to assess performance in my use case since I don&amp;rsquo;t do any heavy
computation and don&amp;rsquo;t rely on low latency, but let&amp;rsquo;s look at Chrome dev tools
profiler&amp;rsquo;s data.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Disclaimer: I&amp;rsquo;m not trying to pretend this is a legit analysis of JavaScript code
generated by js_of_ocaml. I&amp;rsquo;m pretty sure it is the way it is only because I
made it so.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The program itself is rather simple. The password generation is done in
a series of steps:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;parse the password template, creating a password template definition structure&lt;/li&gt;
&lt;li&gt;for each item in the passowrd template, run a corresponding random token generation function&lt;/li&gt;
&lt;li&gt;concatenate the results into a single string&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;Memory&lt;/h3&gt;

&lt;p&gt;According to memory profiler, each password generation run allocates about 20Mb
of heap memory. From the chart it looks like most of the allocated objects, by
volume, are strings. This looks like a pretty bad result.&lt;/p&gt;

&lt;figure class=&#39;normalsize&#39;&gt;&lt;img src=&quot;/media/2015-js_of_ocaml/memory.png&quot;&gt;&lt;figcaption&gt;Memory allocation chart for a single password generation run.&lt;/figcaption&gt;&lt;/figure&gt;

&lt;p&gt;My guess about this is the inefficient string manipulation code:&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-ocaml&quot; data-lang=&quot;ocaml&quot;&gt;&lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;rec&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;explode&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;char&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;list&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;function&lt;/span&gt;
  &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;&amp;quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;[]&lt;/span&gt;
  &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;s&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nn&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;s&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;explode&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nn&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sub&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;s&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;nn&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;length&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;))&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;rec&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;implode&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;char&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;list&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;function&lt;/span&gt;
  &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;[]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;&amp;quot;&lt;/span&gt;
  &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;xs&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nn&quot;&gt;Char&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;escaped&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;^&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;implode&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;xs&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;filter&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;p&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;s&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;
  &lt;span class=&quot;c&quot;&gt;(* ugh *)&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;explode&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;s&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&amp;gt;&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;filter&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;p&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;implode&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;OCaml&amp;rsquo;s standard library doesn&amp;rsquo;t really have a lot of string processing functions.
In fact, the string processing support in standard OCaml is rather lacking, if
not abysmal, considering it&amp;rsquo;s 2015 and there&amp;rsquo;s no Unicode support.&lt;/p&gt;

&lt;p&gt;I just went the simplest route and think that efficiency of this implementation is horrible. I even used that &lt;code&gt;filter&lt;/code&gt; function in one place.&lt;/p&gt;

&lt;h3&gt;CPU&lt;/h3&gt;

&lt;p&gt;Each generation takes about 50-100ms to complete, including ~1-3 garbage
collections, on a 2.3GHz i7. Honestly, this is a pretty bad result as well. I would blame it all on the inefficient string processing again.&lt;/p&gt;

&lt;figure class=&#39;normalsize&#39;&gt;&lt;img src=&quot;/media/2015-js_of_ocaml/cpu.png&quot;&gt;&lt;figcaption&gt;Flame graph of a single password generation run.&lt;/figcaption&gt;&lt;/figure&gt;

&lt;p&gt;It doesn&amp;rsquo;t seem to help that almost half of the time is spent doing garbage collection.&lt;/p&gt;

&lt;h3&gt;Generated code size&lt;/h3&gt;

&lt;p&gt;In my case, js_of_ocaml produced around 60kb of minified JavaScript, which I&amp;rsquo;d
say is not bad at all, especially compared to other compilers that have a
JavaScript backend. They will often drag the whole runtime into the output, compiling
a helloworld-like program to over 1Mb of JavaScript code (looking at you, GHCJS).&lt;/p&gt;

&lt;h2&gt;Ergonomics&lt;/h2&gt;

&lt;p&gt;At this minimal level of sophistication my experience with OCaml + js_of_ocaml has been
good enough &amp;ndash; it works.&lt;/p&gt;

&lt;p&gt;There are some pain points though.&lt;/p&gt;

&lt;h4&gt;Standard OCaml library&lt;/h4&gt;

&lt;p&gt;String processing support is &lt;em&gt;modest&lt;/em&gt;: not a lot of string utility functions,
no support for Unicode in the standard library. There also doesn&amp;rsquo;t seem to be
a lot of math functions in the standard library. I suppose there&amp;rsquo;s everything
you need is in the popular libraries like Batteries.&lt;/p&gt;

&lt;h4&gt;JavaScript interop&lt;/h4&gt;

&lt;p&gt;Interoperation with JavaScript could be more intuitive/documented. I think
it also could be made somewhat easier to do.&lt;/p&gt;

&lt;p&gt;Calling OCaml functions from JavaScript:&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;generate&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;dict&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;tpl&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// call into js_of_ocaml code&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;window&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;pwdGen&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;generate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;tpl&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;dict&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Exposing OCaml code to JavaScript:&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-ocaml&quot; data-lang=&quot;ocaml&quot;&gt;&lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;m&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;Js&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nn&quot;&gt;Unsafe&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;obj&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;[|&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|]&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;in&lt;/span&gt;
&lt;span class=&quot;nn&quot;&gt;Js&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nn&quot;&gt;Unsafe&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;global&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;##&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pwdGen&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;js_generate&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tpl_string&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dict&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;(* ... *)&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;in&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;##&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;generate&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;-&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;Js&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;wrap_callback&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;js_generate&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Returning JavaScript objects from OCaml:&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-ocaml&quot; data-lang=&quot;ocaml&quot;&gt;&lt;span class=&quot;c&quot;&gt;(* ... *)&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Error&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;e&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;r&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;Js&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nn&quot;&gt;Unsafe&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;obj&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;[|&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|]&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;in&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;##&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;error&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;-&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;Js&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;Could not parse template: &amp;quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;^&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;r&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;(* ... *)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h4&gt;&lt;a href=&quot;https://opam.ocaml.org/&quot;&gt;Opam&lt;/a&gt;&lt;/h4&gt;

&lt;p&gt;Often, the language&amp;rsquo;s build system or the package manager can give a lot of grief to beginners.
I liked my first opam experience though. I found the compiler switching
feature very cool, although I only had to use it once so I could get js_of_ocaml
to work.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Another password generation tool</title>
   <link href="http://reversemicrowave.me/blog/2013/12/19/a-password-generation-tool"/>
   <updated>2013-12-19T02:06:58+02:00</updated>
   <id>http://reversemicrowave.me/blog/2013/12/19/a-password-generation-tool</id>
   <content type="html">&lt;h2&gt;Motivation&lt;/h2&gt;

&lt;p&gt;The Windows 2008 R2 on our build server has been reminding me to change the password for quite some time now. Since I can remember I&amp;rsquo;ve been mostly using relatively short (8-10 characters) randomly generated passwords. While it is not too hard (although takes time) to remember one of those, there&amp;rsquo;s a much better way to generate a password, as was illustrated &lt;a href=&quot;http://xkcd.com/936/&quot;&gt;in this comic&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Of course, I didn&amp;rsquo;t want to directly follow that suggestion, so I came up with my own requirements for a perfect password (and perfect password generator) and figured it&amp;rsquo;s time for me to finally do the thing I wanted to do for a long time now: to become one of the many who&amp;rsquo;ve created an &lt;em&gt;xkcd-style password generator&lt;/em&gt;.&lt;/p&gt;

&lt;h2&gt;Development&lt;/h2&gt;

&lt;p&gt;It&amp;rsquo;s been over a year since I&amp;rsquo;ve created this &lt;a href=&quot;https://gist.github.com/blacktaxi/3694912&quot;&gt;gist&lt;/a&gt;:&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span class=&quot;n&quot;&gt;Python&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;2.7&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;default&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Jun&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;12&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2011&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;15&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;08&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;59&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MSC&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1500&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;32&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;bit&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Intel&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)]&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;on&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;win32&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;Type&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot;copyright&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot;credits&amp;quot;&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;or&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot;license()&amp;quot;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;more&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;information&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;random&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;random&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;seed&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;nltk&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;nltk.corpus&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;wordnet&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;wn&lt;/span&gt;

&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nouns&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;verbs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;adjectives&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;adverbs&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;wn&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;all_synsets&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pos&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;POS&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;POS&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;wn&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;NOUN&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;wn&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;VERB&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;wn&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ADJ&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;wn&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ADV&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]]&lt;/span&gt;

&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;gen_phrase&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pattern&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;random&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;choice&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pattern&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;phrase_to_string&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;phrase&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;#39; &amp;#39;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;join&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;lemmas&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;s&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;phrase&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;gen_password&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;phrase_to_string&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;gen_phrase&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;adverbs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;adjectives&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;adjectives&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nouns&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;

&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;gen_password&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;s&quot;&gt;&amp;#39;decidedly Noachian autoplastic bag_lady&amp;#39;&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;gen_password&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;s&quot;&gt;&amp;#39;ad_val faithful regimented accordance&amp;#39;&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;gen_password&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;s&quot;&gt;&amp;#39;familiarly votive empty Gadiformes&amp;#39;&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;gen_password&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;s&quot;&gt;&amp;#39;universally surmountable wiry field_mouse&amp;#39;&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;gen_password&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;s&quot;&gt;&amp;#39;deadpan bilingual grandiose oxygen_deficit&amp;#39;&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;gen_password&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;s&quot;&gt;&amp;#39;flip-flap heterozygous unenlightened solitaire&amp;#39;&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;gen_password&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;s&quot;&gt;&amp;#39;only piquant apart Massawa&amp;#39;&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;gen_password&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;s&quot;&gt;&amp;#39;continuously forty-two coarse-haired Israel&amp;#39;&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;gen_password&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;s&quot;&gt;&amp;#39;actually trigger-happy tactful cobia&amp;#39;&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This gist is basically the essence of what I wanted to achieve: generate passwords by given template.&lt;/p&gt;

&lt;p&gt;Here, I used &lt;a href=&quot;http://nltk.org/&quot;&gt;nltk&lt;/a&gt; (which is a natural language processing toolkit for Python), which, among other things, gives you access to a dozen of structured corpora and dictionaries and whatnot. I used it to pick a random word that is a specific part of speech. As the WordNet dictionary was the better-structured one, and with permissive license, I&amp;rsquo;ve decided to use that.&lt;/p&gt;

&lt;p&gt;Now, how do I make a webapp out of it, so I can access it from anywhere? I&amp;rsquo;ve decided that I should finally give &lt;a href=&quot;https://developers.google.com/appengine/&quot;&gt;Google App Engine&lt;/a&gt; a try. Overall, registering an application, downloading the example code, deploying and seeing it running on the appengine server took about 5 minutes, which I think is pretty impressive.&lt;/p&gt;

&lt;p&gt;Using nltk and WordNet directly on the appengine seemed overkill, so I didn&amp;rsquo;t do that. Instead, I&amp;rsquo;ve flattened the dictionary and all the metadata to a much simpler structure using the following code:&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;nltk&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;nltk.corpus&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;wordnet&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;#39;DICTIONARY =&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;repr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pos&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;lemma&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;synset&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;wordnet&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;all_synsets&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pos&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;getattr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;wordnet&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pos&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lemma&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;synset&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;lemmas&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pos&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;#39;NOUN&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;#39;VERB&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;#39;ADJ&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;#39;ADV&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;As a result, I get a Python dictionary with entries for the parts of speech (nouns, verbs, adjectives and adverbs). Each entry of this dictionary contains a list of words that are of that particular part of speech. The dictionary isn&amp;rsquo;t stored in any kind of database, but instead is imported into the &lt;code&gt;main.py&lt;/code&gt;. The source code for the generated dictionary is a massive 2.8Mb &lt;code&gt;.py&lt;/code&gt; file.&lt;/p&gt;

&lt;p&gt;By storing the dictionary in a Python file I also simplified the deployment a lot, as the download for WordNet dictionary is a separate step which I wasn&amp;rsquo;t sure could be automated. I also think this way it actually makes the web app to load faster, as parsing a huge source file is probably faster than querying WordNet via nltk.&lt;/p&gt;

&lt;h2&gt;Result&lt;/h2&gt;

&lt;p&gt;Couple hours of work, and voila &amp;ndash; &lt;a href=&quot;https://sparemaranta.appspot.com&quot;&gt;the app is ready and you can access it here&lt;/a&gt;. I realize that the writing is goofy, but my excuse is that I just had to fill all that space that &lt;a href=&quot;http://getbootstrap.com/examples/jumbotron-narrow/&quot;&gt;this Twitter Bootstrap example template&lt;/a&gt; had.&lt;/p&gt;

&lt;p&gt;One cool feature my generator has that I haven&amp;rsquo;t seen anywhere else is that the user can set the template from which the password will be generated. For example, the default template is &lt;code&gt;&amp;lt;adj&amp;gt;&amp;lt;noun&amp;gt;&amp;lt;00&amp;gt;&lt;/code&gt;, which means that it will concatenate a random adjective, a noun and a two digit number to create a password. Like this: &lt;code&gt;distributionaldisplaycase83&lt;/code&gt;. So far the templating capabilities are limited, but I will probably work on expanding them in future.&lt;/p&gt;

&lt;p&gt;Of course, in the spirit of all modern things, I&amp;rsquo;ve also made the API to the generator available &lt;a href=&quot;https://sparemaranta.appspot.com/api/1/generate&quot;&gt;here&lt;/a&gt;. The available parameters are &lt;code&gt;template&lt;/code&gt; (default is &lt;code&gt;&amp;lt;adj&amp;gt;&amp;lt;noun&amp;gt;&amp;lt;00&amp;gt;&lt;/code&gt;) and &lt;code&gt;count&lt;/code&gt; (default is &lt;code&gt;5&lt;/code&gt;). For example, a &lt;code&gt;GET&lt;/code&gt; request to &lt;a href=&quot;https://sparemaranta.appspot.com/api/1/generate?template=%3Cadv%3E-%3Cadj%3E-%3Cnoun%3E.%3C000%3E&amp;amp;count=3&quot;&gt;https://sparemaranta.appspot.com/api/1/generate?template=&amp;lt;adv&amp;gt;-&amp;lt;adj&amp;gt;-&amp;lt;noun&amp;gt;.&amp;lt;000&amp;gt;&amp;amp;count=3&lt;/a&gt; yielded this for me:&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-json&quot; data-lang=&quot;json&quot;&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;&amp;quot;passwords&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;
    &lt;span class=&quot;s2&quot;&gt;&amp;quot;bad-ineligible-staripomoea.837&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; 
    &lt;span class=&quot;s2&quot;&gt;&amp;quot;astern-shortened-conflictofinterest.857&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; 
    &lt;span class=&quot;s2&quot;&gt;&amp;quot;unscientifically-indurate-cithern.760&amp;quot;&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; 
  &lt;span class=&quot;nt&quot;&gt;&amp;quot;template&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;&amp;lt;adv&amp;gt;-&amp;lt;adj&amp;gt;-&amp;lt;noun&amp;gt;.&amp;lt;000&amp;gt;&amp;quot;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The successful reply will be a JSON object with two properties: &lt;code&gt;passwords&lt;/code&gt;, which will contain a list of generated passwords, and &lt;code&gt;template&lt;/code&gt;, which will just echo the received template, for debugging purposes.&lt;/p&gt;

&lt;p&gt;Only after I was done, I&amp;rsquo;ve found out that there&amp;rsquo;s already a generator like that: &lt;a href=&quot;http://preshing.com/20110811/xkcd-password-generator/&quot;&gt;see here&lt;/a&gt; (and it&amp;rsquo;s even been covered at &lt;a href=&quot;http://lifehacker.com/5830355/xkcd-password-generator-creates-high+security-easy+to+remember-passwords&quot;&gt;lifehacker&lt;/a&gt;). Interestingly, while it&amp;rsquo;s been over two years, &lt;a href=&quot;https://www.google.com.ua/search?q=password+generator&quot;&gt;googling for a password generator&lt;/a&gt; will still yield the usual &lt;em&gt;mash-some-random-chars-together-style&lt;/em&gt; generators on the first page.&lt;/p&gt;

&lt;p&gt;It is questionable whether the safety of an online password generator is good enough, even though the data is pumped over a secure connection, but I think it is at least good enough for me. I might make the generator offline, but for now this project has served it&amp;rsquo;s both purposes &amp;ndash; create something useful and have some fun.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/blacktaxi/pwdgen&quot;&gt;Source code&lt;/a&gt;&lt;/p&gt;
</content>
 </entry>
 
</feed>