diff options
Diffstat (limited to 'app/src/main/java/org/pacien/tincapp/utils/TincKeyring.kt')
-rw-r--r-- | app/src/main/java/org/pacien/tincapp/utils/TincKeyring.kt | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/app/src/main/java/org/pacien/tincapp/utils/TincKeyring.kt b/app/src/main/java/org/pacien/tincapp/utils/TincKeyring.kt new file mode 100644 index 0000000..422763f --- /dev/null +++ b/app/src/main/java/org/pacien/tincapp/utils/TincKeyring.kt | |||
@@ -0,0 +1,26 @@ | |||
1 | package org.pacien.tincapp.utils | ||
2 | |||
3 | import android.os.ParcelFileDescriptor | ||
4 | import org.pacien.tincapp.commands.TincApp | ||
5 | import java.io.File | ||
6 | import java.io.FileNotFoundException | ||
7 | |||
8 | /** | ||
9 | * @author pacien | ||
10 | */ | ||
11 | object TincKeyring { | ||
12 | fun needsPassphrase(netName: String) = try { | ||
13 | TincApp.listPrivateKeys(netName).filter { it.exists() }.any { PemUtils.isEncrypted(PemUtils.read(it)) } | ||
14 | } catch (e: FileNotFoundException) { | ||
15 | false | ||
16 | } | ||
17 | |||
18 | fun openPrivateKey(f: File?, passphrase: String?): ParcelFileDescriptor? { | ||
19 | if (f == null || !f.exists() || passphrase == null) return null | ||
20 | val pipe = ParcelFileDescriptor.createPipe() | ||
21 | val decryptedKey = PemUtils.decrypt(PemUtils.read(f), passphrase) | ||
22 | val outputStream = ParcelFileDescriptor.AutoCloseOutputStream(pipe[1]) | ||
23 | PemUtils.write(decryptedKey, outputStream.writer()) | ||
24 | return pipe[0] | ||
25 | } | ||
26 | } | ||