preface

Recently in the study of Kotlin, found a relatively good sharepreference library kotpref. It is implemented using Kotlin’s extensions and proxies, and is also quick and easy to use. However, there is still a want function, is encryption. I then wrote my own KotprefEncryptSupport to support it.

The installation

allprojects {
		repositories {
			maven { url 'https://jitpack.io'}}}Copy the code
dependencies {
    compile 'com. Making. Fly7632785: KotprefEncryptSupport: 1.0.1'
}
Copy the code

Initialize the

It already includes Gson-support, so if you use this library, you don’t need to plug in gson-support anymore

class SampleApplication : Application() {
    override fun onCreate(a) {
        super.onCreate()
        Kotpref.init(applicationContext)
        // add Encrypt Support 
        Kotpref.gson = Gson()
        Kotpref.cipherAdapter = SharedPrefCipherAdapter(applicationContext)
    }
}
Copy the code

That use

    var password by ecStringPref("jafirPass")
    var code1 by ecNullableStringPref()
    var isMan by ecBooleanPref(trueVar highScore1 by ecLongPref(111111111111l) var rate1 by ecFloatPref(0.5555f) var person1 by ecLongPref(111111111111l) var person1 by ecFloatPref(0.5555f) var person1 by ecGsonPref(Person("g jafir", 21))
    var avatar21 by ecGsonPref(Avatar())
    var avatar22 by ecGsonNullablePref(Avatar())
Copy the code

Support for Int, String, Boolean, Long, Float, Gson

senior

If you want to customize the encryption rules, you can. You just need to implement the CipherAdapter yourself, and then implement the encrypt and Decrypt methods. For example,

class SharedPrefCipherAdapter @Throws(Exception::class)
constructor(context: Context) : CipherAdapter {
    private val secretKey: SecretKey

    init {
        this.secretKey = AESUtil.generateKey(context)
    }

    override fun encrypt(raw: String): String {
        return AESUtil.execEncrypted(secretKey, raw)
    }

    override fun decrypt(encode: String): String {
        return AESUtil.execDecrypted(secretKey, encode)
    }
}
Copy the code

See the source code for more details

Default encryption

A default encryption Adapter has been integrated with the library: SharedPrefCipherAdapter uses a combination of AES and PBE encryption, where AES encrypts the content and PBE encrypts the secret key

Xml

<map>
    <long name="highScore" value="3901" />
    <float name="rate" value="0.4" />
    <string name="password">WUb7wV8SS18d9hEvUt8kPg==&#10;    </string>
    <string name="age1">vELsGwmt5Bhz1WkAuasEHA==&#10;    </string>
    <string name="avatar22">rN29eRFNgIlf6yIAV9cptoyabAkqmDqDtf6S4ElzPWIVS1YRMXw2avvYbyJseOZEOBqVE9kAAARV&#10; T4MpZ31fAw==&#10;</string>
    <string name="avatar1">null</string>
    <string name="avatar21">rN29eRFNgIlf6yIAV9cpttcgywAfWQ9P21mqhkpLjhty0xyusdIZtGLibaD5gzdExLQhyLF2BbIR&#10; Vz7hM0a0KA==&#10;</string>
    <string name="avatar">{&quot; icon&quot; :&quot; lion&quot; ,&quot; updated_at&quot; :&quot; Dec 19, 2017 11:13:28 PM&quot; }</string>
    <string name="person1">gA4aAC4rCqoo9Vz3VCBgVtnerDMep/WhMsoUK736qJ4=&#10;    </string>
    <string name="code">451B65F6-EF95-4C2C-AE76-D34535F51B3B</string>
    <string name="isMan">gi8S6qu7Sklcx0oiYDGnsw==&#10;    </string>
    <set name="prizes">
        <string>New Born</string>
    </set>
    <int name="age" value="2" />
    <string name="highScore1">L5E9zu5NO5AQGFVZBmmHTA==&#10;    </string>
    <string name="name">chibatching Jr</string>
    <string name="rate1">Pa0WPZj6Of7DV8S4LYfp2g==&#10;    </string>
    <string name="code1">FZbcxuspUwL0HUdYvuQ6ltT/nWL+e5d3ZXtfTfPXGcccThyKavFb+7iB1bR8PGF6&#10;    </string>
    <string name="gameLevel">EASY</string>
</map>

Copy the code