-
Notifications
You must be signed in to change notification settings - Fork 207
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added 260 new Crypto algorithms by #1589
- Loading branch information
Showing
33 changed files
with
594 additions
and
349 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
app/src/main/java/ru/tech/imageresizershrinker/app/presentation/components/utils/Security.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
* ImageToolbox is an image editor for android | ||
* Copyright (c) 2025 T8RIN (Malik Mukhametzyanov) | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* | ||
* 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. | ||
* | ||
* You should have received a copy of the Apache License | ||
* along with this program. If not, see <http://www.apache.org/licenses/LICENSE-2.0>. | ||
*/ | ||
|
||
package ru.tech.imageresizershrinker.app.presentation.components.utils | ||
|
||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.launch | ||
import org.bouncycastle.asn1.ASN1ObjectIdentifier | ||
import org.bouncycastle.jce.provider.BouncyCastleProvider | ||
import org.bouncycastle.operator.DefaultAlgorithmNameFinder | ||
import ru.tech.imageresizershrinker.core.domain.model.CipherType | ||
import ru.tech.imageresizershrinker.core.domain.model.HashingType | ||
import java.security.Security | ||
import kotlin.text.all | ||
|
||
internal fun registerSecurityProviders() { | ||
Security.removeProvider(BouncyCastleProvider.PROVIDER_NAME) | ||
Security.addProvider(BouncyCastleProvider()) | ||
|
||
HashingType.registerSecurityMessageDigests( | ||
Security.getAlgorithms("MessageDigest").filterNotNull() | ||
) | ||
|
||
CoroutineScope(Dispatchers.Default).launch { | ||
val finder = DefaultAlgorithmNameFinder() | ||
|
||
CipherType.registerSecurityCiphers( | ||
Security.getAlgorithms("Cipher").filterNotNull().map { cipher -> | ||
val oid = cipher.removePrefix("OID.") | ||
if (oid.all { it.isDigit() || it.isWhitespace() || it == '.' }) { | ||
finder.getAlgorithmName( | ||
ASN1ObjectIdentifier(oid) | ||
) | ||
} else { | ||
oid | ||
} | ||
} | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
core/domain/src/main/kotlin/ru/tech/imageresizershrinker/core/domain/model/CipherType.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/* | ||
* ImageToolbox is an image editor for android | ||
* Copyright (c) 2025 T8RIN (Malik Mukhametzyanov) | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* | ||
* 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. | ||
* | ||
* You should have received a copy of the Apache License | ||
* along with this program. If not, see <http://www.apache.org/licenses/LICENSE-2.0>. | ||
*/ | ||
|
||
package ru.tech.imageresizershrinker.core.domain.model | ||
|
||
@ConsistentCopyVisibility | ||
/** | ||
* [CipherType] multiplatform domain wrapper for java Cipher, in order to add custom digests, you need to call [registerSecurityCiphers] when process created | ||
**/ | ||
data class CipherType private constructor( | ||
val cipher: String, | ||
val name: String = cipher | ||
) { | ||
companion object { | ||
val AES_NO_PADDING = CipherType("AES/GCM/NoPadding") | ||
|
||
private var securityCiphers: List<String>? = null | ||
|
||
fun registerSecurityCiphers(ciphers: List<String>) { | ||
if (!securityCiphers.isNullOrEmpty()) { | ||
throw IllegalArgumentException("SecurityCiphers already registered") | ||
} | ||
securityCiphers = ciphers.distinctBy { it.replace("OID.", "").uppercase() } | ||
} | ||
|
||
val entries: List<CipherType> by lazy { | ||
val available = securityCiphers?.mapNotNull { cipher -> | ||
if (cipher.isEmpty()) null | ||
else { | ||
val strippedCipher = cipher.replace("OID.", "") | ||
SecureAlgorithmsMapping.findMatch(strippedCipher)?.let { mapping -> | ||
CipherType( | ||
cipher = cipher, | ||
name = mapping.algorithm | ||
) | ||
} ?: CipherType(cipher = cipher) | ||
} | ||
}?.sortedBy { it.cipher } ?: emptyList() | ||
|
||
listOf( | ||
AES_NO_PADDING | ||
).let { | ||
it + available | ||
}.distinctBy { it.name.replace("-", "") } | ||
} | ||
|
||
fun fromString( | ||
cipher: String? | ||
): CipherType? = cipher?.let { | ||
entries.find { | ||
it.cipher == cipher | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.