package com.yidian.common.utils

import okhttp3.internal.and
import java.security.MessageDigest
import java.security.NoSuchAlgorithmException

class EncryptUtil {
    companion object{
        fun getMD5(info: String): String{
            val md5: MessageDigest = MessageDigest.getInstance("MD5")
            md5.update(info.toByteArray())
            val m = md5.digest()
            return byte2hex(m)
        }

        fun encryptToSHA(info: String): String{
            val digesta: ByteArray
            var rs = ""
            try {
                val alga: MessageDigest = MessageDigest.getInstance("SHA-1")
                alga.update(info.toByteArray())
                digesta = alga.digest()
                rs = byte2hex(digesta)
            } catch (e: NoSuchAlgorithmException) {
                e.printStackTrace()
            }
            return rs
        }

        private fun byte2hex(b: ByteArray): String{
            var hs = ""
            var stmp: String
            repeat(b.size){
                stmp = Integer.toHexString(b[it] and  0XFF)
                hs = if(stmp.length == 1){
                    hs + "0" + stmp
                }else{
                    hs + stmp
                }
            }
            return hs
        }
    }
}