package utils

import (
	"strings"
)

func GetMD5Sign(params map[string]string, keys []string, paySecret string) string {
	str := ""
	for i := 0; i < len(keys); i++ {
		k := keys[i]
		if len(params[k]) == 0 || k == "sign" {
			continue
		}
		str += k + "=" + params[k] + "&"
	}
	str += "paySecret=" + paySecret
	sign := GetMD5Upper(str)
	return sign
}

func GetMD5SignLower(params map[string]string, keys []string, paySecret string) string {
	return strings.ToLower(GetMD5Sign(params, keys, paySecret))
}

func GetMD5SignMF(params map[string]string, paySecret string) string {
	strArr := SortMap(params)
	signStr := ""
	for i := 0; i < len(strArr); i++ {
		k := strArr[i]
		if len(params[k]) == 0 {
			signStr += k
		} else {
			signStr += k + params[k]
		}
	}
	signStr += paySecret
	return GetMD5LOWER(signStr)
}

func Md5Verify(params map[string]string, paySecret string) bool {
	sign := params["sign"]
	if sign == "" {
		return false
	}

	delete(params, "sign")
	keys := SortMap(params)
	tmpSign := GetMD5Sign(params, keys, paySecret)
	if tmpSign != sign {
		return false
	} else {
		return true
	}
}

func Md5MFVerify(params map[string]string, paySecret string) bool {
	sign := params["sign"]
	if sign == "" {
		return false
	}
	delete(params, "sign")
	tmpSign := GetMD5SignMF(params, paySecret)
	if tmpSign != sign {
		return false
	} else {
		return true
	}
}
