2020-07-02 21:24:12 +00:00
|
|
|
/*
|
|
|
|
Mangadex@Home
|
|
|
|
Copyright (c) 2020, MangaDex Network
|
|
|
|
This file is part of MangaDex@Home.
|
|
|
|
|
|
|
|
MangaDex@Home is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
MangaDex@Home is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this MangaDex@Home. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
/* ktlint-disable no-wildcard-imports */
|
|
|
|
package mdnet.base.server
|
|
|
|
|
|
|
|
import com.goterl.lazycode.lazysodium.LazySodiumJava
|
|
|
|
import com.goterl.lazycode.lazysodium.exceptions.SodiumException
|
|
|
|
import com.goterl.lazycode.lazysodium.interfaces.Box
|
2020-08-11 16:47:04 +00:00
|
|
|
import java.security.MessageDigest
|
|
|
|
import javax.crypto.Cipher
|
|
|
|
import javax.crypto.spec.SecretKeySpec
|
2020-07-02 21:24:12 +00:00
|
|
|
|
|
|
|
@Throws(SodiumException::class)
|
2020-08-11 16:47:04 +00:00
|
|
|
internal fun LazySodiumJava.cryptoBoxOpenEasyAfterNm(cipherBytes: ByteArray, nonce: ByteArray, sharedKey: ByteArray): String {
|
2020-07-02 21:24:12 +00:00
|
|
|
if (!Box.Checker.checkNonce(nonce.size)) {
|
|
|
|
throw SodiumException("Incorrect nonce length.")
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!Box.Checker.checkBeforeNmBytes(sharedKey.size)) {
|
|
|
|
throw SodiumException("Incorrect shared secret key length.")
|
|
|
|
}
|
|
|
|
|
|
|
|
val message = ByteArray(cipherBytes.size - Box.MACBYTES)
|
|
|
|
val res: Boolean = cryptoBoxOpenEasyAfterNm(message, cipherBytes, cipherBytes.size.toLong(), nonce, sharedKey)
|
|
|
|
if (!res) {
|
|
|
|
throw SodiumException("Could not fully complete shared secret key decryption.")
|
|
|
|
}
|
|
|
|
return str(message)
|
|
|
|
}
|
2020-08-11 16:47:04 +00:00
|
|
|
|
|
|
|
internal fun getRc4(key: ByteArray): Cipher {
|
|
|
|
val rc4 = Cipher.getInstance("RC4")
|
|
|
|
rc4.init(Cipher.ENCRYPT_MODE, SecretKeySpec(key, "RC4"))
|
|
|
|
return rc4
|
|
|
|
}
|
|
|
|
|
|
|
|
internal fun md5Bytes(stringToHash: String): ByteArray {
|
|
|
|
val digest = MessageDigest.getInstance("MD5")
|
|
|
|
return digest.digest(stringToHash.toByteArray())
|
|
|
|
}
|
|
|
|
|
|
|
|
internal fun printHexString(bytes: ByteArray): String {
|
|
|
|
val sb = StringBuilder()
|
|
|
|
for (b in bytes) {
|
|
|
|
sb.append(String.format("%02x", b))
|
|
|
|
}
|
|
|
|
return sb.toString()
|
|
|
|
}
|