aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/CryptoHandler.kt
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/kotlin/CryptoHandler.kt')
-rw-r--r--src/main/kotlin/CryptoHandler.kt51
1 files changed, 0 insertions, 51 deletions
diff --git a/src/main/kotlin/CryptoHandler.kt b/src/main/kotlin/CryptoHandler.kt
deleted file mode 100644
index e3f33df..0000000
--- a/src/main/kotlin/CryptoHandler.kt
+++ /dev/null
@@ -1,51 +0,0 @@
-package space.anity
-
-import java.io.*
-import java.security.*
-import javax.crypto.*
-import javax.crypto.spec.*
-
-class CryptoHandler @Throws(NoSuchPaddingException::class, NoSuchAlgorithmException::class)
-internal constructor(private val secretKey: SecretKey, cipher: String) {
- private val cipher: Cipher = Cipher.getInstance(cipher)
-
- @Throws(InvalidKeyException::class, IOException::class)
- internal fun encrypt(content: String, fileName: String) {
- cipher.init(Cipher.ENCRYPT_MODE, secretKey)
- val iv = cipher.iv
-
- FileOutputStream(fileName).use { fileOut ->
- fileOut.write(iv)
- CipherOutputStream(fileOut, cipher).use { cipherOut ->
- cipherOut.write(content.toByteArray())
- }
- }
- }
-
- @Throws(InvalidAlgorithmParameterException::class, InvalidKeyException::class, IOException::class)
- internal fun decrypt(fileName: String): String {
- var content = ""
-
- FileInputStream(fileName).use { fileIn ->
- val iv = ByteArray(16)
- fileIn.read(iv)
- cipher.init(Cipher.DECRYPT_MODE, secretKey, IvParameterSpec(iv))
-
- CipherInputStream(fileIn, cipher).use { cipherIn ->
- InputStreamReader(cipherIn, Charsets.UTF_8).use { inputReader ->
- BufferedReader(inputReader).use { reader ->
- val sb = StringBuilder()
- var line: String? = reader.readLine()
- while (line != null) {
- sb.append(line + "\n")
- line = reader.readLine()
- }
- content = sb.toString()
- }
- }
- }
- }
-
- return content // TODO: Fix char handling as 1 byte in decryption
- }
-}