-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2e8d01c
commit ba032d3
Showing
2 changed files
with
124 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package helpers | ||
|
||
import ( | ||
"crypto/aes" | ||
"crypto/cipher" | ||
"crypto/hmac" | ||
"crypto/sha1" | ||
"encoding/base64" | ||
"encoding/hex" | ||
"encoding/json" | ||
"errors" | ||
"strings" | ||
) | ||
|
||
type laravelAesKey struct { | ||
Iv []byte `json:"iv"` | ||
Value []byte `json:"value"` | ||
Mac string `json:"mac"` | ||
Tag string `json:"tag"` | ||
} | ||
|
||
func LaravelAesDecrypt(name, payload string) (string, error) { | ||
cipherText, err := base64.StdEncoding.DecodeString(payload) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
aesKey := laravelAesKey{} | ||
err = json.Unmarshal(cipherText, &aesKey) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
dummykey := Env("APP_KEY", "") | ||
dummykey = dummykey[7:] | ||
|
||
key, err := base64.StdEncoding.DecodeString(dummykey) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
block, err := aes.NewCipher(key) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
if len(aesKey.Value)%aes.BlockSize != 0 { | ||
return "", errors.New("block size cant be zero") | ||
} | ||
|
||
mode := cipher.NewCBCDecrypter(block, aesKey.Iv) | ||
mode.CryptBlocks(aesKey.Value, aesKey.Value) | ||
plainText := string(unpadding(aesKey.Value)) | ||
|
||
validator := createValidator(name, key) | ||
if strings.HasPrefix(plainText, validator) { | ||
plainText = strings.TrimPrefix(plainText, validator) | ||
} else { | ||
return "", errors.New("token cannot be validated") | ||
} | ||
|
||
return plainText, nil | ||
} | ||
|
||
func unpadding(src []byte) []byte { | ||
length := len(src) | ||
unpadding := int(src[length-1]) | ||
|
||
return src[:(length - unpadding)] | ||
} | ||
|
||
func createValidator(cookieName string, key []byte) string { | ||
h := hmac.New(sha1.New, key) | ||
h.Write([]byte(cookieName + "v2")) | ||
return hex.EncodeToString(h.Sum(nil)) + "|" | ||
} |