Browse Source

HADOOP-10713. Refactor CryptoCodec#generateSecureRandom to take a byte[]. (wang via yliu)

git-svn-id: https://svn.apache.org/repos/asf/hadoop/common/branches/fs-encryption@1604537 13f79535-47bb-0310-9956-ffa450edef68
Yi Liu 11 years ago
parent
commit
f43f0999d9

+ 3 - 0
hadoop-common-project/hadoop-common/CHANGES-fs-encryption.txt

@@ -25,6 +25,9 @@ fs-encryption (Unreleased)
     HADOOP-10662. NullPointerException in CryptoInputStream while wrapped
     stream is not ByteBufferReadable. Add tests using normal stream. (Yi Liu)
 
+    HADOOP-10713. Refactor CryptoCodec#generateSecureRandom to take a byte[]. 
+    (wang via yliu)
+
   OPTIMIZATIONS
 
   BUG FIXES

+ 5 - 4
hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/crypto/CryptoCodec.java

@@ -81,9 +81,10 @@ public abstract class CryptoCodec implements Configurable {
   public abstract void calculateIV(byte[] initIV, long counter, byte[] IV);
   
   /**
-   * Generate secure random.
-   * @param bytes length of the secure random
-   * @return byte[] the secure random
+   * Generate a number of secure, random bytes suitable for cryptographic use.
+   * This method needs to be thread-safe.
+   *
+   * @param bytes byte array to populate with random data
    */
-  public abstract byte[] generateSecureRandom(int bytes);
+  public abstract void generateSecureRandom(byte[] bytes);
 }

+ 2 - 4
hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/crypto/JCEAESCTRCryptoCodec.java

@@ -79,10 +79,8 @@ public class JCEAESCTRCryptoCodec extends AESCTRCryptoCodec {
   }
   
   @Override
-  public byte[] generateSecureRandom(int bytes) {
-    final byte[] data = new byte[bytes];
-    random.nextBytes(data);
-    return data;
+  public void generateSecureRandom(byte[] bytes) {
+    random.nextBytes(bytes);
   }  
   
   private static class JCEAESCTRCipher implements Encryptor, Decryptor {

+ 4 - 2
hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/crypto/TestCryptoCodec.java

@@ -49,8 +49,10 @@ public class TestCryptoCodec {
   }
   
   private void checkSecureRandom(int len) {
-    byte[] rand = codec.generateSecureRandom(len);
-    byte[] rand1 = codec.generateSecureRandom(len);
+    byte[] rand = new byte[len];
+    byte[] rand1 = new byte[len];
+    codec.generateSecureRandom(rand);
+    codec.generateSecureRandom(rand1);
     
     Assert.assertEquals(len, rand.length);
     Assert.assertEquals(len, rand1.length);