MCPcopy Create free account
hub / github.com/authorizerdev/authorizer / DecryptAES

Function DecryptAES

internal/crypto/aes.go:59–92  ·  view source on GitHub ↗

DecryptAES decrypts a base64 RawURL-encoded AES-256-GCM ciphertext produced by EncryptAES. Returns an error if authentication fails or input is malformed.

(key, encryptedText string)

Source from the content-addressed store, hash-verified

57// DecryptAES decrypts a base64 RawURL-encoded AES-256-GCM ciphertext produced
58// by EncryptAES. Returns an error if authentication fails or input is malformed.
59func DecryptAES(key, encryptedText string) (string, error) {
60 keyBytes, err := deriveAESKey(key)
61 if err != nil {
62 return "", err
63 }
64
65 data, err := base64.RawURLEncoding.DecodeString(encryptedText)
66 if err != nil {
67 return "", err
68 }
69
70 block, err := aes.NewCipher(keyBytes)
71 if err != nil {
72 return "", err
73 }
74
75 gcm, err := cipher.NewGCM(block)
76 if err != nil {
77 return "", err
78 }
79
80 nonceSize := gcm.NonceSize()
81 if len(data) < nonceSize {
82 return "", errors.New("ciphertext too short")
83 }
84
85 nonce, ciphertext := data[:nonceSize], data[nonceSize:]
86 plaintext, err := gcm.Open(nil, nonce, ciphertext, nil)
87 if err != nil {
88 return "", err
89 }
90
91 return string(plaintext), nil
92}

Callers 10

LogoutHandlerMethod · 0.92
AuthorizeHandlerMethod · 0.92
TestEncryptDecryptAESFunction · 0.85
TestDecryptAES_WrongKeyFunction · 0.85
TestDecryptAES_TooShortFunction · 0.85
DecryptTOTPSecretFunction · 0.85

Calls 1

deriveAESKeyFunction · 0.85

Tested by 6

TestEncryptDecryptAESFunction · 0.68
TestDecryptAES_WrongKeyFunction · 0.68
TestDecryptAES_TooShortFunction · 0.68