先從個簡單點的,也是用的比較多MD5加密開始,很少說直接上代碼html
package sign import "crypto/md5" type MD5Client struct { } var MD5 = MD5Client{} func (this *MD5Client) Encrypt(plantext []byte) []byte { result := md5.Sum(plantext) return result[:] } /* 給要加密的信息加把鹽 */ func (this *MD5Client) EncryptWithSalt(plantext []byte,salt []byte) []byte { hash := md5.New() hash.Write(plantext) hash.Write(salt) return hash.Sum(nil) }
加密後的獲得長度爲16的一個byte數組,若是想轉成string,可使用16進制字符集進行轉碼,代碼代碼以下ui
func main(){ sum:=sign.MD5.Encrypt([]byte(`紅薯鴨`)) sumStr:=hex.EncodeToString(sum) }
OK,MD5到此結束,簡單吧,下回我們聊聊AES...this