update response
This commit is contained in:
@@ -1,6 +1,10 @@
|
||||
package docs
|
||||
|
||||
import (
|
||||
"crypto/aes"
|
||||
"crypto/cipher"
|
||||
"crypto/sha256"
|
||||
"encoding/base64"
|
||||
"errors"
|
||||
"math"
|
||||
"unicode/utf8"
|
||||
@@ -109,3 +113,71 @@ func DecompressFromEncodedUriComponent(input string) (string, error) {
|
||||
|
||||
return "", errors.New("Unexpected end of buffer reached.")
|
||||
}
|
||||
|
||||
func StringDecrypt(key string, str string) string {
|
||||
// hash
|
||||
keyHash := sha256.Sum256([]byte(key))
|
||||
|
||||
// iv - encrypt method AES-256-CBC expects 16 bytes - else you will get a warning
|
||||
iv := keyHash[:16]
|
||||
|
||||
// create a new aes cipher using the key and iv
|
||||
block, err := aes.NewCipher(keyHash[:32])
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
// decode the base64 string to a []byte
|
||||
ciphertext, _ := base64.StdEncoding.DecodeString(str)
|
||||
|
||||
// create the decrypter
|
||||
decrypter := cipher.NewCBCDecrypter(block, iv)
|
||||
|
||||
// decrypt
|
||||
decrypted := make([]byte, len(ciphertext))
|
||||
decrypter.CryptBlocks(decrypted, ciphertext)
|
||||
|
||||
// remove padding
|
||||
padLen := int(decrypted[len(decrypted)-1])
|
||||
decrypted = decrypted[:len(decrypted)-padLen]
|
||||
|
||||
return string(decrypted)
|
||||
}
|
||||
|
||||
func ResponseVclaim(encrypted string, key string) (string, error) {
|
||||
|
||||
cipherText, err := base64.StdEncoding.DecodeString(encrypted)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
hash := sha256.Sum256([]byte(key))
|
||||
|
||||
block, err := aes.NewCipher(hash[:])
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
if len(cipherText) < aes.BlockSize {
|
||||
return "", errors.New("cipherText too short")
|
||||
}
|
||||
|
||||
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)
|
||||
|
||||
// cipherText, _ = pkcs7.Unpad(cipherText, aes.BlockSize)
|
||||
cipherText, _ = Unpad(cipherText, aes.BlockSize)
|
||||
// data, err := lzstring.DecompressFromEncodedURIComponent(string(cipherText))
|
||||
data, err := DecompressFromEncodedUriComponent(string(cipherText))
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return data, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user