// Copyright 2019 Drone.IO Inc. All rights reserved. // Use of this source code is governed by the Drone Non-Commercial License // that can be found in the LICENSE file. package encrypt import ( "crypto/cipher" "crypto/rand" "errors" "io" ) type aesgcm struct { block cipher.Block } func (e *aesgcm) Encrypt(plaintext string) ([]byte, error) { gcm, err := cipher.NewGCM(e.block) if err != nil { return nil, err } nonce := make([]byte, gcm.NonceSize()) _, err = io.ReadFull(rand.Reader, nonce) if err != nil { return nil, err } return gcm.Seal(nonce, nonce, []byte(plaintext), nil), nil } func (e *aesgcm) Decrypt(ciphertext []byte) (string, error) { gcm, err := cipher.NewGCM(e.block) if err != nil { return "", err } if len(ciphertext) < gcm.NonceSize() { return "", errors.New("malformed ciphertext") } plaintext, err := gcm.Open(nil, ciphertext[:gcm.NonceSize()], ciphertext[gcm.NonceSize():], nil, ) return string(plaintext), err }