3 Copyright (C) 2005 and later, Cockos Incorporated
5 This software is provided 'as-is', without any express or implied
6 warranty. In no event will the authors be held liable for any damages
7 arising from the use of this software.
9 Permission is granted to anyone to use this software for any purpose,
10 including commercial applications, and to alter it and redistribute it
11 freely, subject to the following restrictions:
13 1. The origin of this software must not be misrepresented; you must not
14 claim that you wrote the original software. If you use this software
15 in a product, an acknowledgment in the product documentation would be
16 appreciated but is not required.
17 2. Altered source versions must be plainly marked as such, and must not be
18 misrepresented as being the original software.
19 3. This notice may not be removed or altered from any source distribution.
25 This file provides the implementation of a decent random number generator,
26 that internally uses a 256-bit state, and SHA-1 to iterate.
42 static unsigned char state
[32];
44 void WDL_RNG_addentropy(void *buf
, int buflen
)
47 tmp
.add(state
,sizeof(state
));
50 tmp
.add((unsigned char *)buf
,buflen
);
51 tmp
.result(state
+sizeof(state
) - WDL_SHA1SIZE
);
54 static void rngcycle()
57 for (i
= 0; i
< (int)sizeof(state
) && state
[i
]++; i
++);
63 tmp
.add(state
,sizeof(state
));
66 char buf
[WDL_SHA1SIZE
];
75 void WDL_RNG_bytes(void *buf
, int buflen
)
80 char tb
[WDL_SHA1SIZE
];
82 tmp
.add(state
,sizeof(state
));
86 const int l
=buflen
< WDL_SHA1SIZE
? buflen
: WDL_SHA1SIZE
;