Random number generator

Dependents:   mBuinoBlinky

Files at this revision

API Documentation at this revision

Comitter:
feb11
Date:
Tue Sep 17 11:30:29 2013 +0000
Child:
1:0536a4ca8d35
Commit message:
initial import

Changed in this revision

Random.cpp Show annotated file Show diff for this revision Revisions of this file
Random.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Random.cpp	Tue Sep 17 11:30:29 2013 +0000
@@ -0,0 +1,41 @@
+#include "Random.h"
+#include "Crypto.h"
+
+Random::Random():
+f(p20)
+{
+    AnalogIn a(p15), b(p16), c(p17), d(p18);
+    
+    uint16_t tmp = a.read_u16();
+    memcpy(pool, &tmp, 2);
+    tmp = b.read_u16();
+    memcpy(&pool[2], &tmp, 2);
+    tmp = c.read_u16();
+    memcpy(&pool[4], &tmp, 2);
+    tmp = d.read_u16();
+    memcpy(&pool[6], &tmp, 2);
+    tmp = a.read_u16();
+    memcpy(&pool[8], &tmp, 2);
+    tmp = b.read_u16();
+    memcpy(&pool[10], &tmp, 2);
+    tmp = c.read_u16();
+    memcpy(&pool[12], &tmp, 2);
+    tmp = d.read_u16();
+    memcpy(&pool[14], &tmp, 2);
+}
+
+uint8_t Random::getByte()
+{
+    uint8_t hash[16];
+    MD5::computeHash(hash, pool, 16);
+    uint8_t tmp = pool[hash[6]%16];
+    memcpy(pool, hash, 16);
+    
+    return tmp ^ (f.read_u16() & 0xff);
+}
+
+void Random::getBytes(uint8_t *out, uint32_t length)
+{
+    for(int i = 0; i < length; ++i)
+        out[i] = getByte();
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Random.h	Tue Sep 17 11:30:29 2013 +0000
@@ -0,0 +1,21 @@
+#ifndef RANDOM_H
+#define RANDOM_H
+
+#include "mbed.h"
+
+class Random
+{
+    public :
+        
+        Random();
+        
+        uint8_t getByte();
+        void getBytes(uint8_t *out, uint32_t length);
+
+    private :
+    
+        uint8_t pool[16];
+        AnalogIn f;
+};
+
+#endif