authorizer/server/crypto/aes.go

43 lines
1.2 KiB
Go
Raw Normal View History

2022-02-28 15:56:49 +00:00
package crypto
2021-12-31 08:22:10 +00:00
import (
"crypto/aes"
"crypto/cipher"
"github.com/authorizerdev/authorizer/server/constants"
2022-01-17 06:02:13 +00:00
"github.com/authorizerdev/authorizer/server/envstore"
2021-12-31 08:22:10 +00:00
)
2022-03-02 12:12:31 +00:00
var bytes = []byte{35, 46, 57, 24, 85, 35, 24, 74, 87, 35, 88, 98, 66, 32, 14, 05}
2021-12-31 08:22:10 +00:00
2022-03-02 12:12:31 +00:00
// EncryptAES method is to encrypt or hide any classified text
func EncryptAES(text string) (string, error) {
key := []byte(envstore.EnvStoreObj.GetStringStoreEnvVariable(constants.EnvKeyEncryptionKey))
block, err := aes.NewCipher(key)
2021-12-31 08:22:10 +00:00
if err != nil {
2022-03-02 12:12:31 +00:00
return "", err
2021-12-31 08:22:10 +00:00
}
2022-03-02 12:12:31 +00:00
plainText := []byte(text)
cfb := cipher.NewCFBEncrypter(block, bytes)
cipherText := make([]byte, len(plainText))
cfb.XORKeyStream(cipherText, plainText)
return EncryptB64(string(cipherText)), nil
2021-12-31 08:22:10 +00:00
}
2022-03-02 12:12:31 +00:00
// DecryptAES method is to extract back the encrypted text
func DecryptAES(text string) (string, error) {
2022-02-28 02:25:01 +00:00
key := []byte(envstore.EnvStoreObj.GetStringStoreEnvVariable(constants.EnvKeyEncryptionKey))
2022-03-02 12:12:31 +00:00
block, err := aes.NewCipher(key)
2021-12-31 08:22:10 +00:00
if err != nil {
2022-03-02 12:12:31 +00:00
return "", err
2021-12-31 08:22:10 +00:00
}
2022-03-02 12:12:31 +00:00
cipherText, err := DecryptB64(text)
2021-12-31 08:22:10 +00:00
if err != nil {
2022-03-02 12:12:31 +00:00
return "", err
2021-12-31 08:22:10 +00:00
}
2022-03-02 12:12:31 +00:00
cfb := cipher.NewCFBDecrypter(block, bytes)
plainText := make([]byte, len(cipherText))
cfb.XORKeyStream(plainText, []byte(cipherText))
return string(plainText), nil
2021-12-31 08:22:10 +00:00
}