Adding more randomised token generation logic
[platform/upstream/iotivity.git] / resource / c_common / ocrandom / src / ocrandom.c
index 631906c..97fe7be 100644 (file)
@@ -195,6 +195,39 @@ void OCFillRandomMem(uint8_t * location, uint16_t len)
     {
         *location++ = OCGetRandomByte();
     }
+    uint8_t *temp = (char *) OICCalloc(len, sizeof(char));
+    int *mask = (int *) OICCalloc(len, sizeof(int));
+    int i, j, rand_idx;
+    for (i = 0; i < len; i++)
+    {
+        mask[i] = 0;
+    }
+    j = 0;
+    for (i = 0; i < len; i++)
+    {
+        rand_idx = lrand48() % len;
+        while((rand_idx < len) && (mask[rand_idx] != 0))
+        {
+            rand_idx++;
+        }
+        if(rand_idx == len)
+        {
+            rand_idx = 0;
+            while(mask[rand_idx] != 0)
+            {
+                rand_idx++;
+            }
+        }
+        temp[rand_idx] = location[j];
+        mask[rand_idx] = 1;
+        j++;
+    }
+    for (i = 0; i < len; i++)
+    {
+        location[i] = temp[i];
+    }
+    OICFree(temp);
+    OICFree(mask);
 }
 
 uint32_t OCGetRandom()