diff --git a/library/core/src/main/java/com/google/android/exoplayer2/upstream/crypto/AesCipherDataSink.java b/library/core/src/main/java/com/google/android/exoplayer2/upstream/crypto/AesCipherDataSink.java index 691e496c3b..4e5b9f2b8e 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/upstream/crypto/AesCipherDataSink.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/upstream/crypto/AesCipherDataSink.java @@ -66,10 +66,12 @@ public final class AesCipherDataSink implements DataSink { @Override public void open(DataSpec dataSpec) throws IOException { wrappedDataSink.open(dataSpec); - long nonce = CryptoUtil.getFNV64Hash(dataSpec.key); cipher = new AesFlushingCipher( - Cipher.ENCRYPT_MODE, secretKey, nonce, dataSpec.uriPositionOffset + dataSpec.position); + Cipher.ENCRYPT_MODE, + secretKey, + dataSpec.key, + dataSpec.uriPositionOffset + dataSpec.position); } @Override diff --git a/library/core/src/main/java/com/google/android/exoplayer2/upstream/crypto/AesCipherDataSource.java b/library/core/src/main/java/com/google/android/exoplayer2/upstream/crypto/AesCipherDataSource.java index 52352729eb..98ec914fa0 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/upstream/crypto/AesCipherDataSource.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/upstream/crypto/AesCipherDataSource.java @@ -51,10 +51,12 @@ public final class AesCipherDataSource implements DataSource { @Override public long open(DataSpec dataSpec) throws IOException { long dataLength = upstream.open(dataSpec); - long nonce = CryptoUtil.getFNV64Hash(dataSpec.key); cipher = new AesFlushingCipher( - Cipher.DECRYPT_MODE, secretKey, nonce, dataSpec.uriPositionOffset + dataSpec.position); + Cipher.DECRYPT_MODE, + secretKey, + dataSpec.key, + dataSpec.uriPositionOffset + dataSpec.position); return dataLength; } diff --git a/library/core/src/main/java/com/google/android/exoplayer2/upstream/crypto/AesFlushingCipher.java b/library/core/src/main/java/com/google/android/exoplayer2/upstream/crypto/AesFlushingCipher.java index b539d31ab9..96cb13604e 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/upstream/crypto/AesFlushingCipher.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/upstream/crypto/AesFlushingCipher.java @@ -15,6 +15,7 @@ */ package com.google.android.exoplayer2.upstream.crypto; +import androidx.annotation.Nullable; import com.google.android.exoplayer2.util.Assertions; import com.google.android.exoplayer2.util.Util; import java.nio.ByteBuffer; @@ -42,6 +43,10 @@ public final class AesFlushingCipher { private int pendingXorBytes; + public AesFlushingCipher(int mode, byte[] secretKey, @Nullable String nonce, long offset) { + this(mode, secretKey, getFNV64Hash(nonce), offset); + } + public AesFlushingCipher(int mode, byte[] secretKey, long nonce, long offset) { try { cipher = Cipher.getInstance("AES/CTR/NoPadding"); @@ -121,4 +126,23 @@ public final class AesFlushingCipher { private byte[] getInitializationVector(long nonce, long counter) { return ByteBuffer.allocate(16).putLong(nonce).putLong(counter).array(); } + + /** + * Returns the hash value of the input as a long using the 64 bit FNV-1a hash function. The hash + * values produced by this function are less likely to collide than those produced by {@link + * #hashCode()}. + */ + private static long getFNV64Hash(@Nullable String input) { + if (input == null) { + return 0; + } + + long hash = 0; + for (int i = 0; i < input.length(); i++) { + hash ^= input.charAt(i); + // This is equivalent to hash *= 0x100000001b3 (the FNV magic prime number). + hash += (hash << 1) + (hash << 4) + (hash << 5) + (hash << 7) + (hash << 8) + (hash << 40); + } + return hash; + } } diff --git a/library/core/src/main/java/com/google/android/exoplayer2/upstream/crypto/CryptoUtil.java b/library/core/src/main/java/com/google/android/exoplayer2/upstream/crypto/CryptoUtil.java deleted file mode 100644 index 20681ee15a..0000000000 --- a/library/core/src/main/java/com/google/android/exoplayer2/upstream/crypto/CryptoUtil.java +++ /dev/null @@ -1,43 +0,0 @@ -/* - * Copyright (C) 2016 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.google.android.exoplayer2.upstream.crypto; - -import androidx.annotation.Nullable; - -/** Utility functions for the crypto package. */ -/* package */ final class CryptoUtil { - - private CryptoUtil() {} - - /** - * Returns the hash value of the input as a long using the 64 bit FNV-1a hash function. The hash - * values produced by this function are less likely to collide than those produced by {@link - * #hashCode()}. - */ - public static long getFNV64Hash(@Nullable String input) { - if (input == null) { - return 0; - } - - long hash = 0; - for (int i = 0; i < input.length(); i++) { - hash ^= input.charAt(i); - // This is equivalent to hash *= 0x100000001b3 (the FNV magic prime number). - hash += (hash << 1) + (hash << 4) + (hash << 5) + (hash << 7) + (hash << 8) + (hash << 40); - } - return hash; - } -}