Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(730)

Unified Diff: remoting/android/java/src/org/chromium/chromoting/SecureRandomInitializer.java

Issue 463393002: Android Chromoting: Initialize SecureRandom generator. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Pull initialization code into a reusable class Created 6 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: remoting/android/java/src/org/chromium/chromoting/SecureRandomInitializer.java
diff --git a/remoting/android/java/src/org/chromium/chromoting/SecureRandomInitializer.java b/remoting/android/java/src/org/chromium/chromoting/SecureRandomInitializer.java
new file mode 100644
index 0000000000000000000000000000000000000000..cf731ed31054de66a661a015a008c036a8abe013
--- /dev/null
+++ b/remoting/android/java/src/org/chromium/chromoting/SecureRandomInitializer.java
@@ -0,0 +1,41 @@
+// Copyright 2014 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+package org.chromium.chromoting;
palmer 2014/08/18 17:47:52 Is there some more general place we can put this s
Lambros 2014/08/18 20:31:31 Probably base/android, to be accessible to our app
+
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.security.SecureRandom;
+
+/**
+ * This class contains code to initialize a SecureRandom generator securely on Android platforms
+ * <= 4.3. See
+ * {@link http://android-developers.blogspot.com/2013/08/some-securerandom-thoughts.html}.
+ */
+public class SecureRandomInitializer {
+ private static final int NUM_RANDOM_BYTES = 16;
+
+ /**
+ * Safely initializes the random number generator, by seeding it with data from /dev/urandom.
+ */
+ public static void initialize(SecureRandom generator) throws IOException {
+ FileInputStream fis = null;
palmer 2014/08/18 17:47:52 Nit: Reference types are always initialized to nul
Lambros 2014/08/18 20:31:31 Acknowledged.
+ try {
+ fis = new FileInputStream("/dev/urandom");
+ byte[] bytes = new byte[NUM_RANDOM_BYTES];
palmer 2014/08/18 17:47:52 Nit: You could optimize a bit by making the array
Lambros 2014/08/18 20:31:31 Acknowledged.
+ if (bytes.length != fis.read(bytes)) {
+ throw new IOException("Failed to get enough random data.");
+ }
+ generator.setSeed(bytes);
+ } finally {
+ try {
+ if (fis != null) {
+ fis.close();
+ }
+ } catch (IOException e) {
+ // Ignore exception closing the device.
+ }
+ }
+ }
+}

Powered by Google App Engine
This is Rietveld 408576698