Perbaikan SEP
This commit is contained in:
@@ -7,28 +7,22 @@ import (
|
||||
"crypto/sha256"
|
||||
"encoding/base64"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
lzstring "github.com/daku10/go-lz-string"
|
||||
)
|
||||
|
||||
// ResponseVclaim decrypts the encrypted response from VClaim API
|
||||
func ResponseVclaim(encrypted string, key string) (string, error) {
|
||||
if encrypted == "" {
|
||||
return "", errors.New("encrypted response is empty")
|
||||
}
|
||||
|
||||
if key == "" {
|
||||
return "", errors.New("decryption key is empty")
|
||||
}
|
||||
|
||||
cipherText, err := base64.StdEncoding.DecodeString(encrypted)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to decode base64: %w", err)
|
||||
return "", err
|
||||
}
|
||||
|
||||
hash := sha256.Sum256([]byte(key))
|
||||
|
||||
block, err := aes.NewCipher(hash[:])
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to create cipher: %w", err)
|
||||
return "", err
|
||||
}
|
||||
|
||||
if len(cipherText) < aes.BlockSize {
|
||||
@@ -36,23 +30,20 @@ func ResponseVclaim(encrypted string, key string) (string, error) {
|
||||
}
|
||||
|
||||
iv := hash[:aes.BlockSize]
|
||||
|
||||
if len(cipherText)%aes.BlockSize != 0 {
|
||||
return "", errors.New("cipherText is not a multiple of the block size")
|
||||
}
|
||||
|
||||
mode := cipher.NewCBCDecrypter(block, iv)
|
||||
mode.CryptBlocks(cipherText, cipherText)
|
||||
|
||||
// Unpad the decrypted data
|
||||
cipherText, err = helper.Unpad(cipherText, aes.BlockSize)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to unpad: %w", err)
|
||||
}
|
||||
|
||||
// Decompress the data
|
||||
data, err := helper.DecompressFromEncodedUriComponent(string(cipherText))
|
||||
// cipherText, _ = pkcs7.Unpad(cipherText, aes.BlockSize)
|
||||
cipherText = helper.RemovePKCS7Padding(cipherText)
|
||||
data, err := lzstring.DecompressFromEncodedURIComponent(string(cipherText))
|
||||
// data, err := helper.DecompressFromEncodedUriComponent(string(cipherText))
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to decompress: %w", err)
|
||||
return "", err
|
||||
}
|
||||
|
||||
return data, nil
|
||||
|
||||
Reference in New Issue
Block a user