Files
authorizer/server/crypto/aes.go

43 lines
1.2 KiB
Go
Raw Normal View History

2022-02-28 21:26:49 +05:30
package crypto
2021-12-31 13:52:10 +05:30
import (
"crypto/aes"
"crypto/cipher"
"github.com/authorizerdev/authorizer/server/constants"
2022-01-17 11:32:13 +05:30
"github.com/authorizerdev/authorizer/server/envstore"
2021-12-31 13:52:10 +05:30
)
2022-03-02 17:42:31 +05:30
var bytes = []byte{35, 46, 57, 24, 85, 35, 24, 74, 87, 35, 88, 98, 66, 32, 14, 05}
2021-12-31 13:52:10 +05:30
2022-03-02 17:42:31 +05:30
// 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 13:52:10 +05:30
if err != nil {
2022-03-02 17:42:31 +05:30
return "", err
2021-12-31 13:52:10 +05:30
}
2022-03-02 17:42:31 +05:30
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 13:52:10 +05:30
}
2022-03-02 17:42:31 +05:30
// DecryptAES method is to extract back the encrypted text
func DecryptAES(text string) (string, error) {
2022-02-28 07:55:01 +05:30
key := []byte(envstore.EnvStoreObj.GetStringStoreEnvVariable(constants.EnvKeyEncryptionKey))
2022-03-02 17:42:31 +05:30
block, err := aes.NewCipher(key)
2021-12-31 13:52:10 +05:30
if err != nil {
2022-03-02 17:42:31 +05:30
return "", err
2021-12-31 13:52:10 +05:30
}
2022-03-02 17:42:31 +05:30
cipherText, err := DecryptB64(text)
2021-12-31 13:52:10 +05:30
if err != nil {
2022-03-02 17:42:31 +05:30
return "", err
2021-12-31 13:52:10 +05:30
}
2022-03-02 17:42:31 +05:30
cfb := cipher.NewCFBDecrypter(block, bytes)
plainText := make([]byte, len(cipherText))
cfb.XORKeyStream(plainText, []byte(cipherText))
return string(plainText), nil
2021-12-31 13:52:10 +05:30
}