mirror of
https://github.com/recloudstream/cloudstream.git
synced 2024-08-15 01:53:11 +00:00
comply
This commit is contained in:
parent
58ee0554f4
commit
b872930934
1 changed files with 8 additions and 8 deletions
|
@ -13,14 +13,17 @@ import javax.crypto.spec.SecretKeySpec
|
||||||
|
|
||||||
object AesHelper {
|
object AesHelper {
|
||||||
|
|
||||||
|
private const val HASH = "AES/CBC/PKCS5PADDING"
|
||||||
|
private const val KDF = "MD5"
|
||||||
|
|
||||||
fun cryptoAESHandler(
|
fun cryptoAESHandler(
|
||||||
data: String,
|
data: String,
|
||||||
pass: ByteArray,
|
pass: ByteArray,
|
||||||
encrypt: Boolean = true,
|
encrypt: Boolean = true,
|
||||||
padding: String = "AES/CBC/PKCS5PADDING",
|
padding: String = HASH,
|
||||||
): String? {
|
): String? {
|
||||||
val parse = AppUtils.tryParseJson<AesData>(data) ?: return null
|
val parse = AppUtils.tryParseJson<AesData>(data) ?: return null
|
||||||
val (key, iv) = generateKeyAndIv(pass, parse.s.hexToByteArray()) ?: throw ErrorLoadingException("failed to generate key")
|
val (key, iv) = generateKeyAndIv(pass, parse.s.hexToByteArray()) ?: return null
|
||||||
val cipher = Cipher.getInstance(padding)
|
val cipher = Cipher.getInstance(padding)
|
||||||
return if (!encrypt) {
|
return if (!encrypt) {
|
||||||
cipher.init(Cipher.DECRYPT_MODE, SecretKeySpec(key, "AES"), IvParameterSpec(iv))
|
cipher.init(Cipher.DECRYPT_MODE, SecretKeySpec(key, "AES"), IvParameterSpec(iv))
|
||||||
|
@ -35,11 +38,11 @@ object AesHelper {
|
||||||
fun generateKeyAndIv(
|
fun generateKeyAndIv(
|
||||||
password: ByteArray,
|
password: ByteArray,
|
||||||
salt: ByteArray,
|
salt: ByteArray,
|
||||||
hashAlgorithm: String = "MD5",
|
hashAlgorithm: String = KDF,
|
||||||
keyLength: Int = 32,
|
keyLength: Int = 32,
|
||||||
ivLength: Int = 16,
|
ivLength: Int = 16,
|
||||||
iterations: Int = 1
|
iterations: Int = 1
|
||||||
): List<ByteArray>? {
|
): Pair<ByteArray,ByteArray>? {
|
||||||
|
|
||||||
val md = MessageDigest.getInstance(hashAlgorithm)
|
val md = MessageDigest.getInstance(hashAlgorithm)
|
||||||
val digestLength = md.digestLength
|
val digestLength = md.digestLength
|
||||||
|
@ -70,10 +73,7 @@ object AesHelper {
|
||||||
|
|
||||||
generatedLength += digestLength
|
generatedLength += digestLength
|
||||||
}
|
}
|
||||||
return listOf(
|
return generatedData.copyOfRange(0, keyLength) to generatedData.copyOfRange(keyLength, targetKeySize)
|
||||||
generatedData.copyOfRange(0, keyLength),
|
|
||||||
generatedData.copyOfRange(keyLength, targetKeySize)
|
|
||||||
)
|
|
||||||
} catch (e: DigestException) {
|
} catch (e: DigestException) {
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue