All pastes #2074065 Raw Edit

Miscellany

public text v1 · immutable
#2074065 ·published 2011-06-03 10:05 UTC
rendered paste body
    nonce Security::getNonce() {
        static mongo::mutex m("getNonce");
        scoped_lock lk(m);

        if ( ! _initialized )
            init();

        /* question/todo: /dev/random works on OS X.  is it better
           to use that than random() / srandom()?
        */

        nonce n;
#if defined(__linux__) || defined(__sunos__) || defined(__APPLE__)
        _devrandom->read((char*)&n, sizeof(n));
        massert( 10355 , "devrandom failed", !_devrandom->fail());
#elif defined(_WIN32)
        unsigned a=0, b=0;
        assert( rand_s(&a) == 0 );
        assert( rand_s(&b) == 0 );
        n = (((unsigned long long)a)<<32) | b;
#else
        n = (((unsigned long long)random())<<32) | random();
#endif
        return n;
    }