Skip to content

Commit

Permalink
AVRO-4016: Use SecureRandom for file sync markers (#3016)
Browse files Browse the repository at this point in the history
  • Loading branch information
opwvhk committed Jul 11, 2024
1 parent 7e04c38 commit 25d8684
Showing 1 changed file with 16 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,15 @@
*/
package org.apache.avro.file;

import static java.nio.charset.StandardCharsets.UTF_8;
import org.apache.avro.AvroRuntimeException;
import org.apache.avro.Schema;
import org.apache.avro.file.DataFileStream.DataBlock;
import org.apache.avro.generic.GenericDatumReader;
import org.apache.avro.io.BinaryEncoder;
import org.apache.avro.io.DatumWriter;
import org.apache.avro.io.EncoderFactory;
import org.apache.avro.util.NonCopyingByteArrayOutputStream;
import org.apache.commons.compress.utils.IOUtils;

import java.io.BufferedOutputStream;
import java.io.Closeable;
Expand All @@ -29,22 +37,12 @@
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.function.Function;

import org.apache.avro.AvroRuntimeException;
import org.apache.avro.Schema;
import org.apache.avro.file.DataFileStream.DataBlock;
import org.apache.avro.generic.GenericDatumReader;
import org.apache.avro.io.BinaryEncoder;
import org.apache.avro.io.DatumWriter;
import org.apache.avro.io.EncoderFactory;
import org.apache.avro.util.NonCopyingByteArrayOutputStream;
import org.apache.commons.compress.utils.IOUtils;
import static java.nio.charset.StandardCharsets.UTF_8;

/**
* Stores in a file a sequence of data conforming to a schema. The schema is
Expand Down Expand Up @@ -263,15 +261,12 @@ private void init(OutputStream outs) throws IOException {
this.isOpen = true;
}

private static final SecureRandom RNG = new SecureRandom();

private static byte[] generateSync() {
try {
MessageDigest digester = MessageDigest.getInstance("MD5");
long time = System.currentTimeMillis();
digester.update((UUID.randomUUID() + "@" + time).getBytes(UTF_8));
return digester.digest();
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
byte[] sync = new byte[16];
RNG.nextBytes(sync);
return sync;
}

private DataFileWriter<D> setMetaInternal(String key, byte[] value) {
Expand Down

0 comments on commit 25d8684

Please sign in to comment.