Skip to content

Commit

Permalink
Version 1.3.0.0, updated libraries, removed deprecated methods, added…
Browse files Browse the repository at this point in the history
… meaningful debug logging info
  • Loading branch information
cioccarellia committed Oct 25, 2018
1 parent 9a19536 commit 3b5a473
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 134 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ android {
buildToolsVersion "28.0.3"
minSdkVersion 15
targetSdkVersion 28
versionCode 7
versionName "1.2.0.2"
versionCode 8
versionName "1.3.0.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ buildscript {

dependencies {
classpath 'com.github.dcendents:android-maven-gradle-plugin:2.0'
classpath 'com.android.tools.build:gradle:3.2.0'
classpath 'com.android.tools.build:gradle:3.2.1'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.9-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-4.10-all.zip
Original file line number Diff line number Diff line change
Expand Up @@ -55,129 +55,10 @@ public class CryptoPrefs(context: Context, fileName: String, key: String, should
Double::class -> preferences.get(key, default).toDouble()
Short::class -> preferences.get(key, default).toShort()
Byte::class -> preferences.get(key, default).toByte()
else -> throw IllegalStateException("Cannot cast string to type ${default::class}, create your own extension function to parse it properly")
else -> throw IllegalStateException("Cannot cast string [$key] to type ${default::class}; create your own extension function to parse it properly")
} as T


/**
* Returns the String found in pair with the matching key.
* If no key is found in the file, the default value will
* be returned and then, a field containing the key and the
* given default value will be created on the preferences.
*
* @param key the key of the item that will be searched
* @param default the default value, in case the key doesn't
* exists in the file
* */
public fun getString(key: String, default: Any): String {
return preferences.get(key, default)
}


/**
* Returns the Boolean found in pair with the matching key.
* If no key is found in the file, the default value will
* be returned and then, a field containing the key and the
* given default value will be created on the preferences.
*
* @param key the key of the item that will be searched
* @param default the default value, in case the key doesn't
* exists in the file
* */
public fun getBoolean(key: String, default: Boolean): Boolean {
return preferences.get(key, default).toBoolean()
}


/**
* Returns the Integer found in pair with the matching key.
* If no key is found in the file, the default value will
* be returned and then, a field containing the key and the
* given default value will be created on the preferences.
*
* @param key the key of the item that will be searched
* @param default the default value, in case the key doesn't
* exists in the file
* */
public fun getInt(key: String, default: Number): Int {
return preferences.get(key, default).toInt()
}


/**
* Returns the Float found in pair with the matching key.
* If no key is found in the file, the default value will
* be returned and then, a field containing the key and the
* given default value will be created on the preferences.
*
* @param key the key of the item that will be searched
* @param default the default value, in case the key doesn't
* exists in the file
* */
public fun getFloat(key: String, default: Number): Float {
return preferences.get(key, default).toFloat()
}


/**
* Returns the Double found in pair with the matching key.
* If no key is found in the file, the default value will
* be returned and then, a field containing the key and the
* given default value will be created on the preferences.
*
* @param key the key of the item that will be searched
* @param default the default value, in case the key doesn't
* exists in the file
* */
public fun getDouble(key: String, default: Number): Double {
return preferences.get(key, default).toDouble()
}


/**
* Returns the Long found in pair with the matching key.
* If no key is found in the file, the default value will
* be returned and then, a field containing the key and the
* given default value will be created on the preferences.
*
* @param key the key of the item that will be searched
* @param default the default value, in case the key doesn't
* exists in the file
* */
public fun getLong(key: String, default: Number): Long {
return preferences.get(key, default).toLong()
}

/**
* Returns the Short found in pair with the matching key.
* If no key is found in the file, the default value will
* be returned and then, a field containing the key and the
* given default value will be created on the preferences.
*
* @param key the key of the item that will be searched
* @param default the default value, in case the key doesn't
* exists in the file
* */
public fun getShort(key: String, default: Number): Short {
return preferences.get(key, default).toShort()
}


/**
* Returns the Byte found in pair with the matching key.
* If no key is found in the file, the default value will
* be returned and then, a field containing the key and the
* given default value will be created on the preferences.
*
* @param key the key of the item that will be searched
* @param default the default value, in case the key doesn't
* exists in the file
* */
public fun getByte(key: String, default: Byte): Byte {
return preferences.get(key, default).toByte()
}


/**
* Enqueues a modification that is kept on a volatile copy
* of the file, also with every eventual new modification
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,30 +23,30 @@ import javax.crypto.spec.SecretKeySpec
* Part of the package com.andreacioccarelli.cryptoprefs.wrappers
*/

internal class PreferencesEncrypter(context: Context, auto: Pair<String, String>) : Wrapper {
internal class PreferencesEncrypter(context: Context, auth: Pair<String, String>) : Wrapper {

private var writer: Cipher
private var reader: Cipher
private var keyCrypt: Cipher

override val prefReader: SharedPreferences = context.getSharedPreferences(auto.first, Context.MODE_PRIVATE)
override val prefWriter: SharedPreferences.Editor = context.getSharedPreferences(auto.first, Context.MODE_PRIVATE).edit()
override val prefReader: SharedPreferences = context.getSharedPreferences(auth.first, Context.MODE_PRIVATE)
override val prefWriter: SharedPreferences.Editor = context.getSharedPreferences(auth.first, Context.MODE_PRIVATE).edit()

init {
try {
if (auto.second.isEmpty()) throw IllegalStateException("Encryption key length is 0")
if (auth.second.isEmpty()) throw IllegalStateException("Encryption key length is 0 [key = ${auth.second}]")

writer = Cipher.getInstance(transformation)
reader = Cipher.getInstance(transformation)
keyCrypt = Cipher.getInstance(transformation)

initializeCiphers(auto.second)
initializeCiphers(auth.second)
} catch (e: GeneralSecurityException) {
throw SecurePreferencesException(e, "Error while initializing the preferences ciphers keys")
throw SecurePreferencesException(e, "Error while initializing the preferences ciphers keys [file = ${auth.first}]")
} catch (e: UnsupportedEncodingException) {
throw SecurePreferencesException(e, "Error while initializing the preferences ciphers, unsupported charset.")
throw SecurePreferencesException(e, "Error while initializing the preferences ciphers, unsupported charset [file = ${auth.first}].")
} catch (e: KeyException) {
throw SecurePreferencesException(e, "Error while initializing the preferences ciphers.")
throw SecurePreferencesException(e, "Error while initializing the preferences ciphers [file = ${auth.first}].")
}
}

Expand Down Expand Up @@ -81,7 +81,7 @@ internal class PreferencesEncrypter(context: Context, auto: Pair<String, String>
try {
encodedValue = finalize(writer, value.toByteArray(charset(charset)))
} catch (e: UnsupportedEncodingException) {
throw SecurePreferencesException(e, "Error while initializing the encryption ciphers, unsupported charset.")
throw SecurePreferencesException(e, "Error while initializing the encryption ciphers, unsupported charset. [value = $value]")
}

return Base64.encodeToString(encodedValue, Base64.NO_WRAP)
Expand All @@ -95,7 +95,7 @@ internal class PreferencesEncrypter(context: Context, auto: Pair<String, String>
return try {
String(finalized, Charsets.UTF_8)
} catch (e: UnsupportedEncodingException) {
throw SecurePreferencesException(e, "Error while initializing the decryption ciphers, unsupported charset.")
throw SecurePreferencesException(e, "Error while initializing the decryption ciphers, unsupported charset. [value = $value]")
}
}

Expand Down

0 comments on commit 3b5a473

Please sign in to comment.