Golang加密解密

来源:互联网 时间:1970-01-01

package commonimport ("crypto/md5""crypto/rand""encoding/base64""encoding/hex""io""strings")const (//BASE64字符表,不要有重复base64Table = "<>:;',./[email protected]#$CDVWX%^&*ABYZabcghijklmnopqrstuvwxyz01EFGHIJKLMNOPQRSTU2345678(def)_+|{}[]9/"hashFunctionHeader = "zh.ife.iya"hashFunctionFooter = "09.O25.O20.78")/** * 对一个字符串进行MD5加密,不可解密 */func GetMd5String(s string) string {h := md5.New()h.Write([]byte(s + "zhifeiya")) //使用zhifeiya名字做散列值,设定后不要变return hex.EncodeToString(h.Sum(nil))}/** * 获取一个Guid值 */func GetGuid() string {b := make([]byte, 48)if _, err := io.ReadFull(rand.Reader, b); err != nil {return ""}return GetMd5String(base64.URLEncoding.EncodeToString(b))}var coder = base64.NewEncoding(base64Table)/** * base64加密 */func Base64Encode(str string) string {var src []byte = []byte(hashFunctionHeader + str + hashFunctionFooter)return string([]byte(coder.EncodeToString(src)))}/** * base64解密 */func Base64Decode(str string) (string, error) {var src []byte = []byte(str)by, err := coder.DecodeString(string(src))return strings.Replace(strings.Replace(string(by), hashFunctionHeader, "", -1), hashFunctionFooter, "", -1), err}

版权声明:本文为博主原创文章,未经博主允许不得转载。


相关阅读:
Top