Random number generator

Dependents:   mBuinoBlinky

Revision:
0:299f3795114b
Child:
1:0536a4ca8d35
--- /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();
+}