Kotlin KotprefEncryptSupport加密sharepreference

前言

最近在學(xué)習(xí)kotlin,發(fā)現(xiàn)一個比較不錯的sharepreference庫kotpref。
它是利用kotlin的擴展和代理來實現(xiàn)的,使用起來也方便快捷。
但是,就是還差一個想要的功能,就是加密。
然后自己寫了個KotprefEncryptSupport來支持一下。

安裝

allprojects {
        repositories {
            maven { url 'https://jitpack.io' }
        }
    }
dependencies {
    compile 'com.github.fly7632785:KotprefEncryptSupport:1.0.1'
}

初始化

它已經(jīng)包含了gson-support,所以,如果使用了這個庫,就不用再接入gson-support了

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

申明使用

    var password by ecStringPref("jafirPass")
    var code1 by ecNullableStringPref()
    var isMan by ecBooleanPref(true)
    var age1 by ecIntPref(23)
    var highScore1 by ecLongPref(1111111111L)
    var rate1 by ecFloatPref(0.5555f)
    var person1 by ecGsonPref(Person("g jafir", 21))
    var avatar21 by ecGsonPref(Avatar())
    var avatar22 by ecGsonNullablePref(Avatar())

支持 Int,String,Boolean,Long,Float,Gson

高級

如果你想自定義加密規(guī)則,也是可以的。
只需要自己實現(xiàn)一下CipherAdapter,然后實現(xiàn)一下encrypt和decrypt兩個方法就可以了。例如

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)
    }
}

更多的細節(jié)可以看源碼

默認的加密

庫中已經(jīng)集成了一個默認的加密adapter:SharedPrefCipherAdapter
采用的是AES、PBE混合的加密方式,AES加密內(nèi)容,然后PBE加密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>

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,533評論 19 139
  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 178,872評論 25 709
  • 概述 之前一直對加密相關(guān)的算法知之甚少,只知道類似DES、RSA等加密算法能對數(shù)據(jù)傳輸進行加密,且各種加密算法各有...
    Henryzhu閱讀 3,208評論 0 14
  • 創(chuàng)意字母4
    LLL_Lynne閱讀 112評論 0 0
  • 我來時微風(fēng)細雨,萬物始出, 晨風(fēng)徐徐,潤物無聲 我長大時陽光明媚,燦爛輝煌, 反射著艷陽,撐起一片陰涼 我老去時顏...
    愛bo兒閱讀 173評論 0 1

友情鏈接更多精彩內(nèi)容