mirror of
https://github.com/RobTillaart/Arduino.git
synced 2024-10-03 18:09:02 -04:00
85 lines
3.0 KiB
Markdown
85 lines
3.0 KiB
Markdown
|
|
[![Arduino CI](https://github.com/RobTillaart/DEVRANDOM/workflows/Arduino%20CI/badge.svg)](https://github.com/marketplace/actions/arduino_ci)
|
|
[![Arduino-lint](https://github.com/RobTillaart/DEVRANDOM/actions/workflows/arduino-lint.yml/badge.svg)](https://github.com/RobTillaart/DEVRANDOM/actions/workflows/arduino-lint.yml)
|
|
[![JSON check](https://github.com/RobTillaart/DEVRANDOM/actions/workflows/jsoncheck.yml/badge.svg)](https://github.com/RobTillaart/DEVRANDOM/actions/workflows/jsoncheck.yml)
|
|
[![License: MIT](https://img.shields.io/badge/license-MIT-green.svg)](https://github.com/RobTillaart/DEVRANDOM/blob/master/LICENSE)
|
|
[![GitHub release](https://img.shields.io/github/release/RobTillaart/DEVRANDOM.svg?maxAge=3600)](https://github.com/RobTillaart/DEVRANDOM/releases)
|
|
|
|
|
|
# DEVRANDOM
|
|
|
|
Arduino library to wrap a random generator in a stream.
|
|
|
|
|
|
## Description
|
|
|
|
The library implements a stream class that mimics the /dev/random
|
|
device of a Linux system. It can be used for testing with streams.
|
|
|
|
|
|
## Interface
|
|
|
|
### Constructor
|
|
|
|
- **DEVRANDOM()** Constructor.
|
|
- **DEVRANDOM(char \* str)** Constructor, seeding with a string of characters.
|
|
- **DEVRANDOM(uint32_t value)** Constructor, seeding with a integer value.
|
|
- **DEVRANDOM(float value)** Constructor, seeding with a float value.
|
|
|
|
|
|
### Streeam interface
|
|
|
|
to read and to reseed the random generator.
|
|
|
|
- **available()** There is always 1 next byte available
|
|
- **peek()** will give you next byte
|
|
- **read()** will give you next byte and generate a new one.
|
|
- **write()** data will be used for reseeding the random number generator (RNG)
|
|
(SW mode only, in HW mode and AN mode the seed is used to XOR the value, so it
|
|
does have some effect);
|
|
- **flush()** to keep the CI happy.
|
|
|
|
|
|
### Random generator selection
|
|
|
|
- **useSW()** use a software random number generator. This is the default.
|
|
By default the build-in random number generator is used.
|
|
This can be replaced by a RNG of your choice.
|
|
- **useHW(uint8_t pin)** use digitalRead to read 8 bits from a defined pin.
|
|
One can build a hardware RNG that flips between 0 and 1 very rapidly and unpredictably.
|
|
Connect this signal to the pin and it will be read and generate a random byte.
|
|
The seed value from the write is used as an XOR byte.
|
|
- **useAR(uint8_t pin)** use the analogRead to read 8 bits
|
|
This can be fed with an analogue noise source.
|
|
The seed value from the write is used as a XOR byte.
|
|
- **getMode()** returns the source of randomness => 0 = SW, 1 = HW, 2 = AR (see above).
|
|
|
|
As **write()** reseeds the RNG, printing to **DEVRANDOM** will also reseed the RNG.
|
|
E.g. **dr.println("Hello world");** would reseed it too.
|
|
|
|
|
|
## Operation
|
|
|
|
See example sketches.
|
|
|
|
|
|
### Example
|
|
|
|
As shown in the example one can use fscanf to read larger datatypes,
|
|
|
|
```cpp
|
|
DEVRANDOM dr;
|
|
uint32_t x;
|
|
fscanf((FILE*) &dr, "%lu", &x);
|
|
Serial.println(x);
|
|
```
|
|
However float is not supported standard in the fscanf by UNO and strings (%s) generate garbage.
|
|
So a password generator is a bit more difficult (and a good exercise).
|
|
|
|
|
|
## Future
|
|
|
|
- improve documentation
|
|
- add Marsaglia PRG
|
|
-
|