-
Notifications
You must be signed in to change notification settings - Fork 0
/
alea.c
133 lines (100 loc) · 3.84 KB
/
alea.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
//
// alea.c
// encrypt_One
//
// Copyleft (ɔ) 2014 Mailden
// Use of this source code is governed by a GNU AFFERO GENERAL PUBLIC
// license (AGPL) that can be found in the LICENSE file.
#include "alea.h"
#define CHARSET_LENGTH 89
void read_random_device (int size, char *dest)
{
int fd;
fd = (int) open("/dev/random",O_RDONLY);
read (fd, dest, size);
}
unsigned char *pick_rand_32bytes (){
typedef struct{
unsigned int part1;
unsigned int part2;
unsigned int part3;
unsigned int part4;
char part5[16];
} CHUNKS;
typedef union
{
CHUNKS seed_chunks;
unsigned char seed[32];
} MYUNION;
MYUNION u;
srandom((unsigned int)(time(NULL) % 9061973)); // put a secret prime number to raise entropy
u.seed_chunks.part1 = ((unsigned int)arc4random()%222)+32;
srandom((unsigned int)(time(NULL) % 2039485230948572951)); // put a secret prime number to raise entropy
u.seed_chunks.part3 = ((unsigned int)arc4random()%222)+32;
read_random_device(8, u.seed_chunks.part5);
srandom((unsigned int)(time(NULL) % 2039485267)); // put a secret prime number to raise entropy
u.seed_chunks.part2 = ((unsigned int)arc4random()%222)+32;
srandom((unsigned int)(time(NULL) % 98876033)); // put a secret prime number to raise entropy
u.seed_chunks.part4 = ((unsigned int)arc4random()%222)+32;
read_random_device(8, u.seed_chunks.part5 + 8);
unsigned char *aeskey = gcry_malloc_secure(32);
memcpy(aeskey, u.seed, 32);
return aeskey;
}
char *pick_rand_16bytes (){
typedef struct{
unsigned int part1;
unsigned int part2;
char part3[8];
} CHUNKS;
typedef union
{
CHUNKS seed_chunks;
char seed[16];
} MYUNION;
MYUNION u;
srandom((unsigned int)(time(NULL) % 9887607777782101)); // put a secret prime number to raise entropy
u.seed_chunks.part1 = (unsigned int)arc4random();
srandom((unsigned int)(time(NULL) % 98876077793)); // put a secret prime number to raise entropy
u.seed_chunks.part2 = (unsigned int)arc4random();
read_random_device(8, u.seed_chunks.part3);
char *key = gcry_malloc_secure(16);
memcpy(key, u.seed, 16);
return key;
}
char *random_string (size_t length) {
//static char charset[] = "%*msIMNO@PJKLQtuvwxyz<abc,.-#defghijkl>ABCD_|EUVWXYZ=/01289?34FGHRST5nopqr67!";
static char charset[] = "%*msIMNOPJ_KLQtuvw@&xyz<abc,.-#de<>{}[DE]fghi(jkl>A)BCUVWXYZ=/012?!89?34FGHRST5n~opqr67!$";
char *randomString = NULL;
srand(clock()+(time(0)/3001)); //a secret prime number to raise entropy
int i;
if (length) {
if (NULL == (randomString = malloc(length +1))) exit(EXIT_FAILURE);
if (randomString) {
int key = 0; // one-time instantiation (static/global would be even better)
for (i = 0 ;i < length; i++) {
key = rand() % CHARSET_LENGTH; // no instantiation, just assignment, no overhead from sizeof
randomString[i] = charset[key];
}
randomString[length] = '\0';
}
}
return randomString;
}
unsigned char *random_string2 (size_t length) {
//return length bytes into the range of printable char from 0x20 to 0xEF ie 256-34 = 222 possibilities
unsigned char *randomString = NULL;
srand(clock()+(time(0)/3011)); // a secret prime number to raise entropy
int i;
if (length) {
if (NULL == (randomString = gcry_malloc_secure(length))) return NULL;
if (randomString) {
unsigned char key = 0;
for (i = 0 ;i < length; i++) {
key = (unsigned char)rand();
randomString[i] = (key % 222) + 33;
}
}
}
return randomString;
}