diff --git a/library/src/main/java/com/github/markowanga/timberloggingtofile/LogManager.kt b/library/src/main/java/com/github/markowanga/timberloggingtofile/LogManager.kt index 2100cda..a3fbd81 100644 --- a/library/src/main/java/com/github/markowanga/timberloggingtofile/LogManager.kt +++ b/library/src/main/java/com/github/markowanga/timberloggingtofile/LogManager.kt @@ -3,6 +3,8 @@ package com.github.markowanga.timberloggingtofile import android.content.Context import androidx.core.content.ContextCompat import java.io.File +import java.time.Instant +import java.time.temporal.ChronoUnit object LogManager { @@ -24,4 +26,12 @@ object LogManager { } } + fun removeFilesOlderThanDays(daysCount: Long, rootFile: File) { + val minimumBefore = Instant.now().minus(daysCount, ChronoUnit.DAYS) + (rootFile.listFiles()?.toList() ?: listOf()) + .filter { it.isFile } + .filter { Instant.ofEpochMilli(it.lastModified()).isBefore(minimumBefore) } + .forEach { it.delete() } + } + } diff --git a/library/src/main/java/com/github/markowanga/timberloggingtofile/TextCrypt.kt b/library/src/main/java/com/github/markowanga/timberloggingtofile/TextCrypt.kt index 0eb6dce..3523867 100644 --- a/library/src/main/java/com/github/markowanga/timberloggingtofile/TextCrypt.kt +++ b/library/src/main/java/com/github/markowanga/timberloggingtofile/TextCrypt.kt @@ -1,20 +1,14 @@ package com.github.markowanga.timberloggingtofile import android.annotation.SuppressLint -import android.util.Base64 import javax.crypto.Cipher import javax.crypto.SecretKey import javax.crypto.spec.SecretKeySpec -fun ByteArray.toBase64(): String = Base64.encodeToString(this, Base64.DEFAULT) -fun String.toBytesFromBase64(): ByteArray = - Base64.decode(this, Base64.DEFAULT) +class TextCrypt(password: String) { - -class TextCrypt(private val password: String) { - - private val secretKey: SecretKey = generateKey() + private val secretKey: SecretKey = generateKey(password) @SuppressLint("GetInstance") private val encryptCipher = Cipher.getInstance("AES/ECB/PKCS5Padding") @@ -24,7 +18,7 @@ class TextCrypt(private val password: String) { private val decryptCipher = Cipher.getInstance("AES/ECB/PKCS5Padding") .also { it.init(Cipher.DECRYPT_MODE, secretKey) } - private fun generateKey(): SecretKey { + private fun generateKey(password: String): SecretKey { return SecretKeySpec(password.toByteArray(), "AES") } diff --git a/library/src/main/java/com/github/markowanga/timberloggingtofile/Utils.kt b/library/src/main/java/com/github/markowanga/timberloggingtofile/Utils.kt new file mode 100644 index 0000000..bd170aa --- /dev/null +++ b/library/src/main/java/com/github/markowanga/timberloggingtofile/Utils.kt @@ -0,0 +1,10 @@ +package com.github.markowanga.timberloggingtofile + +import android.util.Base64 + + +fun ByteArray.toBase64(): String = Base64.encodeToString(this, Base64.DEFAULT) + +fun String.toBytesFromBase64(): ByteArray = + Base64.decode(this, Base64.DEFAULT) +