MCPcopy Create free account
hub / github.com/Preloading/TwitterAPIBridge / Encrypt

Function Encrypt

cryption/authcrypt.go:27–50  ·  view source on GitHub ↗

Encrypt encrypts the given plaintext using AES-GCM with the provided base64 encoded key and returns the ciphertext as a base64 encoded string

(plaintext, base64Key string)

Source from the content-addressed store, hash-verified

25
26// Encrypt encrypts the given plaintext using AES-GCM with the provided base64 encoded key and returns the ciphertext as a base64 encoded string
27func Encrypt(plaintext, base64Key string) (string, error) {
28 key, err := base64.StdEncoding.DecodeString(base64Key)
29 if err != nil {
30 return "", err
31 }
32
33 block, err := aes.NewCipher(key)
34 if err != nil {
35 return "", err
36 }
37
38 gcm, err := cipher.NewGCM(block)
39 if err != nil {
40 return "", err
41 }
42
43 nonce := make([]byte, gcm.NonceSize())
44 if _, err := io.ReadFull(rand.Reader, nonce); err != nil {
45 return "", err
46 }
47
48 ciphertext := gcm.Seal(nonce, nonce, []byte(plaintext), nil)
49 return base64.StdEncoding.EncodeToString(ciphertext), nil
50}
51
52// Decrypt decrypts the given base64 encoded ciphertext using AES-GCM with the provided base64 encoded key and returns the plaintext
53func Decrypt(base64Ciphertext, base64Key string) (string, error) {

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected