evcc-io/vehicle/saic/requests/encryption.go
2024-12-04 21:05:49 +01:00

87 lines
2 KiB
Go

package requests
import (
"strconv"
)
func CalculateRequestVerification(
resourcePath string,
sendDate int64,
tenant,
contentType,
bodyEncrypted,
token string,
) string {
dateString := strconv.FormatInt(sendDate, 10)
str9 := resourcePath + tenant + token + USER_TYPE
a2 := Md5(str9)
str10 := dateString + CONTENT_ENCRYPTED + contentType
a3 := Md5(a2 + str10)
str11 := resourcePath + tenant + token + USER_TYPE + dateString + CONTENT_ENCRYPTED + contentType + bodyEncrypted
a5 := Md5(a3 + dateString)
if len(a5) != 0 && len(str11) != 0 {
return HmacSha256(a5, str11)
} else {
return ""
}
}
func DecryptResponse(timeStamp, contentType, cipherText string) string {
str4 := timeStamp + CONTENT_ENCRYPTED + contentType
a2 := Md5(str4)
hashedTimeStamp := Md5(timeStamp)
if len(cipherText) != 0 {
return Decrypt(cipherText, a2, hashedTimeStamp)
}
return ""
}
func CalculateResponseVerification(str, str2, str3 string) string {
str4 := str + CONTENT_ENCRYPTED + str2
a2 := Md5(str4)
str5 := str + CONTENT_ENCRYPTED + str2 + str3
a4 := Md5(a2 + str)
return HmacSha256(a4, str5)
}
func EncryptRequest(path string, time int64, tenant, token, body, contentType string) string {
sendDate := strconv.FormatInt(time, 10)
// tenant
resourcePath := ""
if len(path) != 0 {
resourcePath = "/" + path
}
encryptedBody := ""
if len(body) != 0 {
sb3 := Md5(resourcePath+tenant+token+USER_TYPE) + sendDate + CONTENT_ENCRYPTED + contentType
a2 := Md5(sb3)
a3 := Md5(sendDate)
if len(body) != 0 && len(a2) != 0 && len(a3) != 0 {
encryptedBody = Encrypt(body, a2, a3)
}
}
return encryptedBody
}
func DecryptRequest(path string, time int64, tenant, token, body, contentType string) string {
timeStamp := strconv.FormatInt(time, 10)
resourcePath := ""
if len(path) != 0 {
resourcePath = "/" + path
}
if len(body) != 0 {
sb3 := Md5(resourcePath+tenant+token+USER_TYPE) + timeStamp + CONTENT_ENCRYPTED + contentType
a2 := Md5(sb3)
timeStampHash := Md5(timeStamp)
return Decrypt(body, a2, timeStampHash)
}
return ""
}