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.
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.
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.
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.
{ v4_1 : () }
This is used to force a major version bump when the JS changes.
You'll usually not use it for anything.
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
.
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.
Message
out the Cmd
Portsend : (Json.Encode.Value -> Platform.Cmd.Cmd msg) -> Message -> Platform.Cmd.Cmd msg
Send a Message
through a Cmd
port.
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.
makeSimulatedCmdPort : (Json.Encode.Value -> msg) -> Json.Encode.Value -> Platform.Cmd.Cmd msg
Make a simulated Cmd
port.
isLoaded : State -> Basics.Bool
Returns true if a Startup
message has been processed.
This is sent by the port code after it has initialized.