mirror of
https://github.com/recloudstream/cloudstream.git
synced 2024-08-15 01:53:11 +00:00
Helper Added: CryptoJS
This commit is contained in:
parent
9e6e28009c
commit
b616e50b00
1 changed files with 8 additions and 8 deletions
|
@ -4,7 +4,7 @@ import java.net.*
|
||||||
import java.util.*
|
import java.util.*
|
||||||
import java.security.*
|
import java.security.*
|
||||||
import kotlin.math.min
|
import kotlin.math.min
|
||||||
import android.util.Base64
|
import java.util.Base64
|
||||||
import javax.crypto.Cipher
|
import javax.crypto.Cipher
|
||||||
import javax.crypto.spec.SecretKeySpec
|
import javax.crypto.spec.SecretKeySpec
|
||||||
import javax.crypto.spec.IvParameterSpec
|
import javax.crypto.spec.IvParameterSpec
|
||||||
|
@ -35,7 +35,7 @@ object CryptoJS {
|
||||||
val saltBytes = generateSalt(8)
|
val saltBytes = generateSalt(8)
|
||||||
val key = ByteArray(KEY_SIZE / 8)
|
val key = ByteArray(KEY_SIZE / 8)
|
||||||
val iv = ByteArray(IV_SIZE / 8)
|
val iv = ByteArray(IV_SIZE / 8)
|
||||||
EvpKDF(password.toByteArray(), KEY_SIZE, IV_SIZE, saltBytes, key, iv)
|
evpkdf(password.toByteArray(), KEY_SIZE, IV_SIZE, saltBytes, key, iv)
|
||||||
|
|
||||||
val keyS = SecretKeySpec(key, AES)
|
val keyS = SecretKeySpec(key, AES)
|
||||||
val cipher = Cipher.getInstance(HASH_CIPHER)
|
val cipher = Cipher.getInstance(HASH_CIPHER)
|
||||||
|
@ -51,7 +51,7 @@ object CryptoJS {
|
||||||
System.arraycopy(saltBytes, 0, b, sBytes.size, saltBytes.size)
|
System.arraycopy(saltBytes, 0, b, sBytes.size, saltBytes.size)
|
||||||
System.arraycopy(cipherText, 0, b, sBytes.size + saltBytes.size, cipherText.size)
|
System.arraycopy(cipherText, 0, b, sBytes.size + saltBytes.size, cipherText.size)
|
||||||
|
|
||||||
val bEncode = Base64.encode(b, Base64.NO_WRAP)
|
val bEncode = Base64.getEncoder().encode(b)
|
||||||
return String(bEncode)
|
return String(bEncode)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -62,13 +62,13 @@ object CryptoJS {
|
||||||
* @param cipherText encrypted string
|
* @param cipherText encrypted string
|
||||||
*/
|
*/
|
||||||
fun decrypt(password: String, cipherText: String): String {
|
fun decrypt(password: String, cipherText: String): String {
|
||||||
val ctBytes = Base64.decode(cipherText.toByteArray(), Base64.NO_WRAP)
|
val ctBytes = Base64.getDecoder().decode(cipherText.toByteArray())
|
||||||
val saltBytes = Arrays.copyOfRange(ctBytes, 8, 16)
|
val saltBytes = Arrays.copyOfRange(ctBytes, 8, 16)
|
||||||
val cipherTextBytes = Arrays.copyOfRange(ctBytes, 16, ctBytes.size)
|
val cipherTextBytes = Arrays.copyOfRange(ctBytes, 16, ctBytes.size)
|
||||||
|
|
||||||
val key = ByteArray(KEY_SIZE / 8)
|
val key = ByteArray(KEY_SIZE / 8)
|
||||||
val iv = ByteArray(IV_SIZE / 8)
|
val iv = ByteArray(IV_SIZE / 8)
|
||||||
EvpKDF(password.toByteArray(), KEY_SIZE, IV_SIZE, saltBytes, key, iv)
|
evpkdf(password.toByteArray(), KEY_SIZE, IV_SIZE, saltBytes, key, iv)
|
||||||
|
|
||||||
val cipher = Cipher.getInstance(HASH_CIPHER)
|
val cipher = Cipher.getInstance(HASH_CIPHER)
|
||||||
val keyS = SecretKeySpec(key, AES)
|
val keyS = SecretKeySpec(key, AES)
|
||||||
|
@ -78,12 +78,12 @@ object CryptoJS {
|
||||||
return String(plainText)
|
return String(plainText)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun EvpKDF(password: ByteArray, keySize: Int, ivSize: Int, salt: ByteArray, resultKey: ByteArray, resultIv: ByteArray): ByteArray {
|
private fun evpkdf(password: ByteArray, keySize: Int, ivSize: Int, salt: ByteArray, resultKey: ByteArray, resultIv: ByteArray): ByteArray {
|
||||||
return EvpKDF(password, keySize, ivSize, salt, 1, KDF_DIGEST, resultKey, resultIv)
|
return evpkdf(password, keySize, ivSize, salt, 1, KDF_DIGEST, resultKey, resultIv)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Suppress("NAME_SHADOWING")
|
@Suppress("NAME_SHADOWING")
|
||||||
private fun EvpKDF(password: ByteArray, keySize: Int, ivSize: Int, salt: ByteArray, iterations: Int, hashAlgorithm: String, resultKey: ByteArray, resultIv: ByteArray): ByteArray {
|
private fun evpkdf(password: ByteArray, keySize: Int, ivSize: Int, salt: ByteArray, iterations: Int, hashAlgorithm: String, resultKey: ByteArray, resultIv: ByteArray): ByteArray {
|
||||||
val keySize = keySize / 32
|
val keySize = keySize / 32
|
||||||
val ivSize = ivSize / 32
|
val ivSize = ivSize / 32
|
||||||
val targetKeySize = keySize + ivSize
|
val targetKeySize = keySize + ivSize
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue