Skip to content

Instantly share code, notes, and snippets.

Avatar
🇵🇰
Passion to help others 😊

Ali Azaz Alam AliAzaz

🇵🇰
Passion to help others 😊
View GitHub Profile
@AliAzaz
AliAzaz / FlowThrottleDebounce.kt
Created Oct 22, 2021 — forked from chibatching/FlowThrottleDebounce.kt
Throttle and Debounce on Flow Kotlin Coroutines
View FlowThrottleDebounce.kt
fun <T> Flow<T>.throttle(waitMillis: Int) = flow {
coroutineScope {
val context = coroutineContext
var nextMillis = 0L
var delayPost: Deferred<Unit>? = null
collect {
val current = SystemClock.uptimeMillis()
if (nextMillis < current) {
nextMillis = current + waitMillis
@AliAzaz
AliAzaz / EncryptFiles.kt
Created Oct 9, 2021
Encrypted Files in Jetpack Security
View EncryptFiles.kt
// Although you can define your own key generation parameter specification, it's
// recommended that you use the value specified here.
val keyGenParameterSpec = MasterKeys.AES256_GCM_SPEC
val mainKeyAlias = MasterKeys.getOrCreate(keyGenParameterSpec)
val encrypFile = EncryptedFile.Builder(
File(directoryPath, "file_name"),
context,
mainKeyAlias,
EncryptedFile.FileEncryptionScheme.AES256_GCM_HKDF_4KB
@AliAzaz
AliAzaz / EncryptSharedPref.kt
Created Oct 9, 2021
Encrypted SharedPreference in Jetpack Security
View EncryptSharedPref.kt
// Although you can define your own key generation parameter specification, it's
// recommended that you use the value specified here.
val keyGenParameterSpec = MasterKeys.AES256_GCM_SPEC
val mainKeyAlias = MasterKeys.getOrCreate(keyGenParameterSpec)
val sharedPref = EncryptedSharedPreferences.create(
"preference_name",
mainKeyAlias,
context,
EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV,
@AliAzaz
AliAzaz / AdvMasterKey.kt
Last active Oct 6, 2021
Master key with advanced specification in Jetpack Security
View AdvMasterKey.kt
fun generateAdvMasterKey(context: Context): MasterKey {
val advSpec = KeyGenParameterSpec.Builder(
"key_name", // define your master key name
KeyProperties.PURPOSE_ENCRYPT or KeyProperties.PURPOSE_DECRYPT
).apply {
setBlockModes(KeyProperties.BLOCK_MODE_GCM)
setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_NONE)
setKeySize(256)
setUserAuthenticationRequired(true)
setUserAuthenticationValidityDurationSeconds(120)
@AliAzaz
AliAzaz / MasterKey.kt
Created Oct 5, 2021
Get MasterKey using JetpackSecurity
View MasterKey.kt
fun getMasterKey(context: Context): MasterKey {
return
MasterKey.Builder(context)
.setKeyGenParameterSpec(AES256_GCM_SPEC)
.build()
}
@AliAzaz
AliAzaz / Activity.kt
Last active Sep 19, 2021
Generic RecyclerAdapter implementtion using DiffUtils
View Activity.kt
private lateinit var adapter: GenericListAdapter<ImagesInfo>
adapter = GenericListAdapter(R.layout.product_view) { item, position ->
viewModel.setSelectedProduct(item)
findNavController().navigate(ImageListFragmentDirections.actionImageListFragmentToImageDetailFragment())
}
@AliAzaz
AliAzaz / MediaStorageFetchingFromManager.kt
Created Aug 9, 2021
Get full file path from a URI that points to a any document
View MediaStorageFetchingFromManager.kt
/*
* Credit to: https://stackoverflow.com/a/60642994/9764941
* */
object ImageMediaStoreFetching {
@JvmStatic
@SuppressLint("NewApi")
fun getPath(uri: Uri): String? {
val selection: String?
val selectionArgs: Array<String>?
@AliAzaz
AliAzaz / Keys.kt
Created Jul 7, 2021
Use this file to load cpp library file in NDK api_key process
View Keys.kt
package edu.practice.utils.shared
object Keys {
init {
System.loadLibrary("native-lib")
}
external fun apiKey(): String
}
@AliAzaz
AliAzaz / lib.cpp
Created Jul 7, 2021
.cpp file to secure key
View lib.cpp
#include <jni.h>
#include <string>
extern "C"
JNIEXPORT jstring JNICALL
Java_edu_practice_utils_shared_Keys_apiKey(JNIEnv *env, jobject thiz) {
std::string api_key = "c527aa8fadcb58f1cccef75e3a64a3ae";
return env->NewStringUTF(api_key.c_str());
}
@AliAzaz
AliAzaz / CMakesList.txt
Created Jul 7, 2021
CMakesList file used in NDK process to secure key
View CMakesList.txt
# For more information about using CMake with Android Studio, read the
# documentation: https://d.android.com/studio/projects/add-native-code.html
# Sets the minimum version of CMake required to build the native library.
cmake_minimum_required(VERSION 3.4.1)
# Creates and names a library, sets it as either STATIC
# or SHARED, and provides the relative paths to its source code.
# You can define multiple libraries, and CMake builds them for you.