Files
mongo/db/nonce.cpp

84 lines
2.3 KiB
C++
Raw Normal View History

2009-01-23 11:28:29 -05:00
// nonce.cpp
/* Copyright 2009 10gen Inc.
2009-01-23 11:28:29 -05:00
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
2009-01-23 11:28:29 -05:00
*
* http://www.apache.org/licenses/LICENSE-2.0
2009-01-23 11:28:29 -05:00
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
2009-01-23 11:28:29 -05:00
*/
2010-04-27 15:27:52 -04:00
#include "pch.h"
2009-01-23 11:28:29 -05:00
#include "nonce.h"
#include "../util/time_support.h"
2009-01-23 11:28:29 -05:00
2009-08-28 11:57:09 -04:00
extern int do_md5_test(void);
2009-01-23 11:28:29 -05:00
2009-08-28 11:57:09 -04:00
namespace mongo {
2011-01-04 00:40:41 -05:00
2010-11-12 09:20:09 -05:00
BOOST_STATIC_ASSERT( sizeof(nonce) == 8 );
2011-01-04 00:40:41 -05:00
Security::Security() {
static int n;
massert( 10352 , "Security is a singleton class", ++n == 1);
init();
}
2011-01-04 00:40:41 -05:00
void Security::init() {
if( _initialized ) return;
_initialized = true;
2011-02-22 11:01:18 -05:00
#if defined(__linux__) || defined(__sunos__) || defined(__APPLE__)
2009-11-25 22:46:01 -05:00
_devrandom = new ifstream("/dev/urandom", ios::binary|ios::in);
massert( 10353 , "can't open dev/urandom", _devrandom->is_open() );
2009-01-23 11:28:29 -05:00
#elif defined(_WIN32)
srand(curTimeMicros());
#else
srandomdev();
#endif
2011-01-04 00:40:41 -05:00
#ifndef NDEBUG
2009-01-23 11:28:29 -05:00
if ( do_md5_test() )
2011-01-04 00:40:41 -05:00
massert( 10354 , "md5 unit test fails", false);
#endif
2009-01-23 11:28:29 -05:00
}
2011-01-04 00:40:41 -05:00
nonce Security::getNonce() {
2010-05-26 00:46:49 -04:00
static mongo::mutex m("getNonce");
scoped_lock lk(m);
2011-02-22 11:01:18 -05:00
if ( ! _initialized )
init();
2011-01-04 00:40:41 -05:00
/* question/todo: /dev/random works on OS X. is it better
to use that than random() / srandom()?
*/
2009-01-23 11:28:29 -05:00
nonce n;
2011-02-22 11:01:18 -05:00
#if defined(__linux__) || defined(__sunos__) || defined(__APPLE__)
2009-11-25 22:46:01 -05:00
_devrandom->read((char*)&n, sizeof(n));
massert( 10355 , "devrandom failed", !_devrandom->fail());
2009-01-23 11:28:29 -05:00
#elif defined(_WIN32)
2010-12-13 20:22:54 -05:00
unsigned a=0, b=0;
assert( rand_s(&a) == 0 );
assert( rand_s(&b) == 0 );
n = (((unsigned long long)a)<<32) | b;
2009-01-23 11:28:29 -05:00
#else
n = (((unsigned long long)random())<<32) | random();
#endif
return n;
}
2010-04-24 19:24:07 -04:00
unsigned getRandomNumber() { return (unsigned) security.getNonce(); }
2011-01-04 00:40:41 -05:00
bool Security::_initialized;
2009-01-23 11:28:29 -05:00
Security security;
2011-01-04 00:40:41 -05:00
} // namespace mongo