Comparison

util-src/crand.c @ 8447:200f4f1b7833

util.crand: Use a small buffer on the stack for small pieces of random, should be faster
author Kim Alvefur <zash@zash.se>
date Sun, 03 Dec 2017 15:03:25 +0100
parent 8446:a72898dde1a0
child 8448:f516a52f19e8
comparison
equal deleted inserted replaced
8446:a72898dde1a0 8447:200f4f1b7833
56 #include <openssl/rand.h> 56 #include <openssl/rand.h>
57 #elif ! defined(WITH_ARC4RANDOM) 57 #elif ! defined(WITH_ARC4RANDOM)
58 #error util.crand compiled without a random source 58 #error util.crand compiled without a random source
59 #endif 59 #endif
60 60
61 #ifndef SMALLBUFSIZ
62 #define SMALLBUFSIZ 32
63 #endif
64
61 int Lrandom(lua_State *L) { 65 int Lrandom(lua_State *L) {
66 char smallbuf[SMALLBUFSIZ];
67 char *buf = &smallbuf[0];
62 const size_t len = luaL_checkinteger(L, 1); 68 const size_t len = luaL_checkinteger(L, 1);
63 void *buf = lua_newuserdata(L, len); 69
70 if(len > SMALLBUFSIZ) {
71 buf = lua_newuserdata(L, len);
72 }
73
64 74
65 #if defined(WITH_GETRANDOM) 75 #if defined(WITH_GETRANDOM)
66 /* 76 /*
67 * This acts like a read from /dev/urandom with the exception that it 77 * This acts like a read from /dev/urandom with the exception that it
68 * *does* block if the entropy pool is not yet initialized. 78 * *does* block if the entropy pool is not yet initialized.