package utils

import (
	"crypto/md5"
	"encoding/base64"
	"encoding/hex"
	"strings"
)

type Encrypt struct{}

// EncodeMd5 将字符串加密成 md5
func (*Encrypt) EncodeMd5(buf []byte) string {
	hash := md5.New()
	hash.Write(buf)
	return hex.EncodeToString(hash.Sum(nil))
}

// Base64Encode base64编码
func (*Encrypt) Base64Encode(raw []byte) string {
	t := base64.StdEncoding.EncodeToString(raw)
	t = strings.TrimSpace(t)
	t = strings.Replace(t, "\r", "", -1)
	t = strings.Replace(t, "\n", "", -1)
	t = strings.Replace(t, "\n\r", "", -1)
	t = strings.Replace(t, "\r\n", "", -1)
	return t
}

// Base64Decode base64解码
func (*Encrypt) Base64Decode(raw string) ([]byte, error) {
	return base64.StdEncoding.DecodeString(raw)
}
