Код: Выделить всё
package ssh.code.editor
import android.security.keystore.KeyGenParameterSpec
import android.security.keystore.KeyProperties
import java.security.KeyPair
import java.security.KeyPairGenerator
import java.security.KeyStore
class KeysManager {
var error = ""
fun generateKeyPair(name:String, size:Int): KeyPair? {
try{
val parameter = KeyGenParameterSpec.Builder(name, KeyProperties.PURPOSE_SIGN or KeyProperties.PURPOSE_VERIFY).apply {
setDigests(KeyProperties.DIGEST_SHA256, KeyProperties.DIGEST_SHA512)
setSignaturePaddings(KeyProperties.SIGNATURE_PADDING_RSA_PKCS1)
setKeySize(size)
}.build()
return KeyPairGenerator.getInstance(KeyProperties.KEY_ALGORITHM_RSA, "AndroidKeyStore").apply { initialize(parameter) }.generateKeyPair()
}catch (e:Exception){
error=""
error+=e.message
return null
}
}
fun getPublicKey(name:String):String{
val keyStore: KeyStore = KeyStore.getInstance("AndroidKeyStore").apply {
load(null)
}
val entry = keyStore.getEntry(name, null)
if(entry !is KeyStore.PrivateKeyEntry){
error="Key is not present"
return ""
}
return String(entry.certificate.publicKey.encoded)
}
}
Код: Выделить всё
package ssh.code.editor
import com.jcraft.jsch.ChannelExec
import com.jcraft.jsch.ChannelSftp
import com.jcraft.jsch.ChannelSftp.LsEntry
import com.jcraft.jsch.JSch
import com.jcraft.jsch.JSchException
import com.jcraft.jsch.Session
import java.io.ByteArrayOutputStream
import java.io.InputStream
import java.util.Vector
class SSH(host: String, user: String, private var key: String, port: Int = 22) {
//ERROR variable
var error:String=""
//JSCH variables
private var jsch = JSch()
private var session:Session?=try {
jsch.getSession(host, user, port)
}
catch(e:JSchException){
error+=e.message
null
}?.
apply {
setPassword(key)
connect()
}
private var execChannel : ChannelExec = session?.openChannel("exec") as ChannelExec
private var sftpChannel : ChannelSftp = session?.openChannel("sftp") as ChannelSftp
private fun inputStreamToString(inputStream: InputStream):String{
val byteOut = ByteArrayOutputStream()
val buffer = ByteArray(1024)
while (inputStream.read(buffer)!=-1){
byteOut.write(buffer, 0, 1024)
}
return byteOut.toString("UTF-8")
}
fun runCommand(command: String):String{
if(execChannel.isClosed) return ""
execChannel.setCommand(command)
execChannel.connect()
val s = inputStreamToString(execChannel.inputStream)
execChannel.disconnect()
return s
}
@Suppress("UNCHECKED_CAST")
fun itemInDirectory(path: String):Array{
var result= emptyArray()
for (entry:LsEntry in sftpChannel.ls(path) as Vector){
result+=entry.filename
}
return result
}
fun getFile(serverPath:String) = inputStreamToString(sftpChannel.get(serverPath))
fun loadFile(serverPath:String, data:String) = sftpChannel.put(data.byteInputStream(), serverPath)
}
Так есть ли это? способ использования ACH с хранилищем ключей или есть другой SSH API, который я могу использовать вместо ACH
Я пытался найти ответ в Интернете, но ничего не нашел
Поэтому я прошу помощи, пожалуйста
Подробнее здесь: https://stackoverflow.com/questions/792 ... h-keystore