62 lines
2.2 KiB
Kotlin
62 lines
2.2 KiB
Kotlin
import com.xenomachina.argparser.ArgParser
|
|
import com.xenomachina.argparser.default
|
|
import com.xenomachina.argparser.mainBody
|
|
import org.fusesource.jansi.internal.CLibrary.STDIN_FILENO
|
|
import org.fusesource.jansi.internal.CLibrary.isatty
|
|
import kotlin.system.exitProcess
|
|
|
|
class Args(parser: ArgParser) {
|
|
val encrypt by parser.flagging("-e", "--encrypt", help = "Encrypt stdin instead of decrypting")
|
|
|
|
val keyExchange by parser
|
|
.storing("-k", "--key-exchange", help = "Key-Exchange header value, required if nonce is not provided")
|
|
.default<String?>(null)
|
|
|
|
val cloudPassword by parser
|
|
.storing("-p", "--password", help = "Cloud password, if camera has been provisioned")
|
|
.default<String?>(null)
|
|
|
|
val username by parser
|
|
.storing("-u", "--username", help = "User name, either admin or none. Default admin")
|
|
.default<String>("admin")
|
|
|
|
val nonce by parser
|
|
.storing("-n", "--nonce", help = "Nonce, required if key-exchange is not provided")
|
|
.default<String?>(null)
|
|
}
|
|
|
|
fun main(args: Array<String>) = mainBody {
|
|
ArgParser(args).parseInto(::Args).run {
|
|
if (keyExchange == null && nonce == null) {
|
|
println("Either the Key-Exchange or the nonce must be provided!")
|
|
exitProcess(1)
|
|
}
|
|
if (cloudPassword == null) {
|
|
println("Cloud password not provided, using the default one for unprovisioned cameras")
|
|
}
|
|
if (isatty(STDIN_FILENO) == 1) {
|
|
println("Data to ${if (encrypt) "encrypt" else "decrypt"} must be sent to standard input!")
|
|
exitProcess(1)
|
|
}
|
|
val toProcess = System.`in`.readAllBytesCompat()
|
|
if (toProcess == null) {
|
|
println("Unable to read data from stdin!")
|
|
exitProcess(1)
|
|
}
|
|
|
|
val aes = if (keyExchange != null) {
|
|
StreamAesUtils.generateFromExchangeKeyAndSuperSecretKey(keyExchange, cloudPassword)
|
|
} else {
|
|
StreamAesUtils.fromUserNonceSuperSecretKey(username, nonce, cloudPassword)
|
|
}
|
|
|
|
val output = if (encrypt) {
|
|
aes.encrypt(toProcess)
|
|
} else {
|
|
aes.decrypt(toProcess)
|
|
}
|
|
|
|
System.out.write(output)
|
|
}
|
|
}
|