Notes
Notes - notes.io |
private val encryptedPrefs by lazy {
EncryptedSharedPreferences.create(
PREFS_FILE_NAME,
masterKeyAlias,
applicationContext,
EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV,
EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM
)
}
private val file by lazy { File(filesDir, ENCRYPTED_FILE_NAME) }
private val encryptedFile by lazy {
EncryptedFile.Builder(file,
applicationContext,
masterKeyAlias,
EncryptedFile.FileEncryptionScheme.AES256_GCM_HKDF_4KB
).build()
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_secuirty)
handleClickListeners()
}
fun handleClickListeners(){
btn_save_pref.setOnClickListener {
saveIntoSharedPreferences(ed_input_information.text.toString())
}
btn_load_pref.setOnClickListener {
val result = getDataFromSharedPreferences() ?: "No Data Found"
tv_result.text = result
}
btn_download.setOnClickListener { downloadAndEncryptFile() }
btn_delete.setOnClickListener {
val deleteStatus = deleteFile()
tv_result.text = if(deleteStatus) {"File deleted"} else { "File is not available" }
}
btn_encrypted_data.setOnClickListener {
readFile{file.inputStream()}
}
btn_decrypted_data.setOnClickListener {
readFile { encryptedFile.openFileInput() }
}
}
fun saveIntoSharedPreferences(value: String){
encryptedPrefs.edit().putString(ENC_KEY, value).apply()
tv_result.text = "Data Save in to shared Prefs with ($value)"
}
fun getDataFromSharedPreferences(): String? = encryptedPrefs.getString(ENC_KEY, null)
fun deleteFile(): Boolean{
var success: Boolean = false
if(file.exists()){
file.delete()
success = true
}
return success
}
private fun downloadAndEncryptFile() {
if (file.exists()) {
Log.i("TAG", "Encrypted file already exists!")
tv_result.text = "file already Exist"
} else {
Log.i("TAG", "Encrypted file does not exist exists! Downloading...")
val request = Request.Builder().url(FILE_URL).build()
okHttpClient.newCall(request).enqueue(
object : Callback {
override fun onFailure(call: Call, e: IOException) {
runOnUiThread { tv_result.text = e.message }
Log.e("TAG", "Error occurred!", e)
}
override fun onResponse(call: Call, response: Response) {
val result = response.body!!.bytes()
runOnUiThread { onFileDownloaded(result) }
}
}
)
}
}
private fun onFileDownloaded(bytes: ByteArray) {
var encryptedOutputStream: FileOutputStream? = null
try {
encryptedOutputStream = encryptedFile.openFileOutput().apply {
write(bytes)
}
tv_result.text = "File Downloaded and Encrypted"
} catch (e: Exception) {
Log.e("TAG", "Could not open encrypted file", e)
tv_result.text = e.message
} finally {
encryptedOutputStream?.close()
}
}
private fun readFile(fileInput: () -> FileInputStream) {
Log.i("TAG", "Loading file...")
var fileInputStream: FileInputStream? = null
try {
fileInputStream = fileInput()
val reader = BufferedReader(InputStreamReader(fileInputStream))
val stringBuilder = StringBuilder()
reader.forEachLine { line -> stringBuilder.appendln(line) }
tv_result.text = stringBuilder.toString()
} catch (e: Exception) {
Log.e("TAG", "Error occurred when reading file", e)
tv_result.text = e.message
} finally {
fileInputStream?.close()
}
}
companion object{
val masterKeyAlias = MasterKeys.getOrCreate(MasterKeys.AES256_GCM_SPEC)
val okHttpClient: OkHttpClient = OkHttpClient.Builder().build()
const val ENC_KEY = "ENCRYPT_KEY"
const val PREFS_FILE_NAME = "MY_ENCRYPTED_PRERS_FILE_NAME"
const val ENCRYPTED_FILE_NAME = "MY_ENCRYPT_FILE_NAME"
const val FILE_URL = "https://raw.githubusercontent.com/salmanyaqoob/react-monster-app/master/README.md"
}
}
|
Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...
With notes.io;
- * You can take a note from anywhere and any device with internet connection.
- * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
- * You can quickly share your contents without website, blog and e-mail.
- * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
- * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.
Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.
Easy: Notes.io doesn’t require installation. Just write and share note!
Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )
Free: Notes.io works for 12 years and has been free since the day it was started.
You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;
Email: [email protected]
Twitter: http://twitter.com/notesio
Instagram: http://instagram.com/notes.io
Facebook: http://facebook.com/notesio
Regards;
Notes.io Team