Skip to content

Instantly share code, notes, and snippets.

View lordcodes's full-sized avatar
⌨️
Coding

Andrew Lord lordcodes

⌨️
Coding
View GitHub Profile
@lordcodes
lordcodes / CountingSink.kt
Created February 25, 2020 20:18
Code for the article: "Uploading a file with progress in Kotlin"
typealias CountingRequestListener = (bytesWritten: Long, contentLength: Long) -> Unit
class CountingSink(
sink: Sink,
private val requestBody: RequestBody,
private val onProgressUpdate: CountingRequestListener
) : ForwardingSink(sink) {
private var bytesWritten = 0L
override fun write(source: Buffer, byteCount: Long) {
@lordcodes
lordcodes / CountingRequestBody.kt
Created February 25, 2020 20:17
Code for the article: "Uploading a file with progress in Kotlin"
class CountingRequestBody(
private val requestBody: RequestBody,
private val onProgressUpdate: CountingRequestListener
) : RequestBody() {
override fun contentType() = requestBody.contentType()
@Throws(IOException::class)
override fun contentLength() = requestBody.contentLength()
...
@lordcodes
lordcodes / AttachmentUploader.kt
Created February 25, 2020 20:15
Code for the article "Uploading a file with progress in Kotlin"
fun createUploadRequestBody(file: File, mimeType: String) =
file.asRequestBody(mimeType.toMediaType())
@lordcodes
lordcodes / AttachmentRemoteApi.kt
Created February 25, 2020 20:10
Code for the article: "Uploading a file with progress in Kotlin"
@Multipart
@POST("file")
fun attachFile(
@Part("name") filename: RequestBody,
@Part("type") mimeType: RequestBody,
@Part("size") fileSize: RequestBody,
@Part filePart: MultipartBody.Part
): Single<AttachmentUploadedRemoteDto>
@lordcodes
lordcodes / EncryptionEngine.kt
Created February 16, 2020 21:37
Code for the article: "Protecting secrets in an Android project"
fun decrypt(encryptedData: ByteArray): String? {
val encryptionKey = generateKey()
val cell = SecureCell(encryptionKey, SecureCell.MODE_SEAL)
return try {
val cellData = SecureCellData(encryptedData, null)
val decodedData = cell.unprotect(encryptionContext, cellData)
String(decodedData)
} catch (error: SecureCellException) {
Log.e("EncryptionEngine", "Failed to decrypt message, error)
null
@lordcodes
lordcodes / EncryptionEngine.kt
Created February 16, 2020 21:36
Code for the article: "Protecting secrets in an Android project"
val encryptedDaya = Base64.decode(secretAsBase64, Base64.NO_WRAP)
@lordcodes
lordcodes / EncryptionEngine.kt
Created February 16, 2020 21:35
Code for the article: "Protecting secrets in an Android project"
val encypted = EncryptionEngine().encrypt("raw_secret_value")
Log.d("ENCRYPTED", encypted.base64EncodedString())
@lordcodes
lordcodes / EncryptionEngine.kt
Created February 16, 2020 21:35
Code for the article: "Protecting secrets in an Android project"
fun encrypt(message: String): ByteArray {
val encryptionKey = generateKey()
val cell = SecureCell(encryptionKey, SecureCell.MODE_SEAL)
val protectedData = cell.protect(
encryptionContext, message.toByteArray()
)
return protectedData.protectedData
}
private val encryptionContext: ByteArray? = null
@lordcodes
lordcodes / EncryptionEngine.kt
Created February 16, 2020 21:34
Code for the article: "Protecting secrets in an Android project"
fun generateKey(): ByteArray {
val rawKey = buildString(5) {
append(byteArrayOf(0x12, 0x27, 0x42).base64EncodedString())
append(500 + 6 / 7 * 89)
append(BuildConfig.ENCRYPTION_KEY)
append("pghy^%£aft")
}
return rawKey.toByteArray()
}
@lordcodes
lordcodes / build.gradle.kts
Created February 16, 2020 21:33
Code for the article: "Protecting secrets in an Android project"
GameCatalogueApp_EncryptionKey=super_secret_key
buildConfigField(
"String",
"ENCRYPTION_KEY",
buildConfigProperty("GameCatalogueApp_EncryptionKey")
)