~ubuntu-branches/ubuntu/gutsy/poco/gutsy

« back to all changes in this revision

Viewing changes to Foundation/src/RandomStream.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Krzysztof Burghardt
  • Date: 2007-04-27 18:33:48 UTC
  • Revision ID: james.westby@ubuntu.com-20070427183348-xgnpct0qd6a2ip34
Tags: upstream-1.2.9
ImportĀ upstreamĀ versionĀ 1.2.9

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//
 
2
// RandomStream.cpp
 
3
//
 
4
// $Id: //poco/1.2/Foundation/src/RandomStream.cpp#1 $
 
5
//
 
6
// Library: Foundation
 
7
// Package: Crypt
 
8
// Module:  RandomStream
 
9
//
 
10
// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
 
11
// and Contributors.
 
12
//
 
13
// Permission is hereby granted, free of charge, to any person or organization
 
14
// obtaining a copy of the software and accompanying documentation covered by
 
15
// this license (the "Software") to use, reproduce, display, distribute,
 
16
// execute, and transmit the Software, and to prepare derivative works of the
 
17
// Software, and to permit third-parties to whom the Software is furnished to
 
18
// do so, all subject to the following:
 
19
// 
 
20
// The copyright notices in the Software and this entire statement, including
 
21
// the above license grant, this restriction and the following disclaimer,
 
22
// must be included in all copies of the Software, in whole or in part, and
 
23
// all derivative works of the Software, unless such copies or derivative
 
24
// works are solely in the form of machine-executable object code generated by
 
25
// a source language processor.
 
26
// 
 
27
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 
28
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 
29
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
 
30
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
 
31
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
 
32
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 
33
// DEALINGS IN THE SOFTWARE.
 
34
//
 
35
 
 
36
 
 
37
#include "Poco/RandomStream.h"
 
38
#include "Poco/Random.h"
 
39
#include "Poco/SHA1Engine.h"
 
40
#if defined(POCO_OS_FAMILY_WINDOWS)
 
41
#include <windows.h>
 
42
#include <wincrypt.h>
 
43
#elif defined(POCO_OS_FAMILY_UNIX)
 
44
#include <fcntl.h>
 
45
#include <unistd.h>
 
46
#endif
 
47
#include <time.h>
 
48
 
 
49
 
 
50
namespace Poco {
 
51
 
 
52
 
 
53
RandomBuf::RandomBuf(): BufferedStreamBuf(256, std::ios::in)
 
54
{
 
55
}
 
56
 
 
57
 
 
58
RandomBuf::~RandomBuf()
 
59
{
 
60
}
 
61
 
 
62
 
 
63
int RandomBuf::readFromDevice(char* buffer, std::streamsize length)
 
64
{
 
65
        int n = 0;
 
66
 
 
67
#if defined(POCO_OS_FAMILY_WINDOWS)
 
68
        HCRYPTPROV hProvider = 0;
 
69
        CryptAcquireContext(&hProvider, 0, 0, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT);
 
70
        CryptGenRandom(hProvider, length, (BYTE*) buffer);
 
71
        CryptReleaseContext(hProvider, 0);
 
72
        n = length;
 
73
#else
 
74
        #if defined(POCO_OS_FAMILY_UNIX)
 
75
        int fd = open("/dev/urandom", O_RDONLY, 0);
 
76
        if (fd >= 0) 
 
77
        {
 
78
                n = read(fd, buffer, length);
 
79
                close(fd);
 
80
        }
 
81
        #endif
 
82
        if (n <= 0)
 
83
        {
 
84
                // x is here as a source of randomness, so it does not make
 
85
                // much sense to protect it with a Mutex.
 
86
                static UInt32 x = 0;
 
87
                Random rnd1(256);
 
88
                Random rnd2(64);
 
89
                x += rnd1.next();
 
90
 
 
91
                n = 0;
 
92
                SHA1Engine engine;
 
93
                UInt32 t = (UInt32) time(NULL);
 
94
                engine.update(&t, sizeof(t));
 
95
                void* p = this;
 
96
                engine.update(&p, sizeof(p));
 
97
                engine.update(buffer, length);
 
98
                UInt32 junk[32];
 
99
                engine.update(junk, sizeof(junk));
 
100
                while (n < length)
 
101
                {
 
102
                        for (int i = 0; i < 100; ++i)
 
103
                        {
 
104
                                UInt32 r = rnd2.next();
 
105
                                engine.update(&r, sizeof(r));
 
106
                                engine.update(&x, sizeof(x));
 
107
                                x += rnd1.next();
 
108
                        }
 
109
                        DigestEngine::Digest d = engine.digest();
 
110
                        for (DigestEngine::Digest::const_iterator it = d.begin(); it != d.end() && n < length; ++it, ++n)
 
111
                        {
 
112
                                engine.update(*it);
 
113
                                *buffer++ = *it++;
 
114
                        }
 
115
                }
 
116
        }
 
117
#endif
 
118
        return n;
 
119
}
 
120
 
 
121
 
 
122
RandomIOS::RandomIOS()
 
123
{
 
124
        poco_ios_init(&_buf);
 
125
}
 
126
 
 
127
 
 
128
RandomIOS::~RandomIOS()
 
129
{
 
130
}
 
131
 
 
132
 
 
133
RandomBuf* RandomIOS::rdbuf()
 
134
{
 
135
        return &_buf;
 
136
}
 
137
 
 
138
 
 
139
RandomInputStream::RandomInputStream(): std::istream(&_buf)
 
140
{
 
141
}
 
142
 
 
143
 
 
144
RandomInputStream::~RandomInputStream()
 
145
{
 
146
}
 
147
 
 
148
 
 
149
} // namespace Poco