Remove one method class in crypto package
PiperOrigin-RevId: 402787577
This commit is contained in:
parent
02719fd5b9
commit
0dc2567179
@ -66,10 +66,12 @@ public final class AesCipherDataSink implements DataSink {
|
|||||||
@Override
|
@Override
|
||||||
public void open(DataSpec dataSpec) throws IOException {
|
public void open(DataSpec dataSpec) throws IOException {
|
||||||
wrappedDataSink.open(dataSpec);
|
wrappedDataSink.open(dataSpec);
|
||||||
long nonce = CryptoUtil.getFNV64Hash(dataSpec.key);
|
|
||||||
cipher =
|
cipher =
|
||||||
new AesFlushingCipher(
|
new AesFlushingCipher(
|
||||||
Cipher.ENCRYPT_MODE, secretKey, nonce, dataSpec.uriPositionOffset + dataSpec.position);
|
Cipher.ENCRYPT_MODE,
|
||||||
|
secretKey,
|
||||||
|
dataSpec.key,
|
||||||
|
dataSpec.uriPositionOffset + dataSpec.position);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -51,10 +51,12 @@ public final class AesCipherDataSource implements DataSource {
|
|||||||
@Override
|
@Override
|
||||||
public long open(DataSpec dataSpec) throws IOException {
|
public long open(DataSpec dataSpec) throws IOException {
|
||||||
long dataLength = upstream.open(dataSpec);
|
long dataLength = upstream.open(dataSpec);
|
||||||
long nonce = CryptoUtil.getFNV64Hash(dataSpec.key);
|
|
||||||
cipher =
|
cipher =
|
||||||
new AesFlushingCipher(
|
new AesFlushingCipher(
|
||||||
Cipher.DECRYPT_MODE, secretKey, nonce, dataSpec.uriPositionOffset + dataSpec.position);
|
Cipher.DECRYPT_MODE,
|
||||||
|
secretKey,
|
||||||
|
dataSpec.key,
|
||||||
|
dataSpec.uriPositionOffset + dataSpec.position);
|
||||||
return dataLength;
|
return dataLength;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -15,6 +15,7 @@
|
|||||||
*/
|
*/
|
||||||
package com.google.android.exoplayer2.upstream.crypto;
|
package com.google.android.exoplayer2.upstream.crypto;
|
||||||
|
|
||||||
|
import androidx.annotation.Nullable;
|
||||||
import com.google.android.exoplayer2.util.Assertions;
|
import com.google.android.exoplayer2.util.Assertions;
|
||||||
import com.google.android.exoplayer2.util.Util;
|
import com.google.android.exoplayer2.util.Util;
|
||||||
import java.nio.ByteBuffer;
|
import java.nio.ByteBuffer;
|
||||||
@ -42,6 +43,10 @@ public final class AesFlushingCipher {
|
|||||||
|
|
||||||
private int pendingXorBytes;
|
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) {
|
public AesFlushingCipher(int mode, byte[] secretKey, long nonce, long offset) {
|
||||||
try {
|
try {
|
||||||
cipher = Cipher.getInstance("AES/CTR/NoPadding");
|
cipher = Cipher.getInstance("AES/CTR/NoPadding");
|
||||||
@ -121,4 +126,23 @@ public final class AesFlushingCipher {
|
|||||||
private byte[] getInitializationVector(long nonce, long counter) {
|
private byte[] getInitializationVector(long nonce, long counter) {
|
||||||
return ByteBuffer.allocate(16).putLong(nonce).putLong(counter).array();
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -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;
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
x
Reference in New Issue
Block a user