Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

87 lines (75 sloc) 2.351 kB
/* vim: set expandtab ts=4 sw=4: */
/*
* You may redistribute this program and/or modify it under the terms of
* the GNU General Public License as published by the Free Software Foundation,
* either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef Random_H
#define Random_H
#include "memory/Allocator.h"
#include "exception/Except.h"
#include <stdint.h>
struct Random;
#define Random_new_INIT_FAILED -1
struct Random* Random_new(struct Allocator* alloc, struct Except* eh);
void Random_bytes(struct Random* rand, uint8_t* location, uint64_t count);
/**
* Get random Base32 text, great for password material.
*
* @param output the buffer to write the output to.
* @param length the number of bytes to write.
*/
void Random_base32(struct Random* rand, uint8_t* output, uint32_t length);
static inline void Random_longs(struct Random* rand, uint64_t* location, uint64_t count)
{
Random_bytes(rand, (uint8_t*) location, count*8);
}
static inline void Random_ints(struct Random* rand, uint32_t* location, uint64_t count)
{
Random_bytes(rand, (uint8_t*) location, count*4);
}
static inline int16_t Random_int16(struct Random* rand)
{
int16_t ret;
Random_bytes(rand, (uint8_t*)&ret, 2);
return ret;
}
static inline uint16_t Random_uint16(struct Random* rand)
{
uint16_t ret;
Random_bytes(rand, (uint8_t*)&ret, 2);
return ret;
}
static inline int32_t Random_int32(struct Random* rand)
{
int32_t ret;
Random_bytes(rand, (uint8_t*)&ret, 4);
return ret;
}
static inline uint32_t Random_uint32(struct Random* rand)
{
uint32_t ret;
Random_bytes(rand, (uint8_t*)&ret, 4);
return ret;
}
static inline int64_t Random_int64(struct Random* rand)
{
int64_t ret;
Random_bytes(rand, (uint8_t*)&ret, 8);
return ret;
}
static inline uint64_t Random_uint64(struct Random* rand)
{
uint64_t ret;
Random_bytes(rand, (uint8_t*)&ret, 8);
return ret;
}
#endif
Jump to Line
Something went wrong with that request. Please try again.