{"id":1565,"date":"2021-12-21T13:38:12","date_gmt":"2021-12-21T05:38:12","guid":{"rendered":"https:\/\/badgameshow.com\/fly\/?p=1565"},"modified":"2022-12-14T13:49:21","modified_gmt":"2022-12-14T05:49:21","slug":"android-annotation-%e5%88%a9%e7%94%a8%e8%a8%bb%e8%a7%a3%e4%b9%9f%e5%8f%af%e4%bb%a5md5","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/fly\/android-annotation-%e5%88%a9%e7%94%a8%e8%a8%bb%e8%a7%a3%e4%b9%9f%e5%8f%af%e4%bb%a5md5\/","title":{"rendered":"\u3010Android\u3011Annotation \u8a3b\u89e3 \u81ea\u5b9a\u7fa9\u5be6\u73fe MD5 \u529f\u80fd"},"content":{"rendered":"<h1>\u3010Android\u3011Annotation \u8a3b\u89e3 \u81ea\u5b9a\u7fa9\u5be6\u73fe MD5 \u529f\u80fd<\/h1>\n<h4>Android Annotation\u662f\u4e00\u7a2e\u6846\u67b6\uff0c\u7528\u65bc\u5feb\u901f\u69cb\u5efaAndroid\u61c9\u7528\u7a0b\u5f0f\u3002<\/h4>\n<h4>\u5b83\u4f7f\u7528\u8a3b\u91cb\u4f86\u5b9a\u7fa9\u6a21\u578b\uff0c\u63a7\u5236\u5668\uff0c\u8996\u5716\u548c\u8def\u7531\uff0c\u7136\u5f8c\u751f\u6210\u9ad8\u8cea\u91cf\u7684\u4ee3\u78bc\u4ee5\u907f\u514d\u8207\u7279\u5b9a\u7684\u6280\u8853\u7d30\u7bc0\u7684\u91cd\u5fa9\u6372\u7a4d\u3002<\/h4>\n<h4>Android Annotation\u7684\u512a\u9ede\u5728\u65bc\u5176\u4f4e\u8026\u5408\uff0c\u6709\u52a9\u65bc\u958b\u767c\u4eba\u54e1\u66f4\u5feb\u901f\uff0c\u66f4\u6709\u6548\u5730\u8655\u7406\u50cfUI\u63a7\u5236\u9805\u548c\u6578\u64da\u6a21\u578b\u7b49\u529f\u80fd\u3002\u5b83\u4e0d\u4f46\u53ef\u4ee5\u5927\u5927\u7e2e\u77ed\u958b\u767c\u6642\u9593\uff0c\u800c\u4e14\u53ef\u4ee5\u78ba\u4fdd\u7522\u54c1\u8cea\u91cf\u3002<\/h4>\n<hr \/>\n<h4>\u6587\u7ae0\u76ee\u9304<\/h4>\n<ol>\n<li><a href=\"#a\">\u5275\u5efa Annotation<\/a><\/li>\n<li><a href=\"#b\">\u6dfb\u52a0 MD5 \u5de5\u5177\u985e<\/a><\/li>\n<li><a href=\"#c\">\u5275\u5efa Annotation \u5be6\u73fe\u7684\u65b9\u6cd5<\/a><\/li>\n<li><a href=\"#d\">\u4f7f\u7528 Annotation \u7684Model<\/a><\/li>\n<li><a href=\"#e\">\u7a0b\u5f0f\u78bc\u7bc4\u4f8b<\/a><\/li>\n<li><a href=\"#f\">\u6548\u679c\u5c55\u793a<\/a><\/li>\n<li><a href=\"#g\">Github<\/a><\/li>\n<\/ol>\n<hr \/>\n<p><a id=\"a\"><\/a><\/p>\n<h4>1.\u5275\u5efa Annotation<\/h4>\n<h5>SerializedEncryption<\/h5>\n<pre><code class=\"language-Kotlin line-numbers\">@Retention(AnnotationRetention.RUNTIME)\n@Target(AnnotationTarget.FIELD)\nannotation class SerializedEncryption(\n    val type: String = \"Base64\"\n)\n<\/code><\/pre>\n<p><a id=\"b\"><\/a><\/p>\n<h4>2.\u6dfb\u52a0 MD5 \u5de5\u5177\u985e<\/h4>\n<pre><code class=\"language-Kotlin line-numbers\">object MDUtil {\n    fun encode(type: Type, data: String): String {\n        return try {\n            val digest = MessageDigest.getInstance(type.value)\n            digest.update(data.toByteArray())\n            \/\/32\u4f4d\u88dc0\n            String.format(\"%032X\", BigInteger(1, digest.digest()))\n        } catch (e: Exception) {\n            data\n        }\n    }\n\n    enum class Type(val value: String) {\n        MD5(\"MD5\"), SHA256(\"SHA-256\"), SHA512(\"SHA-512\");\n    }\n}\n<\/code><\/pre>\n<p><a id=\"c\"><\/a><\/p>\n<h4>3.\u5275\u5efa Annotation \u5be6\u73fe\u7684\u65b9\u6cd5<\/h4>\n<pre><code class=\"language-Kotlin line-numbers\">object SerializedUtil {\n\n    fun convertToRequestContent(model: Any?, field: Field): Array&lt;Any&gt;? {\n        val key = field.name\n        val originalValue = field[model]\n\n        if (TextUtils.isEmpty(key) || originalValue == null) {\n            return null\n        }\n\n        return if (originalValue is File) {\n            arrayOf(key, originalValue)\n        } else {\n            var value = originalValue.toString()\n\n            val serializedEncryption = field.getAnnotation(SerializedEncryption::class.java)\n            if (null != serializedEncryption) {\n                value = when (serializedEncryption.type) {\n                    \"MD5\" -&gt; {\n                        MDUtil.encode(MDUtil.Type.MD5, value).uppercase(Locale.getDefault())\n                    }\n                    \"SHA256\" -&gt; {\n                        MDUtil.encode(MDUtil.Type.SHA256, value).uppercase(Locale.getDefault())\n                    }\n                    \"SHA512\" -&gt; {\n                        MDUtil.encode(MDUtil.Type.SHA512, value).uppercase(Locale.getDefault())\n                    }\n                    else -&gt; {\n                       android.util.Base64.encodeToString(value.toByteArray(), android.util.Base64.DEFAULT)\n                    }\n                }\n            }\n\n            arrayOf(key, value)\n        }\n    }\n}\n<\/code><\/pre>\n<p><a id=\"d\"><\/a><\/p>\n<h4>4.\u4f7f\u7528 Annotation \u7684 Model<\/h4>\n<pre><code class=\"language-kotlin line-numbers\">data class Data(\n    @SerializedEncryption(type = \"SHA512\")\n    val sha512: String,\n    @SerializedEncryption(type = \"SHA256\")\n    val sha256: String,\n    @SerializedEncryption(type = \"MD5\")\n    val md5: String,\n    @SerializedEncryption\n    val base64: String\n)\n<\/code><\/pre>\n<p><a id=\"e\"><\/a><\/p>\n<h4>5.\u7a0b\u5f0f\u78bc\u7bc4\u4f8b<\/h4>\n<pre><code class=\"language-Kotlin line-numbers\">class MainActivity : AppCompatActivity() {\n\n    private lateinit var binding: ActivityMainBinding\n\n    override fun onCreate(savedInstanceState: Bundle?) {\n        super.onCreate(savedInstanceState)\n        setContentView(R.layout.activity_main)\n\n        binding = DataBindingUtil.setContentView(this, R.layout.activity_main)\n\n        val data = Data(\"Annotation\", \"Annotation\", \"Annotation\", \"Annotation\")\n\n        val fields = data.javaClass.declaredFields\n        Field.setAccessible(fields, true)\n\n        for (field in fields) {\n            val result = SerializedUtil.convertToRequestContent(data, field)\n            result?.apply {\n                Log.e(MainActivity::class.java.simpleName, get(0).toString() + \" : \" + get(1).toString())\n            }\n        }\n    }\n}\n<\/code><\/pre>\n<p><a id=\"f\"><\/a><\/p>\n<h4>6.\u6548\u679c\u5c55\u793a<\/h4>\n<p><a href=\"https:\/\/badgameshow.com\/fly\/wp-content\/uploads\/2021\/12\/\u87a2\u5e55\u64f7\u53d6\u756b\u9762-2022-12-14-134806.png\"><img decoding=\"async\" src=\"https:\/\/badgameshow.com\/fly\/wp-content\/uploads\/2021\/12\/\u87a2\u5e55\u64f7\u53d6\u756b\u9762-2022-12-14-134806.png\" width=\"100%\"\/><\/a><\/p>\n<p><a id=\"g\"><\/a><\/p>\n<h4>7.Github<\/h4>\n<p><a class=\"wp-editor-md-post-content-link\" href=\"https:\/\/github.com\/MuHongWeiWei\/AnnotationDemo\" target=\"_blank\" rel=\"noopener\">\u3010Android\u3011Annotation \u8a3b\u89e3 \u81ea\u5b9a\u7fa9\u5be6\u73fe MD5 \u529f\u80fd Github<\/a><\/p>\n\n<div style=\"font-size: 0px; height: 0px; line-height: 0px; margin: 0; padding: 0; clear: both;\"><\/div>","protected":false},"excerpt":{"rendered":"<p>\u3010Android\u3011Annotation \u8a3b\u89e3 \u81ea\u5b9a\u7fa9\u5be6\u73fe MD5 \u529f\u80fd Android Annotation\u662f &hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"pgc_sgb_lightbox_settings":"","footnotes":""},"categories":[5],"tags":[13,212,213],"class_list":["post-1565","post","type-post","status-publish","format-standard","hentry","category-android","tag-android","tag-annotation","tag-md5"],"_links":{"self":[{"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/posts\/1565","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/comments?post=1565"}],"version-history":[{"count":2,"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/posts\/1565\/revisions"}],"predecessor-version":[{"id":1625,"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/posts\/1565\/revisions\/1625"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/media?parent=1565"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/categories?post=1565"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/tags?post=1565"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}