#define RANDOM A0 // Analogue pin as a source of noise.
char ChallResp[11];
void setup() {
pinMode(RANDOM, INPUT); // switch on the analogue pin for random number
Serial.begin(115200);
}
void loop() {
CreateChallenge();
delay(1800);
Serial.println("\n");
}
uint32_t generateRandomSeed(byte RandomSeedDelay) { // Author: Daniel Hienzsch, rheingoldheavy.com
uint8_t seedBitValue = 0;
uint8_t seedByteValue = 0;
uint32_t seedWordValue = 0;
for (uint8_t wordShift = 0; wordShift < 4; wordShift++) // 4 bytes in a 32 bit word
{
for (uint8_t byteShift = 0; byteShift < 8; byteShift++) // 8 bits in a byte
{
for (uint8_t bitSum = 0; bitSum <= 8; bitSum++) // 8 samples of analog pin
{
seedBitValue = seedBitValue + (analogRead(RANDOM) & 0x01); // Flip the coin eight times, adding the results together
}
delay(RandomSeedDelay); // Delay a milliseconds to allow the pin to fluctuate
seedByteValue = seedByteValue | ((seedBitValue & 0x01) << byteShift); // Build a stack of eight flipped coins
seedBitValue = 0; // Clear out the previous coin value
}
seedWordValue = seedWordValue | (uint32_t) seedByteValue << (8 * wordShift); // Build a stack of four sets of 8 coins (shifting right creates a larger number so cast to 32bit)
seedByteValue = 0; // Clear out the previous stack value
}
return (seedWordValue);
}
void CreateChallenge() {
Serial.println("Generating a random string.");
uint32_t RandomSeed = generateRandomSeed(2);
Serial.print("Generated random string: ");
Serial.println(RandomSeed);
randomSeed(RandomSeed);
// ChallResp[0] = '\0';
// if (ChallResp[0] == 0) {
// Serial.println("Character array is empty");
// }
ultoa(RandomSeed, ChallResp, 10);
Serial.print("Character array generated: ");
Serial.print(ChallResp);
Serial.print(" is ");
Serial.print(strlen(ChallResp));
Serial.println(" chars long.");
for (uint8_t i = 0; i < 7; i++) {
// randomSeed(generateRandomSeed(6));
if (ChallResp[i] == '0') {
ChallResp[i] = random(49, 58);
}
// Serial.print(ChallResp[i]);
}
Serial.print("Character array after removing nils: ");
Serial.println(ChallResp);
uint8_t NilPosition = random(3, 7);
Serial.print("Nil position is: ");
Serial.println(NilPosition);
ChallResp[NilPosition] = '0';
Serial.print("Character array after overwriting with 0: ");
Serial.println(ChallResp);
strlcpy(ChallResp, ChallResp, NilPosition + 1);
Serial.print("The right response is: ");
Serial.println(ChallResp);
// for (byte i = 0; i < 10; i++) {
// Serial.print("Reading from input: ");
// Serial.println(analogRead(RANDOM));
// delay(300);
// }
}