PageRenderTime 102ms CodeModel.GetById 61ms app.highlight 2ms RepoModel.GetById 38ms app.codeStats 0ms

/mordor/streams/random.h

http://github.com/mozy/mordor
C Header | 43 lines | 25 code | 11 blank | 7 comment | 0 complexity | 62acb1079a7567d39fc98dcd4c492596 MD5 | raw file
 1#ifndef __MORDOR_RANDOM_STREAM_H__
 2#define __MORDOR_RANDOM_STREAM_H__
 3// Copyright (c) 2009 - Mozy, Inc.
 4
 5#include "stream.h"
 6
 7#ifdef WINDOWS
 8#include <wincrypt.h>
 9#endif
10
11namespace Mordor {
12
13/// a stream to generate random data
14/// uses CryptGenRandom() on WINDOWS; OpenSSL RAND_bytes elsewhere (seeded automatically by /dev/urandom)
15/// @note RandomStream is not guaranteed to be thread safety.
16///   On Windows platform, the thread safety depends on the cryptographic service provider implementation
17///   On Linux/MacOS platform, it depends on openssl thread safety. By default it isn't, use OpensslLockManager
18///   to ensure the thread safety.
19class RandomStream : public Stream
20{
21public:
22    typedef boost::shared_ptr<RandomStream> ptr;
23
24    RandomStream();
25    ~RandomStream();
26
27    bool supportsRead() { return true; }
28    size_t read(void *buffer, size_t length);
29    using Stream::read;
30
31    bool supportsSeek() { return true; }
32    long long seek(long long offset, Anchor anchor = BEGIN) { return 0; }
33
34private:
35#ifdef WINDOWS
36    HCRYPTPROV m_hCP;
37#endif
38};
39
40}
41
42#endif // __MORDOR_RANDOM_STREAM_H__
43