github.com/aaabigfish/gopkg@v1.1.0/crypto/hash.go (about) 1 package crypto 2 3 import ( 4 "crypto/hmac" 5 "crypto/md5" 6 "crypto/sha1" 7 "crypto/sha256" 8 "encoding/base64" 9 "encoding/hex" 10 ) 11 12 // 加盐 MD5 值 13 func Md5WithSalt(s, salt string) string { 14 h := md5.Sum([]byte(s + salt)) 15 return hex.EncodeToString(h[:]) 16 } 17 18 // MD5 值 19 func Md5(s string) string { 20 return Md5WithSalt(s, "") 21 } 22 23 // sha1 24 func Sha1(s string) string { 25 r := sha1.Sum([]byte(s)) 26 return hex.EncodeToString(r[:]) 27 } 28 29 // sha256 30 func Sha256(s string) string { 31 r := sha256.Sum256([]byte(s)) 32 return hex.EncodeToString(r[:]) 33 } 34 35 // 老接口 36 func HmacSha1(input, key string) string { 37 h := hmac.New(sha1.New, []byte(key)) 38 h.Write([]byte(input)) 39 s := base64.StdEncoding.EncodeToString(h.Sum(nil)) 40 return s 41 }