billstclair / elm-dev-random / PortFunnel.DevRandom

The PortFunnel.DevRandom module provides a billstclair/elm-port-funnel funnel to generate cryptographically-secure random numbers. It does this with JavaScript code that calls window.crypto.getRandomValues().

There is a simulator that uses the standard Elm Random module, which is NOT cryptographically secure. See example/Diceware.elm for how to use it.

See the example readme for instructions on creating the ports and using the included JavaScript code.

Types


type Message
    = GenerateBytes Basics.Int
    | GenerateInt Basics.Int
    | RandomBytes RandomBytesRecord
    | RandomInt RandomIntRecord
    | SimulateBytes Basics.Int
    | SimulateInt Basics.Int
    | Startup

The GenerateBytes message requests a list of random bytes of the given size.

The RandomBytes message returns those random bytes.

The GenerateInt message requests a random integer >= 0 and < its arg.

The RandomInt message returns that integer.

The SimulateBytes and SimulateInt messages are used internally by the simulator.


type Response
    = NoResponse
    | RandomBytesResponse ({ isSecure : Basics.Bool, bytes : List Basics.Int })
    | RandomIntResponse ({ isSecure : Basics.Bool, int : Basics.Int })

A MessageResponse encapsulates a message.

RandomBytesResponse wraps a list of integers and whether their generation was cryptographically secure.

RandomIntResponse wraps an integer and whether its generation was cryptographically secure.


type State

Our internal state.

This module's state is only used by the simulator. If you don't save it, the simulator will always use the same random seed.


type alias JSVersion =
{ v4_1 : () }

This is used to force a major version bump when the JS changes.

You'll usually not use it for anything.

Components of a PortFunnel.FunnelSpec

moduleName : String

The name of this funnel: "DevRandom".

moduleDesc : PortFunnel.ModuleDesc Message State Response

Our module descriptor.

commander : (PortFunnel.GenericMessage -> Platform.Cmd.Cmd msg) -> Response -> Platform.Cmd.Cmd msg

Responsible for sending a CmdResponse back through the port.

This funnel doesn't initiate any sends, so this function always returns Cmd.none.

Initial State

initialState : Basics.Int -> State

The initial state. Encapsulates a Random.Seed.

The arg is passed to Random.initialSeed. This is used only by the simulator, so if you're using the JS code for real random numbers, passing 0 here is fine.

Sending a Message out the Cmd Port

send : (Json.Encode.Value -> Platform.Cmd.Cmd msg) -> Message -> Platform.Cmd.Cmd msg

Send a Message through a Cmd port.

Conversion to Strings

toString : Message -> String

Convert a Message to a nice-looking human-readable string.

toJsonString : Message -> String

Convert a Message to the same JSON string that gets sent

over the wire to the JS code.

Simulator

makeSimulatedCmdPort : (Json.Encode.Value -> msg) -> Json.Encode.Value -> Platform.Cmd.Cmd msg

Make a simulated Cmd port.

Non-standard Functions

isLoaded : State -> Basics.Bool

Returns true if a Startup message has been processed.

This is sent by the port code after it has initialized.