github.com/tickstep/library-go@v0.1.1/util/bcypto.go (about) 1 package util 2 3 import ( 4 "encoding/hex" 5 "github.com/tickstep/library-go/crypto" 6 "github.com/tickstep/library-go/ids" 7 "golang.org/x/crypto/bcrypt" 8 ) 9 10 func PasswordBCrypto(password string) string { 11 cryptPwd, _ := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost) 12 return string(cryptPwd) 13 } 14 15 func VerifyPassword(hashPwd string, password string) bool { 16 err := bcrypt.CompareHashAndPassword([]byte(hashPwd), []byte(password)) 17 if err != nil{ 18 return false 19 } 20 return true 21 } 22 23 // EncryptString 加密 24 func EncryptString(text string) string { 25 if text == "" { 26 return "" 27 } 28 d := []byte(text) 29 key := []byte(ids.GetUniqueId("update-service-go", 16)) 30 r, e := crypto.EncryptAES(d, key) 31 if e != nil { 32 return text 33 } 34 return hex.EncodeToString(r) 35 } 36 37 // DecryptString 解密 38 func DecryptString(text string) string { 39 if text == "" { 40 return "" 41 } 42 d, _ := hex.DecodeString(text) 43 key := []byte(ids.GetUniqueId("update-service-go", 16)) 44 r, e := crypto.DecryptAES(d, key) 45 if e != nil { 46 return text 47 } 48 return string(r) 49 }