OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/quic/crypto/quic_random.h" | 5 #include "net/quic/crypto/quic_random.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/memory/singleton.h" | 8 #include "base/memory/singleton.h" |
9 #include "crypto/random.h" | 9 #include "crypto/random.h" |
10 | 10 |
11 namespace net { | 11 namespace net { |
12 | 12 |
13 namespace { | 13 namespace { |
14 | 14 |
15 class DefaultRandom : public QuicRandom { | 15 class DefaultRandom : public QuicRandom { |
16 public: | 16 public: |
17 static DefaultRandom* GetInstance(); | 17 static DefaultRandom* GetInstance(); |
18 | 18 |
19 // QuicRandom implementation | 19 // QuicRandom implementation |
20 virtual void RandBytes(void* data, size_t len) OVERRIDE; | 20 virtual void RandBytes(void* data, size_t len) OVERRIDE; |
21 virtual uint64 RandUint64() OVERRIDE; | 21 virtual uint64 RandUint64() OVERRIDE; |
22 virtual bool RandBool() OVERRIDE; | |
23 virtual void Reseed(const void* additional_entropy, | 22 virtual void Reseed(const void* additional_entropy, |
24 size_t entropy_len) OVERRIDE; | 23 size_t entropy_len) OVERRIDE; |
25 | 24 |
26 private: | 25 private: |
27 DefaultRandom(); | 26 DefaultRandom(); |
28 virtual ~DefaultRandom() {} | 27 virtual ~DefaultRandom() {} |
29 | 28 |
30 friend struct DefaultSingletonTraits<DefaultRandom>; | 29 friend struct DefaultSingletonTraits<DefaultRandom>; |
31 DISALLOW_COPY_AND_ASSIGN(DefaultRandom); | 30 DISALLOW_COPY_AND_ASSIGN(DefaultRandom); |
32 }; | 31 }; |
33 | 32 |
34 DefaultRandom* DefaultRandom::GetInstance() { | 33 DefaultRandom* DefaultRandom::GetInstance() { |
35 return Singleton<DefaultRandom>::get(); | 34 return Singleton<DefaultRandom>::get(); |
36 } | 35 } |
37 | 36 |
38 void DefaultRandom::RandBytes(void* data, size_t len) { | 37 void DefaultRandom::RandBytes(void* data, size_t len) { |
39 crypto::RandBytes(data, len); | 38 crypto::RandBytes(data, len); |
40 } | 39 } |
41 | 40 |
42 uint64 DefaultRandom::RandUint64() { | 41 uint64 DefaultRandom::RandUint64() { |
43 uint64 value; | 42 uint64 value; |
44 RandBytes(&value, sizeof(value)); | 43 RandBytes(&value, sizeof(value)); |
45 return value; | 44 return value; |
46 } | 45 } |
47 | 46 |
48 bool DefaultRandom::RandBool() { | |
49 char value; | |
50 RandBytes(&value, sizeof(value)); | |
51 return (value & 1) == 1; | |
52 } | |
53 | |
54 void DefaultRandom::Reseed(const void* additional_entropy, size_t entropy_len) { | 47 void DefaultRandom::Reseed(const void* additional_entropy, size_t entropy_len) { |
55 // No such function exists in crypto/random.h. | 48 // No such function exists in crypto/random.h. |
56 } | 49 } |
57 | 50 |
58 DefaultRandom::DefaultRandom() { | 51 DefaultRandom::DefaultRandom() { |
59 } | 52 } |
60 | 53 |
61 } // namespace | 54 } // namespace |
62 | 55 |
63 // static | 56 // static |
64 QuicRandom* QuicRandom::GetInstance() { return DefaultRandom::GetInstance(); } | 57 QuicRandom* QuicRandom::GetInstance() { return DefaultRandom::GetInstance(); } |
65 | 58 |
66 } // namespace net | 59 } // namespace net |
OLD | NEW |