Remove one method class in crypto package

PiperOrigin-RevId: 402787577
This commit is contained in:
olly 2021-10-13 11:27:32 +01:00 committed by Oliver Woodman
parent 02719fd5b9
commit 0dc2567179
4 changed files with 32 additions and 47 deletions

View File

@ -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

View File

@ -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;
}

View File

@ -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;
}
}

View File

@ -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;
}
}