Diffstat (limited to 'pwmanager/pwmanager/randomizer.cpp') (more/less context) (show whitespace changes)
-rw-r--r-- | pwmanager/pwmanager/randomizer.cpp | 124 |
1 files changed, 124 insertions, 0 deletions
diff --git a/pwmanager/pwmanager/randomizer.cpp b/pwmanager/pwmanager/randomizer.cpp new file mode 100644 index 0000000..e623f51 --- a/dev/null +++ b/pwmanager/pwmanager/randomizer.cpp @@ -0,0 +1,124 @@ +/*************************************************************************** + * * + * copyright (C) 2003, 2004 by Michael Buesch * + * email: mbuesch@freenet.de * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License version 2 * + * as published by the Free Software Foundation. * + * * + ***************************************************************************/ + +/*************************************************************************** + * copyright (C) 2004 by Ulf Schenk + * This file is originaly based on version 1.0.1 of pwmanager + * and was modified to run on embedded devices that run microkde + * + * $Id$ + **************************************************************************/ + +#include "randomizer.h" +#include "pwmexception.h" + +#include <stdlib.h> +#include <time.h> + + +Randomizer * Randomizer::rndObj (0); + +Randomizer::Randomizer() +{ + // probe for random devices + rndDev = fopen("/dev/urandom", "r"); + if (rndDev) + return; + rndDev = fopen("/dev/random", "r"); + if (rndDev) + return; + // fall back to rand_r() + seed = time(0); +} + +Randomizer::~Randomizer() +{ + if (rndDev) { + if (fclose(rndDev)) { + printWarn("failed closing the random-device!"); + } + } +} + +char Randomizer::genRndChar() +{ + if (rndDev) { + /* we have a rand-device-node */ + return (getc(rndDev)); + } else { + /* we don't have a random-device-node. + * so fall back to rand_r() + */ + return (rand_r(&seed) % 0xFF); + } +} + +int Randomizer::genRndInt() +{ + if(rndDev) { + int ret; + if (sizeof(int) == 4) { + (reinterpret_cast<char *>(&ret))[0] = getc(rndDev); + (reinterpret_cast<char *>(&ret))[1] = getc(rndDev); + (reinterpret_cast<char *>(&ret))[2] = getc(rndDev); + (reinterpret_cast<char *>(&ret))[3] = getc(rndDev); + } else if (sizeof(int) == 8) { + (reinterpret_cast<char *>(&ret))[0] = getc(rndDev); + (reinterpret_cast<char *>(&ret))[1] = getc(rndDev); + (reinterpret_cast<char *>(&ret))[2] = getc(rndDev); + (reinterpret_cast<char *>(&ret))[3] = getc(rndDev); + (reinterpret_cast<char *>(&ret))[4] = getc(rndDev); + (reinterpret_cast<char *>(&ret))[5] = getc(rndDev); + (reinterpret_cast<char *>(&ret))[6] = getc(rndDev); + (reinterpret_cast<char *>(&ret))[7] = getc(rndDev); + } else { + printWarn(string(__FILE__) + ":" + tostr(__LINE__) + + ": sizeof(int) != 4 && sizeof(int) != 8"); + rndDev = 0; + seed = time(0); + return genRndInt(); + } + return ret; + } else { + return rand_r(&seed); + } +} + +unsigned int Randomizer::genRndUInt() +{ + if(rndDev) { + unsigned int ret; + if (sizeof(unsigned int) == 4) { + (reinterpret_cast<char *>(&ret))[0] = getc(rndDev); + (reinterpret_cast<char *>(&ret))[1] = getc(rndDev); + (reinterpret_cast<char *>(&ret))[2] = getc(rndDev); + (reinterpret_cast<char *>(&ret))[3] = getc(rndDev); + } else if (sizeof(unsigned int) == 8) { + (reinterpret_cast<char *>(&ret))[0] = getc(rndDev); + (reinterpret_cast<char *>(&ret))[1] = getc(rndDev); + (reinterpret_cast<char *>(&ret))[2] = getc(rndDev); + (reinterpret_cast<char *>(&ret))[3] = getc(rndDev); + (reinterpret_cast<char *>(&ret))[4] = getc(rndDev); + (reinterpret_cast<char *>(&ret))[5] = getc(rndDev); + (reinterpret_cast<char *>(&ret))[6] = getc(rndDev); + (reinterpret_cast<char *>(&ret))[7] = getc(rndDev); + } else { + printWarn(string(__FILE__) + ":" + tostr(__LINE__) + + ": sizeof(unsigned int) != 4 && sizeof(unsigned int) != 8"); + rndDev = 0; + seed = time(0); + return genRndUInt(); + } + return ret; + } else { + return static_cast<unsigned int>(rand_r(&seed)); + } +} |