aboutsummaryrefslogtreecommitdiffhomepage
path: root/Firestore/Source
diff options
context:
space:
mode:
authorGravatar Gil <mcg@google.com>2017-11-27 12:05:23 -0800
committerGravatar GitHub <noreply@github.com>2017-11-27 12:05:23 -0800
commit8c4cd9d5413e7f458001016e5faace2b610a69ce (patch)
tree61f1ff95d2f9b830fbcf3e32aff0472cf7f779c6 /Firestore/Source
parent782408ff5850027898ebb0f9d16cbda8272e0d32 (diff)
Change FSTExponentialBackoff to use firestore::SecureRandom directly (#492)
* Migrate FSTExponentialBackoff to Objective-C++ * Change FSTExponentialBackoff to use firestore::SecureRandom directly
Diffstat (limited to 'Firestore/Source')
-rw-r--r--Firestore/Source/Remote/FSTExponentialBackoff.mm (renamed from Firestore/Source/Remote/FSTExponentialBackoff.m)13
-rw-r--r--Firestore/Source/Util/FSTUtil.h3
-rw-r--r--Firestore/Source/Util/FSTUtil.m6
3 files changed, 10 insertions, 12 deletions
diff --git a/Firestore/Source/Remote/FSTExponentialBackoff.m b/Firestore/Source/Remote/FSTExponentialBackoff.mm
index 2e06238..ad27c25 100644
--- a/Firestore/Source/Remote/FSTExponentialBackoff.m
+++ b/Firestore/Source/Remote/FSTExponentialBackoff.mm
@@ -16,9 +16,12 @@
#import "Firestore/Source/Remote/FSTExponentialBackoff.h"
+#include <random>
+
+#include "Firestore/src/support/secure_random.h"
+
#import "Firestore/Source/Util/FSTDispatchQueue.h"
#import "Firestore/Source/Util/FSTLogger.h"
-#import "Firestore/Source/Util/FSTUtil.h"
@interface FSTExponentialBackoff ()
- (instancetype)initWithDispatchQueue:(FSTDispatchQueue *)dispatchQueue
@@ -33,7 +36,9 @@
@property(nonatomic) NSTimeInterval currentBase;
@end
-@implementation FSTExponentialBackoff
+@implementation FSTExponentialBackoff {
+ firestore::SecureRandom _secureRandom;
+}
- (instancetype)initWithDispatchQueue:(FSTDispatchQueue *)dispatchQueue
initialDelay:(NSTimeInterval)initialDelay
@@ -90,7 +95,9 @@
/** Returns a random value in the range [-currentBase/2, currentBase/2] */
- (NSTimeInterval)jitterDelay {
- return ([FSTUtil randomDouble] - 0.5) * _currentBase;
+ std::uniform_real_distribution<double> dist;
+ double random_double = dist(_secureRandom);
+ return (random_double - 0.5) * _currentBase;
}
@end
diff --git a/Firestore/Source/Util/FSTUtil.h b/Firestore/Source/Util/FSTUtil.h
index 3985d10..230c72a 100644
--- a/Firestore/Source/Util/FSTUtil.h
+++ b/Firestore/Source/Util/FSTUtil.h
@@ -20,9 +20,6 @@ NS_ASSUME_NONNULL_BEGIN
@interface FSTUtil : NSObject
-/** Generates a random double between 0 and 1. */
-+ (double)randomDouble;
-
/** Generates a random ID suitable for use as a document ID. */
+ (NSString *)autoID;
diff --git a/Firestore/Source/Util/FSTUtil.m b/Firestore/Source/Util/FSTUtil.m
index a25cd7e..47a8821 100644
--- a/Firestore/Source/Util/FSTUtil.m
+++ b/Firestore/Source/Util/FSTUtil.m
@@ -18,18 +18,12 @@
NS_ASSUME_NONNULL_BEGIN
-static const double kArc4RandomMax = 0x100000000;
-
static const int kAutoIDLength = 20;
static NSString *const kAutoIDAlphabet =
@"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
@implementation FSTUtil
-+ (double)randomDouble {
- return ((double)arc4random() / kArc4RandomMax);
-}
-
+ (NSString *)autoID {
unichar autoID[kAutoIDLength];
for (int i = 0; i < kAutoIDLength; i++) {